Module: Buyerquest::Opsgenie::Client

Defined in:
lib/buyerquest/opsgenie/client.rb

Constant Summary collapse

OPSGENIE_API_BASE_URL =
'https://api.opsgenie.com/v2'.freeze

Class Method Summary collapse

Class Method Details

.execute(opsgenie_request) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/buyerquest/opsgenie/client.rb', line 12

def self.execute(opsgenie_request)
  require 'addressable/uri'
  uri = Addressable::URI.escape(opsgenie_request[:uri])
  method = opsgenie_request[:method].upcase
  payload = opsgenie_request[:payload]
  raise "[OpsGenie Error] Unknown method" unless %w(GET POST PUT PATCH DELETE).include? method
  request = {
      :method => method,
      :url => "#{OPSGENIE_API_BASE_URL}#{uri}",
      :headers => get_headers(method)
  }
  request[:payload] = payload.to_json unless payload.nil?
  begin
    response = RestClient::Request.execute(request)
  rescue RestClient::NotFound => err
    return nil
  rescue StandardError => err
    puts "[OpsGenie Error] Unhandled response error"
    puts "Request payload:"
    puts request[:payload]
    raise err
  end
  JSON.parse(response)
end

.get_headers(http_method) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/buyerquest/opsgenie/client.rb', line 37

def self.get_headers(http_method)
  headers = {:Authorization => "GenieKey #{@api_key}"}
  if %w(POST PATCH).include? http_method.upcase
    headers[:content_type] = 'application/json'
  end
  headers
end

.init(api_key = nil, opts = {}) ⇒ Object



6
7
8
9
10
# File 'lib/buyerquest/opsgenie/client.rb', line 6

def self.init(api_key = nil, opts={})
  @api_key = api_key
  @api_key ||= ENV['OPSG_API_KEY']
  raise "[OpsGenie Error] No api key provided" if @api_key.nil?
end