Class: Bugno::Reporter

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeReporter

Returns a new instance of Reporter.



12
13
14
15
16
# File 'lib/bugno/reporter.rb', line 12

def initialize
  @uri = URI.parse("#{Bugno.configuration.api_url}/api/v1/projects/#{Bugno.configuration.api_key}/events")
  @http = Net::HTTP.new(uri.host, uri.port)
  @request = Net::HTTP::Post.new(uri.request_uri, 'Content-Type': 'application/json')
end

Instance Attribute Details

#httpObject (readonly)

Returns the value of attribute http.



9
10
11
# File 'lib/bugno/reporter.rb', line 9

def http
  @http
end

#requestObject

Returns the value of attribute request.



10
11
12
# File 'lib/bugno/reporter.rb', line 10

def request
  @request
end

#uriObject (readonly)

Returns the value of attribute uri.



9
10
11
# File 'lib/bugno/reporter.rb', line 9

def uri
  @uri
end

Instance Method Details

#api_response(response) ⇒ Object



27
28
29
30
31
# File 'lib/bugno/reporter.rb', line 27

def api_response(response)
  body = JSON.parse(response.body.presence || '{}')
  message = body['message'] || body['error'] || response.message
  "#{message.capitalize} | Code: #{response.code}"
end

#sendObject



18
19
20
21
22
23
24
25
# File 'lib/bugno/reporter.rb', line 18

def send
  http.use_ssl = true if uri.scheme == 'https'

  response = http.request(request)
  Bugno.log_info(api_response(response))
rescue StandardError => e
  Bugno.log_error(e.message)
end