class Opbeat::HttpClient

@api private

Constants

USER_AGENT

Attributes

adapter[R]
config[R]
state[R]

Public Class Methods

new(config) click to toggle source
# File lib/opbeat/http_client.rb, line 15
def initialize(config)
  @config = config
  @adapter = HTTPAdapter.new(config)
  @state = ClientState.new config
end

Public Instance Methods

post(resource, body) click to toggle source
# File lib/opbeat/http_client.rb, line 23
def post(resource, body)
  path = abs_path(resource)
  debug "POST #{resource}"

  unless state.should_try?
    info "Temporarily skipping sending to Opbeat due to previous failure."
    return
  end

  if body.is_a?(Hash) || body.is_a?(Array)
    body = JSON.dump(body)
  end

  request = adapter.post path do |req|
    req['Authorization'] = auth_header
    req['Content-Type'] = 'application/json'.freeze
    req['Content-Length'] = body.bytesize.to_s
    req['User-Agent'] = USER_AGENT
    req.body = body
  end

  begin
    response = adapter.perform_request request
    unless response.code.to_i.between?(200, 299)
      raise Error.new("Error from Opbeat server (#{response.code}): #{response.body}")
    end
  rescue
    debug { JSON.parse(body).inspect }
    @state.fail!
    raise
  end

  @state.success!

  response
end

Private Instance Methods

abs_path(path) click to toggle source
# File lib/opbeat/http_client.rb, line 66
def abs_path path
  "/api/v1/organizations/#{@config.organization_id}" +
    "/apps/#{@config.app_id}#{path}"
end
auth_header() click to toggle source
# File lib/opbeat/http_client.rb, line 62
def auth_header
  "Bearer #{@config.secret_token}"
end
encode(event) click to toggle source
# File lib/opbeat/http_client.rb, line 71
def encode(event)
  event_hash = @filter.process_event_hash(event.to_hash)
  event_hash.to_json
end