Class: Pagerduty

Inherits:
Object
  • Object
show all
Defined in:
lib/pagerduty/pagerduty.rb,
lib/pagerduty/models/user.rb,
lib/pagerduty/models/report.rb,
lib/pagerduty/models/report.rb,
lib/pagerduty/models/report.rb,
lib/pagerduty/models/report.rb,
lib/pagerduty/models/schedule.rb,
lib/pagerduty/models/schedule.rb,
lib/pagerduty/models/schedule.rb,
lib/pagerduty/models/schedule.rb,
lib/pagerduty/models/schedule.rb,
lib/pagerduty/models/schedule.rb,
lib/pagerduty/models/schedule.rb,
lib/pagerduty/models/schedule.rb,
lib/pagerduty/models/services.rb,
lib/pagerduty/models/services.rb,
lib/pagerduty/models/log_entry.rb,
lib/pagerduty/models/maintenance_window.rb,
lib/pagerduty/models/maintenance_window.rb

Defined Under Namespace

Classes: LogEntry, MaintenanceWindow, MaintenanceWindows, Reports, ScheduleInfo, Schedules, Services, User

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Pagerduty

Returns a new instance of Pagerduty.



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

def initialize(options)
  @@token = options[:token]
  @@subdomain = options[:subdomain]
end

Instance Attribute Details

#subdomainObject (readonly)

Returns the value of attribute subdomain.



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

def subdomain
  @subdomain
end

#tokenObject (readonly)

Returns the value of attribute token.



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

def token
  @token
end

Instance Method Details

#alerts(options = {}) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/pagerduty/pagerduty.rb', line 89

def alerts(options={})

  unless has_requirements? [:since, :until], options
    puts "#> This function requires arguments :since, :until"
    puts "Please see: http://developer.pagerduty.com/documentation/rest/alerts/list"
    return
  end

  JSON.parse(curl({
    uri: "https://#@@subdomain.pagerduty.com/api/v1/alerts",
    params: options,
    method: 'GET'
  }).body)['alerts'].inject([]) { |alerts, alert|
    alerts << Alert.new(alert)
  }

end

#alerts_per_time(options = {}) ⇒ Object



276
277
278
279
280
281
282
# File 'lib/pagerduty/pagerduty.rb', line 276

def alerts_per_time(options={})
  Pagerduty::Reports::Alerts.new(JSON.parse(curl({
    uri: "https://#@@subdomain.pagerduty.com/api/v1/reports/alerts_per_time",
    params: options,
    method: 'GET'
  }).body))
end

#create_escalation_policy(options = {}) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/pagerduty/pagerduty.rb', line 117

def create_escalation_policy(options={})

  if options[:escalation_rules]
    options[:escalation_rules] = options[:escalation_rules].map { |rule| 
      rule.class == EscalationRule ? rule.hashify : rule
    }
  end

  EscalationPolicy.new(JSON.parse(curl({
    uri: "https://#@@subdomain.pagerduty.com/api/v1/escalation_policies",
    data: options,
    method: 'POST'
  }).body)['escalation_policy'])

end

#create_maintenance_window(options = {}) ⇒ Object



308
309
310
311
312
313
314
# File 'lib/pagerduty/pagerduty.rb', line 308

def create_maintenance_window(options={})
  Pagerduty::MaintenanceWindow.new(JSON.parse(curl({
    uri: "https://#@@subdomain.pagerduty.com/api/v1/maintenance_windows",
    data: options,
    method: 'POST'
  }).body)['maintenance_window'])
end

#create_service(options = {}) ⇒ Object



358
359
360
361
362
363
364
# File 'lib/pagerduty/pagerduty.rb', line 358

def create_service(options={})
  Pagerduty::Services::Objects::Service.new(JSON.parse(curl({
    uri: "https://#@@subdomain.pagerduty.com/api/v1/services",
    data: { service: options },
    method: 'POST'
  }).body)['service'])
end

#create_user(options = {}) ⇒ Object



243
244
245
246
247
248
249
# File 'lib/pagerduty/pagerduty.rb', line 243

def create_user(options={})
  User.new(JSON.parse(curl({
    uri: "https://#@@subdomain.pagerduty.com/api/v1/users",
    data: options,
    method: 'POST'
  }).body)['user'])
end

#curl(options) ⇒ Object

def curl

Purpose: Performs a CURL request

Params: options<~Hash> - The options Hash to send

uri<~String> - The URI to curl
ssl<~Boolean><Optional> - Whether or not to use SSL
port<~Integer><Optional> - The port number to connect to
params<~Hash><Optional> - The params to send in the curl request
headers<~Hash><Optional> - The headers to send in the curl request
method<~String> - The HTTP method to perform
basic_auth<~Hash><Optional>
  user<~String> - Basic auth user
  password<~String> - Basic auth password

Returns: <String>



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/pagerduty/pagerduty.rb', line 30

def curl(options)

  curl_request = {
    ssl: true,
    port: 443,
    headers: {
      "Content-Type" => "application/json",
      "Authorization" => "Token token=#@@token",
    },
  }

  options.merge! curl_request

  url = URI.parse(options[:uri])

  if options[:params]
    parameters = options[:params].map { |k,v| "#{k}=#{v}" }.join("&")
    url += "?#{parameters}"
  end

  http = Net::HTTP.new(url.host, 443)
  http.use_ssl = true

  request = case options[:method]
            when 'DELETE'
              Net::HTTP::Delete.new(url)
            when 'GET'
              Net::HTTP::Get.new(url)
            when 'POST'
              Net::HTTP::Post.new(url)
            when 'PUT'
              Net::HTTP::Put.new(url)
            end

  if options.has_key?(:data)
    request.set_form_data(options[:data])
  end

  if options.has_key?(:basic_auth)
    request.basic_auth options[:basic_auth][:user], options[:basic_auth][:password]
  end

  request.body = options[:body]

  options[:headers].each { |key,val| request.add_field(key,val) }

  if options[:method] == 'POST'
    http.post(url.path,options[:data].to_json,options[:headers])
  elsif options[:method] == 'PUT'
    http.put(url.path,options[:data].to_json,options[:headers])
  else
    http.request(request)
  end
end

#escalation_policies(options = {}) ⇒ Object



107
108
109
110
111
112
113
114
115
# File 'lib/pagerduty/pagerduty.rb', line 107

def escalation_policies(options={})
  JSON.parse(curl({
    uri: "https://#@@subdomain.pagerduty.com/api/v1/escalation_policies",
    params: { 'query' => options[:query] },
    method: 'GET'
  }).body)['escalation_policies'].inject([]) { |policies, policy|
    policies << EscalationPolicy.new(policy)
  }
end

#escalation_rules(options) ⇒ Object



140
141
142
143
144
145
146
147
148
# File 'lib/pagerduty/pagerduty.rb', line 140

def escalation_rules(options)
  JSON.parse(curl({
    uri: "https://#@@subdomain.pagerduty.com/api/v1/escalation_policies/#{options[:escalation_policy_id]}/escalation_rules",
    params: { 'query' => options[:query] },
    method: 'GET'
  }).body)['escalation_rules'].inject([]) { |rules, rule|
    rules << EscalationRule.new(rule)
  }
end

#get_escalation_policy(options = {}) ⇒ Object



133
134
135
136
137
138
# File 'lib/pagerduty/pagerduty.rb', line 133

def get_escalation_policy(options={})
  EscalationPolicy.new(JSON.parse(curl({
    uri: "https://#@@subdomain.pagerduty.com/api/v1/escalation_policies/#{options[:id]}",
    method: 'GET'
  }).body)['escalation_policy'])
end

#get_escalation_rule(options = {}) ⇒ Object



150
151
152
153
154
155
# File 'lib/pagerduty/pagerduty.rb', line 150

def get_escalation_rule(options={})
  JSON.parse(curl({
    uri: "https://#@@subdomain.pagerduty.com/api/v1/escalation_policies/#{options[:escalation_policy_id]}/escalation_rules/#{options[:rule_id]}",
    method: 'GET'
  }).body)
end

#get_incident(options = {}) ⇒ Object



219
220
221
# File 'lib/pagerduty/pagerduty.rb', line 219

def get_incident(options={})
  incidents.detect { |incident| incident.id == options[:id] } || 'No results'
end

#get_incident_counts(options = {}) ⇒ Object



223
224
225
226
227
228
229
# File 'lib/pagerduty/pagerduty.rb', line 223

def get_incident_counts(options={})
  JSON.parse(curl({
    uri: "https://#@@subdomain.pagerduty.com/api/v1/incidents/count",
    params: options,
    method: 'GET',
  }).body)
end

#get_log_entries(options = {}) ⇒ Object



260
261
262
263
264
265
266
# File 'lib/pagerduty/pagerduty.rb', line 260

def get_log_entries(options={})
  LogEntries.new(JSON.parse(curl({
    uri: "https://#@@subdomain.pagerduty.com/api/v1/log_entries",
    params: options,
    method: 'GET'
  }).body))
end

#get_log_entry(options = {}) ⇒ Object



268
269
270
271
272
273
274
# File 'lib/pagerduty/pagerduty.rb', line 268

def get_log_entry(options={})
  LogEntry.new(JSON.parse(curl({
    uri: "https://#@@subdomain.pagerduty.com/api/v1/log_entries/#{options[:id]}",
    params: options,
    method: 'GET'
  }).body)['log_entry'])
end

#get_maintenance_window(options = {}) ⇒ Object



300
301
302
303
304
305
306
# File 'lib/pagerduty/pagerduty.rb', line 300

def get_maintenance_window(options={})
  Pagerduty::MaintenanceWindow.new(JSON.parse(curl({
    uri: "https://#@@subdomain.pagerduty.com/api/v1/maintenance_windows/#{options[:id]}",
    params: options,
    method: 'GET'
  }).body)['maintenance_window'])
end

#get_maintenance_windows(options = {}) ⇒ Object



292
293
294
295
296
297
298
# File 'lib/pagerduty/pagerduty.rb', line 292

def get_maintenance_windows(options={})
  Pagerduty::MaintenanceWindows.new(JSON.parse(curl({
    uri: "https://#@@subdomain.pagerduty.com/api/v1/maintenance_windows",
    params: options,
    method: 'GET'
  }).body))
end

#get_schedule(options = {}) ⇒ Object



324
325
326
327
328
329
330
# File 'lib/pagerduty/pagerduty.rb', line 324

def get_schedule(options={})
  Pagerduty::ScheduleInfo.new(JSON.parse(curl({
    uri: "https://#@@subdomain.pagerduty.com/api/v1/schedules/#{options[:id]}",
    params: options,
    method: 'GET'
   }).body)['schedule'])
end

#get_schedule_users(options = {}) ⇒ Object



332
333
334
335
336
337
338
339
340
# File 'lib/pagerduty/pagerduty.rb', line 332

def get_schedule_users(options={})
  JSON.parse(curl({
    uri: "https://#@@subdomain.pagerduty.com/api/v1/schedules/#{options[:id]}/users",
    params: options,
    method: 'GET'
  }).body)['users'].inject([]) { |users, user|
    users << Pagerduty::User.new(user)
  }
end

#get_schedules(options = {}) ⇒ Object



316
317
318
319
320
321
322
# File 'lib/pagerduty/pagerduty.rb', line 316

def get_schedules(options={})
  Pagerduty::Schedules.new(JSON.parse(curl({
    uri: "https://#@@subdomain.pagerduty.com/api/v1/schedules",
    params: options,
    method: 'GET'
  }).body))
end

#get_service(options = {}) ⇒ Object



350
351
352
353
354
355
356
# File 'lib/pagerduty/pagerduty.rb', line 350

def get_service(options={})
  Pagerduty::Services::Objects::Service.new(JSON.parse(curl({
    uri: "https://#@@subdomain.pagerduty.com/api/v1/services/#{options[:id]}",
    params: options,
    method: 'GET'
  }).body)['service'])
end

#get_services(options = {}) ⇒ Object



342
343
344
345
346
347
348
# File 'lib/pagerduty/pagerduty.rb', line 342

def get_services(options={})
  Pagerduty::Services.new(JSON.parse(curl({
    uri: "https://#@@subdomain.pagerduty.com/api/v1/services",
    params: options,
    method: 'GET'
  }).body))
end

#get_user(options = {}) ⇒ Object



239
240
241
# File 'lib/pagerduty/pagerduty.rb', line 239

def get_user(options={})
  get_users.users.detect { |user| user.id == options[:id] }
end

#get_users(options = {}) ⇒ Object



231
232
233
234
235
236
237
# File 'lib/pagerduty/pagerduty.rb', line 231

def get_users(options={})
  Users.new(JSON.parse(curl({
    uri: "https://#@@subdomain.pagerduty.com/api/v1/users",
    params: { query: options[:query] },
    method: 'GET'
  }).body))
end

#has_requirements?(keys, options) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/pagerduty/pagerduty.rb', line 85

def has_requirements?(keys,options)
  (keys - options.keys).empty?
end

#incidents(options = {}) ⇒ Object

Retrieve all incidents

Parameters

  • params<~Hash>

    • ‘since’<~String>: The start of the date range over which to search

    • ‘until’<~String>: The end of the date range over which to search

    • ‘date_range’<~String>: When set to ‘all’ the ‘since’ and ‘until’ params are ignored. Use this to get all incidents since the account was created

    • ‘fields’<~String>: Used to restrict the properties of each incident returned to a set of pre-defined fields. If omitted, returned incidents have all fields present. See below for a list of possible fields.

    • ‘status’<~String>: Returns only the incidents currently in the passed status(es). Valid status options are triggered, acknowledged, and resolved. More status codes may be introduced in the future.

    • ‘incident_key’<~String>: Returns only the incidents with the passed de-duplication key. See the PagerDuty Integration API docs for further details.

    • ‘service’<~String>: Returns only the incidents associated with the passed service(s). This expects one or more service IDs. Separate multiple IDs by commas.

    • ‘assigned_to_user’<~String>: Returns only the incidents currently assigned to the passed user(s). This expects one or more user IDs. Please see below for more info on how to find your users’ IDs. When using the assigned_to_user filter, you will only receive incidents with statuses of triggered or acknowledged. This is because resolved incidents are not assigned to any user.

    • ‘time_zone’<~String>: Time zone in which dates in the result will be rendered. Defaults to UTC.

    • ‘sort_by’<~String>: Used to specify both the field you wish to sort the results on, as well as the direction See API doc for examples.

Returns

  • response<~Array>:

    • <~Pagerduty::Incident>:

      • ‘id’<~String> - Id of request

      • ‘incident_number’<~String>:

      • ‘created_on’<~String>:

      • ‘status’<~String>:

      • ‘html_url’<~String>:

      • ‘incident_key’<~String>:

      • ‘service’<~Pagerduty::Service>

        • ‘id’<~String>:

        • ‘name’<~String>:

        • ‘html_url’<~String>:

        • ‘deleted_at’<~String>:

      • ‘escalation_policy’<~String>:

      • ‘assigned_to_user’<~String>:

      • ‘trigger_summary_data’<~String>:

      • ‘trigger_details_html_url’<~String>:

      • ‘trigger_type’<~String>:

      • ‘last_status_change_on’<~String>:

      • ‘last_status_change_by’<~Pagerduty::User>:

        • ‘id’<~String>:

        • ‘name’<~String>:

        • ‘email’<~String>:

        • ‘html_url’<~String>:

      • ‘number_of_escalations’<~Integer>:

      • ‘resolved_by_user’<~Pagerduty::ResolvedByUser>:

        • ‘id’<~String>:

        • ‘name’<~String>:

        • ‘email’<~String>:

        • ‘html_url’<~String>:

Pagerduty API Reference



206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/pagerduty/pagerduty.rb', line 206

def incidents(options={})
  JSON.parse(curl({
    uri: "https://#@@subdomain.pagerduty.com/api/v1/incidents",
    params: {
      since: options[:since] || (Time.now - 1.day).strftime("%Y-%m-%d"),
      :until => options[:until] || (Time.now + 1.day).strftime("%Y-%m-%d"),
    },
    method: 'GET'
  }).body)['incidents'].inject([]) { |incidents, incident|
    incidents << Incident.new(incident)
  }
end

#incidents_per_time(options = {}) ⇒ Object



284
285
286
287
288
289
290
# File 'lib/pagerduty/pagerduty.rb', line 284

def incidents_per_time(options={})
  Pagerduty::Reports::Incidents.new(JSON.parse(curl({
    uri: "https://#@@subdomain.pagerduty.com/api/v1/reports/incidents_per_time/",
    params: options,
    method: 'GET'
  }).body))
end

#notes(id) ⇒ Object



251
252
253
254
255
256
257
258
# File 'lib/pagerduty/pagerduty.rb', line 251

def notes(id)
  JSON.parse(curl({
    uri: "https://#@@subdomain.pagerduty.com/api/v1/incidents/#{id}/notes",
    method: 'GET'
  }).body)['notes'].inject([]) { |notes, note|
    notes << Note.new(note)
  }
end