Sha256: 4387b5fec88be99277832e8d5b4c230428a08369e3a4ad3e9771af1af931d2c1

Contents?: true

Size: 1.67 KB

Versions: 1

Compression:

Stored size: 1.67 KB

Contents

require 'pagerduty/version'
require 'pagerduty/http_transport'

class PagerdutyException < Exception
  attr_reader :pagerduty_instance, :api_response

  def initialize(instance, resp)
    @pagerduty_instance = instance
    @api_response = resp
  end
end

class Pagerduty

  attr_reader :service_key, :incident_key

  def initialize(service_key, incident_key = nil)
    @service_key = service_key
    @incident_key = incident_key
  end

  def trigger(description, details = {})
    resp = api_call("trigger", description, details)
    raise PagerdutyException.new(self, resp) unless resp["status"] == "success"

    PagerdutyIncident.new @service_key, resp["incident_key"]
  end

  def get_incident(incident_key)
    PagerdutyIncident.new @service_key, incident_key
  end

protected

  def api_call(event_type, description, details = {})
    params = {
      :event_type => event_type,
      :service_key => @service_key,
      :description => description,
      :details => details
    }
    params[:incident_key] = @incident_key if @incident_key
    Pagerduty.transport.send(params)
  end

  class << self
    def transport
      Pagerduty::HttpTransport
    end
  end
end

class PagerdutyIncident < Pagerduty

  def initialize(service_key, incident_key)
    super service_key
    @incident_key = incident_key
  end

  def acknowledge(description, details = {})
    resp = api_call("acknowledge", description, details)
    raise PagerdutyException.new(self, resp) unless resp["status"] == "success"

    self
  end

  def resolve(description, details = {})
    resp = api_call("resolve", description, details)
    raise PagerdutyException.new(self, resp) unless resp["status"] == "success"

    self
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pagerduty-1.4.1 lib/pagerduty.rb