Skip to content
This repository has been archived by the owner on Dec 11, 2022. It is now read-only.

Add StatusPage.io functionality. #4

Merged
merged 6 commits into from Jul 26, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 32 additions & 0 deletions README.org
Expand Up @@ -2,6 +2,7 @@
- Pagerduty support (integration, incidents)
- Pingdom support (checks, results, actions)
- Loggly support (event, search, facets)
- StatusPage support (incidents, components)
[[https://github.com/portertech/redphone/raw/master/redphone.jpg]]
- All API request are done over SSL
- Response bodies are returned as Ruby hashes
Expand Down Expand Up @@ -75,5 +76,36 @@
You can add the following options for a date range
: :from => "2011-08-01T12:00:00Z",
: :until => "2011-10-01T12:00:00Z"
** StatusPage.io
: require "redphone/statuspage"
: statuspage = Redphone::Statuspage.new(
: :page_id => PAGE_ID,
: :api_key => API_KEY
: )
Create a realtime incident
: response = statuspage.create_realtime_incident(
: :name => "testing",
: :status => "identified",
: :wants_twitter_update => "t",
: :message => "testing message"
: )
Update an incident
: incident_id = response["id"]
: statuspage.update_incident(
: :name => "testing",
: :status => "resolved",
: :wants_twitter_update => "t"
: :incident_id => incident_id
: )
Delete an incident
: statuspage.delete_incident(
: :incident_id => incident_id
: )
Update a component status
: statuspage.update_component(
: :component_id => COMPONENT_ID,
: :status => "major_outage"
: )
* Contributors
- [[http://portertech.ca][Sean Porter]]
- [[http://github.com/perryh][Perry Huang]]
2 changes: 2 additions & 0 deletions lib/redphone/helpers.rb
Expand Up @@ -33,6 +33,8 @@ def http_request(options={})
Net::HTTP::Put.new(request_uri)
when "delete"
Net::HTTP::Delete.new(request_uri)
when "patch"
Net::HTTP::Patch.new(request_uri)
else
raise "Unknown HTTP method: #{method}"
end
Expand Down
142 changes: 142 additions & 0 deletions lib/redphone/statuspage.rb
@@ -0,0 +1,142 @@
require File.join(File.dirname(__FILE__), 'helpers')

module Redphone
class Statuspage
def initialize(options={})
has_options(options, [:api_key, :page_id])
@page_id = options[:page_id]
@request_options = {
:ssl => true,
:headers => {"Authorization" => "OAuth #{options[:api_key]}"}
}
@api_url = "https://api.statuspage.io/v1/pages/#{@page_id}/"
end

def convert_options(options={})
incident_options = Hash.new
options.each do |key, val|
incident_options["incident[#{key}]"] = val
end
return incident_options
end

def check_attributes(options={}, checked_options=[])
checked_options.each do |option|
raise "You must supply an incident #{option}." if options[option].nil?
end
end

def get_all_incidents()
response = http_request(
@request_options.merge({
:method => "get",
:uri => @api_url + "incidents.json"
})
)
JSON.parse(response.body)
end

def create_realtime_incident(options={})
check_attributes(options, [:name, :wants_twitter_update])
options = convert_options(options)
response = http_request(
@request_options.merge({
:method => "post",
:uri => @api_url + "incidents.json",
:parameters => options
})
)
JSON.parse(response.body)
end

def create_scheduled_incident(options={})
check_attributes(options, [:name, :status, :wants_twitter_update, :scheduled_for, :scheduled_until])
options = convert_options(options)
response = http_request(
@request_options.merge({
:method => "post",
:uri => @api_url + "incidents.json",
:parameters => options
})
)
JSON.parse(response.body)
end

def create_historical_incident(options={})
check_attributes(options, [:name, :message, :backfilled, :backfill_date])
options = convert_options(options)
response = http_request(
@request_options.merge({
:method => "post",
:uri => @api_url + "incidents.json",
:parameters => options
})
)
JSON.parse(response.body)
end

def update_incident(options={})
check_attributes(options, [:name, :wants_twitter_update, :incident_id])
incident_id = options[:incident_id]
options.delete(:incident_id)
options = convert_options(options)
response = http_request(
@request_options.merge({
:method => "patch",
:uri => @api_url + "incidents/#{incident_id}.json",
:parameters => options
})
)
JSON.parse(response.body)
end

def delete_incident(options={})
check_attributes(options, [:incident_id])
response = http_request(
@request_options.merge({
:method => "delete",
:uri => @api_url + "incidents/#{options[:incident_id]}.json"
})
)
JSON.parse(response.body)
end

def tune_incident_update(options={})
check_attributes(options, [:incident_id, :incident_update_id])
parameter_options = Hash.new
parameter_options["incident_update[body]"] = options[:body] unless options[:body].nil?
parameter_options["incident_update[display_at]"] = options[:display_at] unless options[:display_at].nil?
response = http_request(
@request_options.merge({
:method => "patch",
:uri => @api_url + "incidents/#{options[:incident_id]}/incident_updates/#{options[:incident_update_id]}.json",
:parameters => parameter_options
})
)
JSON.parse(response.body)
end

def get_all_components()
response = http_request(
@request_options.merge({
:method => "get",
:uri => @api_url + "components.json"
})
)
JSON.parse(response.body)
end

def update_component(options={})
check_attributes(options, [:component_id, :status])
parameter_options = {"component[status]" => options[:status]}
response = http_request(
@request_options.merge({
:method => "patch",
:uri => @api_url + "components/#{options[:component_id]}.json",
:parameters => parameter_options
})
)
JSON.parse(response.body)
end
end
end
114 changes: 114 additions & 0 deletions test/statuspage_test.rb
@@ -0,0 +1,114 @@
$: << File.dirname(__FILE__) + '/../lib' unless $:.include?(File.dirname(__FILE__) + '/../lib/')
require 'rubygems' if RUBY_VERSION < '1.9.0'
gem 'minitest'
require 'minitest/autorun'
require 'redphone/statuspage'

STATUSPAGE_PAGE_ID = "YOURPAGEID"
STATUSPAGE_API_KEY = "YOURAPIKEY"

class TestRedphoneStatuspage < MiniTest::Unit::TestCase
def setup
@statuspage = Redphone::Statuspage.new(
:page_id => STATUSPAGE_PAGE_ID,
:api_key => STATUSPAGE_API_KEY
)
end

def test_create_realtime_incident
response = @statuspage.create_realtime_incident(
:name => "testing",
:status => "identified",
:wants_twitter_update => "f",
:message => "testing message"
)
assert_equal 'identified', response['status']
end

def test_create_scheduled_incident
response = @statuspage.create_scheduled_incident(
:name => "testing scheduled",
:status => "scheduled",
:scheduled_for => "2014-04-22T01:00:00Z",
:scheduled_until => "3000-04-22T03:00:00Z",
:wants_twitter_update => "f",
:messsage => "testing message"
)
assert_equal 'scheduled', response['status']
end

def test_create_historical_incident
response = @statuspage.create_historical_incident(
:name => "testing historical",
:message => "testing message",
:backfilled => "t",
:backfill_date => "2013-04-01"
)
assert_equal 'resolved', response['status']
end

def test_update_incident
response = @statuspage.create_realtime_incident(
:name => "testing",
:status => "identified",
:wants_twitter_update => "f",
:message => "testing message"
)
incident_id = response["id"]
response = @statuspage.update_incident(
:name => "testing",
:status => "resolved",
:wants_twitter_update => "f",
:incident_id => incident_id
)
assert_equal 'resolved', response['status']
end

def test_delete_incident
response = @statuspage.create_realtime_incident(
:name => "this incident will be removed.",
:status => "investigating",
:wants_twitter_update => "f",
:message => "testing message"
)
incident_status = response["status"]
incident_id = response["id"]
response = @statuspage.delete_incident(
:incident_id => incident_id
)
assert_equal incident_status, response['status']
end

def test_tune_update_incident
response = @statuspage.create_realtime_incident(
:name => "this incident will be updated",
:status => "investigating",
:wants_twitter_update => "f",
:message => "testing message"
)
incident_id = response["id"]
response = @statuspage.update_incident(
:name => "this update will be tuned later",
:status => "resolved",
:wants_twitter_update => "f",
:incident_id => incident_id
)
incident_update_id = response["incident_updates"].first["id"]
response = @statuspage.tune_incident_update(
:incident_id => incident_id,
:incident_update_id => incident_update_id,
:body => "updated incident new body",
)
assert_equal 'updated incident new body', response['body']
end

def test_update_component
test_component_id = @statuspage.get_all_components.first["id"]

response = @statuspage.update_component(
:component_id => test_component_id,
:status => "major_outage"
)
assert_equal 'major_outage', response['status']
end
end