Class: Opsgenie

Inherits:
Object
  • Object
show all
Defined in:
lib/opsgenie.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key) ⇒ Opsgenie

Returns a new instance of Opsgenie.



10
11
12
13
# File 'lib/opsgenie.rb', line 10

def initialize(api_key)
  @api_key = api_key
  @url_prefix = 'https://api.opsgenie.com/v1/json/alert'
end

Class Method Details

.fetch_json(uri, params, req_type) ⇒ Object



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

def self.fetch_json(uri, params, req_type)
  uri = URI(uri)
  https = Net::HTTP.new(uri.host, uri.port)
  https.use_ssl = true
  begin
    if req_type=='post'
      res = https.post(uri.path, params) 
    else
      uri.query = URI.encode_www_form(params) if params
      res = Net::HTTP.get_response(uri)
    end
    if res.is_a?(Net::HTTPSuccess)
      return JSON.parse(res.body)
    else
      return false
    end
  rescue
    return false
  end
  
end

Instance Method Details

#send_alert(message) ⇒ Object



37
38
39
40
41
# File 'lib/opsgenie.rb', line 37

def send_alert message
  params = "{ \"apiKey\":\"#{@api_key}\", \"message\":\"#{message}\" }"
  response =  Opsgenie.fetch_json(@url_prefix, params, 'post')
  return response==false ? 'Alert could not be generated.':'Alert generated.'
end

#test_alertObject



43
44
45
46
47
48
# File 'lib/opsgenie.rb', line 43

def test_alert
  puts "Testing alert service."
  params = "{ \"apiKey\":\"#{@api_key}\", \"message\":\"testalert\" }"
  response = Opsgenie.fetch_json(@url_prefix, params, 'post')
  return response==false ? 'Alert could not be generated.':'Alert generated.'
end