Class: RaceGuard::Reporters::WebhookReporter

Inherits:
Object
  • Object
show all
Defined in:
lib/race_guard/reporters/webhook_reporter.rb

Overview

POSTs one JSON event per call to a URL. Network errors are swallowed. Pass http_request: for tests: ->(request_uri, body) { … }.

Instance Method Summary collapse

Constructor Details

#initialize(url, open_timeout: 2, read_timeout: 2, http_request: nil) ⇒ WebhookReporter

Returns a new instance of WebhookReporter.



12
13
14
15
16
17
# File 'lib/race_guard/reporters/webhook_reporter.rb', line 12

def initialize(url, open_timeout: 2, read_timeout: 2, http_request: nil)
  @url = url
  @open_timeout = open_timeout
  @read_timeout = read_timeout
  @http_request = http_request
end

Instance Method Details

#report(event) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/race_guard/reporters/webhook_reporter.rb', line 19

def report(event)
  body = JSON.generate(event.to_h)
  uri = URI(@url)
  if @http_request
    @http_request.call(uri, body)
  else
    post_json(uri, body)
  end
rescue StandardError
  nil
end