Created
December 24, 2013 00:49
-
-
Save bds/8107165 to your computer and use it in GitHub Desktop.
PagerDuty Incidents
This file contains hidden or 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
require 'minitest/spec' | |
require 'minitest/pride' | |
require 'minitest/autorun' | |
require 'net/http' | |
require 'json' | |
describe "PagerDuty::Incident" do | |
before { @incident = PagerDuty::Incident.new } | |
it "must respond to created date" do | |
@incident.must_respond_to :created_on | |
end | |
it "must respond to assigned user" do | |
@incident.must_respond_to :assigned_to_user | |
end | |
it "must respond to status" do | |
@incident.must_respond_to :status | |
end | |
end | |
describe "When asked for a list of the recently created incidents" do | |
before { @incidents = PagerDuty::Incident.recent } | |
it "must be contain exactly 10 Incident objects" do | |
@incidents.count.must_equal 10 | |
@incidents.each { |i| i.must_be_instance_of PagerDuty::Incident } | |
end | |
it "must be sorted in descending date order" do | |
@incidents.size.times do | |
0.upto(@incidents.size-2) do |idx| | |
@incidents[idx].created_on.must_be :>=, @incidents[idx + 1].created_on | |
end | |
end | |
end | |
end | |
module PagerDuty | |
class Incident | |
attr_reader :created_on, :assigned_to_user, :status | |
def initialize(opts={}) | |
@created_on = opts[:created_on] | |
@assigned_to_user = opts[:assigned_to_user] | |
@status = opts[:status] | |
end | |
def self.recent | |
# As the class grows - seperate connection and parsing logic | |
result = [] | |
uri = URI.parse("https://webdemo.pagerduty.com/api/v1/incidents") | |
params = { :sort_by => "created_on:desc", :limit => 10, :offset => 0 } | |
uri.query = URI.encode_www_form(params) | |
Net::HTTP.start(uri.host, uri.port, | |
:use_ssl => uri.scheme == 'https') do |http| | |
request = Net::HTTP::Get.new uri | |
request["Content-Type"] = "application/json" | |
request["Authorization"] = "Token token=YOUR TOKEN GOES HERE" | |
response = http.request request | |
parsed = JSON.parse(response.body) | |
parsed["incidents"].each do |incident| | |
result << new(:created_on => incident["created_on"], | |
:status => incident["status"], | |
:assigned_to_user => (incident["assigned_to_user"]["email"] if incident["assigned_to_user"])) | |
end | |
end | |
result | |
end | |
end | |
end | |
PagerDuty::Incident.recent.each {|i| p i } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment