-
-
Save thegreenrobot/48131186621ff59e7664 to your computer and use it in GitHub Desktop.
PagerDuty Incidents with Faraday
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# encoding: UTF-8 | |
require 'faraday' | |
require 'json' | |
SECRETS_file = './lib/secrets.json' | |
SECRETS_PARSED = JSON.parse( IO.read( SECRETS_file )) | |
URL = SECRETS_PARSED['url'] | |
API_KEY = SECRETS_PARSED['api_key'] | |
@total_incident_count = 0 | |
@incidents = [] | |
def query_pagerduty(offset) | |
conn = Faraday.new(:url => "#{URL}") do |faraday| | |
faraday.request :url_encoded | |
faraday.adapter Faraday.default_adapter | |
faraday.headers['Content-type'] = 'application/json' | |
faraday.headers['Authorization'] = "Token token=#{API_KEY}" | |
faraday.params['since'] = '2014/01/01' | |
faraday.params['until'] = '2014/02/01' | |
faraday.params['offset'] = offset | |
faraday.params['limit'] = 100 | |
faraday.params['status'] = 'resolved' | |
end | |
begin | |
response = conn.get '/api/v1/incidents/' | |
data = JSON.parse(response.body) | |
@total_incident_count = data['total'] | |
data['incidents'].each do |incident| | |
@incidents.push(incident) | |
end | |
rescue | |
puts 'Awww snap. The response from the PagerDuty API was not successful.' | |
exit | |
end | |
end | |
while @incidents.length <= @total_incident_count do | |
# We're just starting out, first query to PagerDuty | |
if @incidents.length == 0 | |
offset = 0 | |
data = query_pagerduty(offset) | |
# We have all of the incidents, stop querying | |
elsif @incidents.length == @total_incident_count | |
break | |
# We don't have all of the incidents, increment the offset and query again | |
else | |
offset += 100 | |
data = query_pagerduty(offset) | |
end | |
end | |
# @incidents.each do |incident| | |
# puts incident | |
# end | |
puts "Total incidents: #{@incidents.length}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"url": "https://mysubdomain.pagerduty.com", | |
"api_key": "MYREALLYLONGAPIKEY", | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment