Skip to content

Instantly share code, notes, and snippets.

@lfepp
Created July 14, 2016 15:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lfepp/f337f064e86ce39e88b7b98a91e4a5b5 to your computer and use it in GitHub Desktop.
Save lfepp/f337f064e86ce39e88b7b98a91e4a5b5 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'pp'
require 'httparty'
# Class to GET or POST to the PagerDuty REST API
class PagerDuty
include HTTParty
format :json
def initialize(api_token)
@options = {
:headers => {
'Authorization' => "Token token=#{api_token}",
'Content-type' => 'application/json',
'Accept' => 'application/vnd.pagerduty+json;version=2'
},
:output => 'json'
}
end
def get(req, opts = {})
opts = opts.merge(@options)
self.class.get("https://api.pagerduty.com/#{req}", opts)
end
def post(req, opts = {})
opts = opts.merge(@options)
self.class.post("https://api.pagerduty.com/#{req}", opts)
end
end
# This is a read-only API key:
webdemo = PagerDuty.new('3Zy8bTHN1fDJq5vBW-X7')
# Get incidents:
puts webdemo.get('incidents', :query => 'limit=1')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment