Class: Adzerk::Reporting
- Inherits:
-
Object
- Object
- Adzerk::Reporting
- Includes:
- Util
- Defined in:
- lib/adzerk/reporting.rb
Constant Summary collapse
- REPORT_STATUS =
{ success: 2 }
Instance Attribute Summary collapse
-
#client ⇒ Object
Returns the value of attribute client.
Instance Method Summary collapse
- #create_queued_report(data = {}) ⇒ Object
-
#create_report(data = {}, timeout = nil) ⇒ Object
Queues a report and then polls for it using exponential backoff, blocking until the report is either retrieved or (optionally) if a timeout is exceeded.
-
#initialize(args = {}) ⇒ Reporting
constructor
A new instance of Reporting.
- #retrieve_queued_report(id) ⇒ Object
Methods included from Util
#camelize_data, #parse_response, #uncamelize_data
Constructor Details
#initialize(args = {}) ⇒ Reporting
Returns a new instance of Reporting.
11 12 13 |
# File 'lib/adzerk/reporting.rb', line 11 def initialize(args = {}) @client = args.fetch(:client) end |
Instance Attribute Details
#client ⇒ Object
Returns the value of attribute client.
5 6 7 |
# File 'lib/adzerk/reporting.rb', line 5 def client @client end |
Instance Method Details
#create_queued_report(data = {}) ⇒ Object
55 56 57 58 |
# File 'lib/adzerk/reporting.rb', line 55 def create_queued_report(data={}) data = { 'criteria' => camelize_data(data).to_json } parse_response(client.post_request('report/queue', data)) end |
#create_report(data = {}, timeout = nil) ⇒ Object
Queues a report and then polls for it using exponential backoff, blocking until the report is either retrieved or (optionally) if a timeout is exceeded.
The timeout is a total number of milliseconds to wait before giving up and raising an error.
If no timeout is provided, this function will continue to poll forever with exponential backoff.
Returns the parsed report JSON when a report is retrieved.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/adzerk/reporting.rb', line 26 def create_report(data={}, timeout=nil) # Queue the report. queue_response = create_queued_report(data) # Ensure it contains a report ID. report_id = queue_response[:id] raise "Unexpected response: #{queue_response}" unless report_id # Try to get the report indefinitely or up until the timeout, with # exponential backoff. time_to_stop = Time.now + (timeout / 1000.0) unless timeout.nil? retries = 0 while timeout.nil? or Time.now < time_to_stop poll_result = retrieve_queued_report(report_id) if poll_result[:status] == REPORT_STATUS[:success] return poll_result[:result] end poll_interval = (2 ** retries) * 100 sleep poll_interval / 1000.0 retries += 1 end raise "Failed to retrieve report within #{timeout} ms." end |
#retrieve_queued_report(id) ⇒ Object
60 61 62 63 |
# File 'lib/adzerk/reporting.rb', line 60 def retrieve_queued_report(id) url = 'report/queue/' + id parse_response(client.get_request(url)) end |