Class: ExceptionDog::HttpNotifier

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ HttpNotifier

Returns a new instance of HttpNotifier.



12
13
14
15
# File 'lib/exception_dog/http_notifier.rb', line 12

def initialize(configuration)
  @configuration = configuration
  @logger = configuration.logger
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



9
10
11
# File 'lib/exception_dog/http_notifier.rb', line 9

def configuration
  @configuration
end

#loggerObject (readonly)

Returns the value of attribute logger.



10
11
12
# File 'lib/exception_dog/http_notifier.rb', line 10

def logger
  @logger
end

Instance Method Details

#errorsObject



37
38
39
40
# File 'lib/exception_dog/http_notifier.rb', line 37

def errors
  @errors  = []
  @errors << "Invalid API Key" unless configuration.api_key && configuration.api_key =~ /[0-9a-f]{32}/i
end

#notify(title, text, opts) ⇒ Object



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

def notify(title, text, opts)
  uri = URI.parse("https://api.datadoghq.com/api/v1/events?api_key=#{configuration.api_key}")
  logger = configuration.logger
  header = {'Content-Type': 'application/json'}
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  request = Net::HTTP::Post.new(uri.request_uri, header)
  request.body = {title: title, text: text}.merge(opts).to_json
  logger.info "ExceptionDog::send_to_api"
  begin
    response = http.request(request)
    logger.debug response.body if response.respond_to?(:body)
    logger.debug "ExceptionDog:Response: #{response.inspect}"
  rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError,
           Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e
    logger.error("ExceptionDog::Failed to send to datadog")
    logger.error(e)
  end
end