Class: Pagerduty

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

Instance Method Summary collapse

Constructor Details

#initialize(http, token, email) ⇒ Pagerduty

Returns a new instance of Pagerduty.



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

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

Instance Method Details

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



43
44
45
46
47
48
# File 'lib/pagerduty.rb', line 43

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

  JSON.parse(response.body, symbolize_names: true).fetch(:incident, nil)
end

#get_incidents(params = {}) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/pagerduty.rb', line 9

def get_incidents(params = {})
  response = @http.get '/incidents', params
  data = JSON.parse(response.body, symbolize_names: true)
             .fetch(:incidents, [])
  raise Exceptions::IncidentsEmptyList if data.empty?

  data
end

#get_notes_by_incident_id(incident_id) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/pagerduty.rb', line 50

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

  data = JSON.parse(response.body, symbolize_names: true).fetch(:notes, [])
  raise Exceptions::NotesEmptyList if data.empty?

  data
end

#get_oncall_user(params = {}) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/pagerduty.rb', line 35

def get_oncall_user(params = {})
  response = @http.get '/oncalls', params
  data = JSON.parse(response.body, symbolize_names: true).fetch(:oncalls, [])
  raise Exceptions::NoOncallUser if data.empty?

  data.first.fetch(:user)
end

#get_schedules(params = {}) ⇒ Object



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

def get_schedules(params = {})
  response = @http.get '/schedules', params
  data = JSON.parse(response.body, symbolize_names: true)
             .fetch(:schedules, [])
  raise Exceptions::SchedulesEmptyList if data.empty?

  data
end

#get_users(params = {}) ⇒ Object



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

def get_users(params = {})
  response = @http.get '/users', params
  data = JSON.parse(response.body, symbolize_names: true).fetch(:users, [])
  raise Exceptions::UsersEmptyList if data.empty?

  data
end

#manage_incidents(action, ids) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'lib/pagerduty.rb', line 60

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



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/pagerduty.rb', line 71

def override(schedule_id, user_id, minutes)
  from = ::Time.now.utc + 10
  to = from + (60 * minutes)
  payload = { override: {
    start: from.iso8601,
    end: to.iso8601,
    user: { id: user_id, type: 'user_reference' }
  } }
  response = @http.post "/schedules/#{schedule_id}/overrides", payload
  raise Exceptions::OverrideUnsuccess if response.status != 201

  JSON.parse(response.body, symbolize_names: true).fetch(:override, nil)
end