Class: Xcflushd::Reporter

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

Defined Under Namespace

Classes: ReporterError, ThreeScaleAuthError, ThreeScaleBadParams, ThreeScaleInternalError

Instance Method Summary collapse

Constructor Details

#initialize(threescale_client) ⇒ Reporter

Returns a new instance of Reporter.



43
44
45
# File 'lib/xcflushd/reporter.rb', line 43

def initialize(threescale_client)
  @threescale_client = threescale_client
end

Instance Method Details

#report(service_id, credentials, usage) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/xcflushd/reporter.rb', line 47

def report(service_id, credentials, usage)
  transaction = credentials.creds.merge(usage: usage)

  begin
    resp = threescale_client.report(transactions: [transaction],
                                    service_id: service_id)
  # TODO: get rid of the coupling with ThreeScale::ServerError
  rescue ThreeScale::ServerError, SocketError
    # We'll get a SocketError if there's a timeout when contacting 3scale.
    raise ThreeScaleInternalError.new(service_id, transaction)
  rescue ArgumentError
    raise ThreeScaleBadParams.new(service_id, transaction)
  end

  raise ThreeScaleAuthError.new(service_id, transaction) unless resp.success?
  true
end