Class: OopsGenie::OopsGenieClient
- Inherits:
-
Object
- Object
- OopsGenie::OopsGenieClient
- Defined in:
- lib/oops_genie/oops_genie_client.rb
Overview
Implement sending an alert to OpsGenie
Instance Method Summary collapse
- #fetch_json(alert_config) ⇒ Object
-
#initialize(api_key) ⇒ OopsGenieClient
constructor
A new instance of OopsGenieClient.
- #send_alert(alert_config) ⇒ Object
Constructor Details
#initialize(api_key) ⇒ OopsGenieClient
Returns a new instance of OopsGenieClient.
11 12 13 14 15 |
# File 'lib/oops_genie/oops_genie_client.rb', line 11 def initialize(api_key) @api_key = api_key # TODO: dondeng - Do not hard code this @url_prefix = 'https://api.eu.opsgenie.com/v2/alerts' end |
Instance Method Details
#fetch_json(alert_config) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/oops_genie/oops_genie_client.rb', line 17 def fetch_json(alert_config) uri = URI(@url_prefix) https = Net::HTTP.new(uri.host, uri.port) https.use_ssl = true request = Net::HTTP::Post.new(uri.request_uri, { 'Content-Type' => 'application/json', 'Authorization' => "GenieKey #{@api_key}" }) request.body = alert_config.alert_hash.to_json res = https.request(request) return JSON.parse(res.body) if res.is_a?(Net::HTTPSuccess) false rescue StandardError => e puts "Error in oops_genie gem: #{e.}" false end |
#send_alert(alert_config) ⇒ Object
35 36 37 38 |
# File 'lib/oops_genie/oops_genie_client.rb', line 35 def send_alert(alert_config) response = fetch_json(alert_config) response == false ? 'Alert could not be generated.' : 'Alert generated.' end |