Class: Pagerduty
- Inherits:
-
Object
- Object
- Pagerduty
- 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
Direct Known Subclasses
EscalationPolicy, EscalationRule, Incident, MaintenanceWindow, User
Defined Under Namespace
Classes: LogEntry, MaintenanceWindow, MaintenanceWindows, Reports, ScheduleInfo, Schedules, Services, User
Instance Attribute Summary collapse
-
#subdomain ⇒ Object
readonly
Returns the value of attribute subdomain.
-
#token ⇒ Object
readonly
Returns the value of attribute token.
Instance Method Summary collapse
- #alerts(options = {}) ⇒ Object
- #alerts_per_time(options = {}) ⇒ Object
- #create_escalation_policy(options = {}) ⇒ Object
- #create_maintenance_window(options = {}) ⇒ Object
- #create_service(options = {}) ⇒ Object
- #create_user(options = {}) ⇒ Object
-
#curl(options) ⇒ Object
def curl.
- #escalation_policies(options = {}) ⇒ Object
- #escalation_rules(options) ⇒ Object
- #get_escalation_policy(options = {}) ⇒ Object
- #get_escalation_rule(options = {}) ⇒ Object
- #get_incident(options = {}) ⇒ Object
- #get_incident_counts(options = {}) ⇒ Object
- #get_log_entries(options = {}) ⇒ Object
- #get_log_entry(options = {}) ⇒ Object
- #get_maintenance_window(options = {}) ⇒ Object
- #get_maintenance_windows(options = {}) ⇒ Object
- #get_schedule(options = {}) ⇒ Object
- #get_schedule_users(options = {}) ⇒ Object
- #get_schedules(options = {}) ⇒ Object
- #get_service(options = {}) ⇒ Object
- #get_services(options = {}) ⇒ Object
- #get_user(options = {}) ⇒ Object
- #get_users(options = {}) ⇒ Object
- #has_requirements?(keys, options) ⇒ Boolean
-
#incidents(options = {}) ⇒ Object
Retrieve all incidents.
- #incidents_per_time(options = {}) ⇒ Object
-
#initialize(options) ⇒ Pagerduty
constructor
A new instance of Pagerduty.
- #notes(id) ⇒ Object
Constructor Details
#initialize(options) ⇒ Pagerduty
Returns a new instance of Pagerduty.
7 8 9 10 |
# File 'lib/pagerduty/pagerduty.rb', line 7 def initialize() @@token = [:token] @@subdomain = [:subdomain] end |
Instance Attribute Details
#subdomain ⇒ Object (readonly)
Returns the value of attribute subdomain.
5 6 7 |
# File 'lib/pagerduty/pagerduty.rb', line 5 def subdomain @subdomain end |
#token ⇒ Object (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(={}) unless has_requirements? [:since, :until], 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: , 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(={}) Pagerduty::Reports::Alerts.new(JSON.parse(curl({ uri: "https://#@@subdomain.pagerduty.com/api/v1/reports/alerts_per_time", params: , 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(={}) if [:escalation_rules] [:escalation_rules] = [: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: , 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(={}) Pagerduty::MaintenanceWindow.new(JSON.parse(curl({ uri: "https://#@@subdomain.pagerduty.com/api/v1/maintenance_windows", data: , 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(={}) Pagerduty::Services::Objects::Service.new(JSON.parse(curl({ uri: "https://#@@subdomain.pagerduty.com/api/v1/services", data: { service: }, 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(={}) User.new(JSON.parse(curl({ uri: "https://#@@subdomain.pagerduty.com/api/v1/users", data: , 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() curl_request = { ssl: true, port: 443, headers: { "Content-Type" => "application/json", "Authorization" => "Token token=#@@token", }, } .merge! curl_request url = URI.parse([:uri]) if [:params] parameters = [:params].map { |k,v| "#{k}=#{v}" }.join("&") url += "?#{parameters}" end http = Net::HTTP.new(url.host, 443) http.use_ssl = true request = case [: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 .has_key?(:data) request.set_form_data([:data]) end if .has_key?(:basic_auth) request.basic_auth [:basic_auth][:user], [:basic_auth][:password] end request.body = [:body] [:headers].each { |key,val| request.add_field(key,val) } if [:method] == 'POST' http.post(url.path,[:data].to_json,[:headers]) elsif [:method] == 'PUT' http.put(url.path,[:data].to_json,[: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(={}) JSON.parse(curl({ uri: "https://#@@subdomain.pagerduty.com/api/v1/escalation_policies", params: { 'query' => [: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() JSON.parse(curl({ uri: "https://#@@subdomain.pagerduty.com/api/v1/escalation_policies/#{[:escalation_policy_id]}/escalation_rules", params: { 'query' => [: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(={}) EscalationPolicy.new(JSON.parse(curl({ uri: "https://#@@subdomain.pagerduty.com/api/v1/escalation_policies/#{[: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(={}) JSON.parse(curl({ uri: "https://#@@subdomain.pagerduty.com/api/v1/escalation_policies/#{[:escalation_policy_id]}/escalation_rules/#{[:rule_id]}", method: 'GET' }).body) end |
#get_incident(options = {}) ⇒ Object
219 220 221 |
# File 'lib/pagerduty/pagerduty.rb', line 219 def get_incident(={}) incidents.detect { |incident| incident.id == [: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(={}) JSON.parse(curl({ uri: "https://#@@subdomain.pagerduty.com/api/v1/incidents/count", params: , 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(={}) LogEntries.new(JSON.parse(curl({ uri: "https://#@@subdomain.pagerduty.com/api/v1/log_entries", params: , 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(={}) LogEntry.new(JSON.parse(curl({ uri: "https://#@@subdomain.pagerduty.com/api/v1/log_entries/#{[:id]}", params: , 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(={}) Pagerduty::MaintenanceWindow.new(JSON.parse(curl({ uri: "https://#@@subdomain.pagerduty.com/api/v1/maintenance_windows/#{[:id]}", params: , 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(={}) Pagerduty::MaintenanceWindows.new(JSON.parse(curl({ uri: "https://#@@subdomain.pagerduty.com/api/v1/maintenance_windows", params: , 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(={}) Pagerduty::ScheduleInfo.new(JSON.parse(curl({ uri: "https://#@@subdomain.pagerduty.com/api/v1/schedules/#{[:id]}", params: , 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(={}) JSON.parse(curl({ uri: "https://#@@subdomain.pagerduty.com/api/v1/schedules/#{[:id]}/users", params: , 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(={}) Pagerduty::Schedules.new(JSON.parse(curl({ uri: "https://#@@subdomain.pagerduty.com/api/v1/schedules", params: , 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(={}) Pagerduty::Services::Objects::Service.new(JSON.parse(curl({ uri: "https://#@@subdomain.pagerduty.com/api/v1/services/#{[:id]}", params: , 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(={}) Pagerduty::Services.new(JSON.parse(curl({ uri: "https://#@@subdomain.pagerduty.com/api/v1/services", params: , method: 'GET' }).body)) end |
#get_user(options = {}) ⇒ Object
239 240 241 |
# File 'lib/pagerduty/pagerduty.rb', line 239 def get_user(={}) get_users.users.detect { |user| user.id == [:id] } end |
#get_users(options = {}) ⇒ Object
231 232 233 234 235 236 237 |
# File 'lib/pagerduty/pagerduty.rb', line 231 def get_users(={}) Users.new(JSON.parse(curl({ uri: "https://#@@subdomain.pagerduty.com/api/v1/users", params: { query: [:query] }, method: 'GET' }).body)) end |
#has_requirements?(keys, options) ⇒ Boolean
85 86 87 |
# File 'lib/pagerduty/pagerduty.rb', line 85 def has_requirements?(keys,) (keys - .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>:
-
-
-
206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/pagerduty/pagerduty.rb', line 206 def incidents(={}) JSON.parse(curl({ uri: "https://#@@subdomain.pagerduty.com/api/v1/incidents", params: { since: [:since] || (Time.now - 1.day).strftime("%Y-%m-%d"), :until => [: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(={}) Pagerduty::Reports::Incidents.new(JSON.parse(curl({ uri: "https://#@@subdomain.pagerduty.com/api/v1/reports/incidents_per_time/", params: , 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 |