Class: Opsgenie::Schedule

Inherits:
Object
  • Object
show all
Defined in:
lib/opsgenie/schedule.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs) ⇒ Schedule

Returns a new instance of Schedule.



27
28
29
30
31
# File 'lib/opsgenie/schedule.rb', line 27

def initialize(attrs)
  @id = attrs["id"]
  @name = attrs["name"]
  @rotations = attrs["rotations"].map { |r| Rotation.new(self, r) }
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



5
6
7
# File 'lib/opsgenie/schedule.rb', line 5

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/opsgenie/schedule.rb', line 5

def name
  @name
end

#rotationsObject (readonly)

Returns the value of attribute rotations.



5
6
7
# File 'lib/opsgenie/schedule.rb', line 5

def rotations
  @rotations
end

Class Method Details

.allObject



8
9
10
11
# File 'lib/opsgenie/schedule.rb', line 8

def all
  body = Opsgenie::Client.get("schedules?expand=rotation")
  body["data"].map { |s| new(s) }
end

.find(id_or_name, type = "id") ⇒ Object



21
22
23
24
# File 'lib/opsgenie/schedule.rb', line 21

def find(id_or_name, type = "id")
  body = Opsgenie::Client.get("schedules/#{id_or_name}?identifierType=#{type}")
  new(body["data"]) unless body["data"].nil?
end

.find_by_id(id) ⇒ Object



17
18
19
# File 'lib/opsgenie/schedule.rb', line 17

def find_by_id(id)
  find(id)
end

.find_by_name(name) ⇒ Object



13
14
15
# File 'lib/opsgenie/schedule.rb', line 13

def find_by_name(name)
  find(name, "name")
end

Instance Method Details

#on_calls(datetime = nil) ⇒ Object



33
34
35
36
37
38
# File 'lib/opsgenie/schedule.rb', line 33

def on_calls(datetime = nil)
  endpoint = "schedules/#{id}/on-calls"
  endpoint += "?date=#{escape_datetime(datetime)}" unless datetime.nil?
  body = Opsgenie::Client.get(endpoint)
  get_participants(body).map { |u| User.find_by_username(u["name"]) }
end

#timeline(date: Date.today, interval: nil, interval_unit: nil) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/opsgenie/schedule.rb', line 40

def timeline(date: Date.today, interval: nil, interval_unit: nil)
  check_interval_unit(interval_unit) if interval_unit

  datetime = date.to_datetime
  endpoint = "schedules/#{id}/timeline?date=#{escape_datetime(datetime)}"
  endpoint += "&interval=#{interval}" if interval
  endpoint += "&intervalUnit=#{interval_unit}" if interval_unit
  body = Opsgenie::Client.get(endpoint)
  body.dig("data", "finalTimeline", "rotations").map do |rotation|
    TimelineRotation.new(
      id: rotation["id"],
      name: rotation["name"],
      periods: (rotation["periods"] || [])
    )
  end
end