Class: Pagerduty

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(http, token, email, teams = []) ⇒ Pagerduty

Returns a new instance of Pagerduty.



4
5
6
7
8
9
10
# File 'lib/pagerduty.rb', line 4

def initialize(http, token, email, teams = [])
  @http = http
  @teams = teams || []

  http.url_prefix = 'https://api.pagerduty.com/'
  http.headers = auth_headers(email, token)
end

Instance Attribute Details

#httpObject (readonly)

Returns the value of attribute http.



2
3
4
# File 'lib/pagerduty.rb', line 2

def http
  @http
end

#teamsObject (readonly)

Returns the value of attribute teams.



2
3
4
# File 'lib/pagerduty.rb', line 2

def teams
  @teams
end

Instance Method Details

#get_incident(id = '404stub') ⇒ Object



40
41
42
43
44
45
# File 'lib/pagerduty.rb', line 40

def get_incident(id = '404stub')
  response = http.get "/incidents/#{id}"
  raise Exceptions::IncidentNotFound if response.status == 404

  parse_json_response(response, :incident)
end

#get_incidents(params = {}) ⇒ Object



12
13
14
15
16
17
# File 'lib/pagerduty.rb', line 12

def get_incidents(params = {})
  data = get_resources(:incidents, params)
  raise Exceptions::IncidentsEmptyList if data.empty?

  data
end

#get_notes_by_incident_id(incident_id) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/pagerduty.rb', line 47

def get_notes_by_incident_id(incident_id)
  response = http.get "/incidents/#{incident_id}/notes"
  raise Exceptions::IncidentNotFound if response.status == 404

  data = parse_json_response(response, :notes, [])
  raise Exceptions::NotesEmptyList if data.empty?

  data
end

#get_oncall_user(params = {}) ⇒ Object



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

def get_oncall_user(params = {})
  data = get_resources(:oncalls, params)
  raise Exceptions::NoOncallUser if data.empty?

  data.first.fetch(:user)
end

#get_schedules(params = {}) ⇒ Object



26
27
28
29
30
31
# File 'lib/pagerduty.rb', line 26

def get_schedules(params = {})
  data = get_resources(:schedules, params)
  raise Exceptions::SchedulesEmptyList if data.empty?

  data
end

#get_users(params = {}) ⇒ Object



19
20
21
22
23
24
# File 'lib/pagerduty.rb', line 19

def get_users(params = {})
  data = get_resources(:users, params)
  raise Exceptions::UsersEmptyList if data.empty?

  data
end

#manage_incidents(action, ids) ⇒ Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/pagerduty.rb', line 57

def manage_incidents(action, ids)
  incidents = ids.map do |id|
    { id: id, type: 'incident_reference', status: "#{action}d" }
  end
  payload = { incidents: incidents }
  response = http.put '/incidents', payload
  raise Exceptions::IncidentManageUnsuccess if response.status != 200

  response
end

#override(schedule_id, user_id, minutes) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/pagerduty.rb', line 68

def override(schedule_id, user_id, minutes)
  payload = override_payload(user_id, minutes)
  response = http.post("/schedules/#{schedule_id}/overrides", payload)
  raise Exceptions::OverrideUnsuccess if response.status != 201

  parse_json_response(response, :override)
end