Class: Errorgap::Notifier

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

Defined Under Namespace

Classes: Response

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ Notifier

Returns a new instance of Notifier.



15
16
17
# File 'lib/errorgap/notifier.rb', line 15

def initialize(configuration)
  configure(configuration)
end

Instance Method Details

#configure(configuration) ⇒ Object



19
20
21
# File 'lib/errorgap/notifier.rb', line 19

def configure(configuration)
  @configuration = configuration
end

#deliver(notice) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/errorgap/notifier.rb', line 45

def deliver(notice)
  uri = URI.join(@configuration.endpoint.end_with?("/") ? @configuration.endpoint : "#{@configuration.endpoint}/", "api/projects/#{@configuration.project_slug}/notices")
  request = Net::HTTP::Post.new(uri)
  request["Content-Type"] = "application/json"
  request["User-Agent"] = "errorgap-ruby/#{Errorgap::VERSION}"
  request["X-Errorgap-Project-Key"] = @configuration.api_key if present?(@configuration.api_key)
  request.body = JSON.generate(notice.to_h)

  response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == "https") do |http|
    http.request(request)
  end
  Response.new(status: response.code.to_i, body: response.body)
rescue StandardError => exception
  log(exception)
  Response.new(error: exception)
end

#notify(error, context: {}, environment: {}, session: {}, params: {}, sync: false) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/errorgap/notifier.rb', line 23

def notify(error, context: {}, environment: {}, session: {}, params: {}, sync: false)
  @configuration.validate!
  notice = Notice.from_exception(
    error,
    configuration: @configuration,
    context: context,
    environment: environment,
    session: session,
    params: params
  )

  if sync || !@configuration.async
    deliver(notice)
  else
    Thread.new { deliver(notice) }
    Response.new(status: 202, body: "queued")
  end
rescue StandardError => exception
  log(exception)
  Response.new(error: exception)
end