Class: Clearbit::Analytics::Request
- Inherits:
-
Object
- Object
- Clearbit::Analytics::Request
- Includes:
- Defaults::Request, Logging, Utils
- Defined in:
- lib/clearbit/analytics/request.rb
Constant Summary
Constants included from Utils
Utils::UTC_OFFSET_WITHOUT_COLON, Utils::UTC_OFFSET_WITH_COLON
Constants included from Defaults::Request
Defaults::Request::HEADERS, Defaults::Request::HOST, Defaults::Request::PATH, Defaults::Request::PORT, Defaults::Request::RETRIES, Defaults::Request::SSL
Class Attribute Summary collapse
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Request
constructor
public: Creates a new request object to send analytics batch.
-
#post(write_key, batch) ⇒ Object
public: Posts the write key and batch of messages to the API.
Methods included from Logging
Methods included from Utils
#date_in_iso8601, #datetime_in_iso8601, #formatted_offset, #isoify_dates, #isoify_dates!, #seconds_to_utc_offset, #stringify_keys, #symbolize_keys, #symbolize_keys!, #time_in_iso8601, #uid
Constructor Details
#initialize(options = {}) ⇒ Request
public: Creates a new request object to send analytics batch
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/clearbit/analytics/request.rb', line 19 def initialize( = {}) [:host] ||= HOST [:port] ||= PORT [:ssl] ||= SSL @headers = [:headers] || HEADERS @path = [:path] || PATH @retries = [:retries] || RETRIES @backoff_policy = [:backoff_policy] || Clearbit::Analytics::BackoffPolicy.new http = Net::HTTP.new([:host], [:port]) http.use_ssl = [:ssl] http.read_timeout = 8 http.open_timeout = 4 @http = http end |
Class Attribute Details
.stub ⇒ Object
128 129 130 |
# File 'lib/clearbit/analytics/request.rb', line 128 def stub @stub || ENV['STUB'] end |
Instance Method Details
#post(write_key, batch) ⇒ Object
public: Posts the write key and batch of messages to the API.
returns - Response of the status and error if it exists
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/clearbit/analytics/request.rb', line 40 def post(write_key, batch) logger.debug("Sending request for #{batch.length} items") last_response, exception = retry_with_backoff(@retries) do status_code, body = send_request(write_key, batch) error = JSON.parse(body)['error'] should_retry = should_retry_request?(status_code, body) logger.debug("Response status code: #{status_code}") logger.debug("Response error: #{error}") if error [Response.new(status_code, error), should_retry] end if exception logger.error(exception.) exception.backtrace.each { |line| logger.error(line) } Response.new(-1, exception.to_s) else last_response end end |