Class: Rox::Core::Analytics::Transport

Inherits:
Object
  • Object
show all
Includes:
Defaults::Request, Logging, Utils
Defined in:
lib/rox/core/analytics/transport.rb

Constant Summary

Constants included from Utils

Utils::UTC_OFFSET_WITHOUT_COLON, Utils::UTC_OFFSET_WITH_COLON

Constants included from Defaults::Request

Defaults::Request::RETRIES

Class Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

included, #logger

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(device_properties) ⇒ Transport

Returns a new instance of Transport.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rox/core/analytics/transport.rb', line 18

def initialize(device_properties)
  @device_properties = device_properties
  uri = URI.parse(Rox::Core::Environment.analytics_path)
  logger.debug("Using Analytics url #{uri}")
  @headers = {
    'Accept' => 'application/json',
    'Content-Type' => 'application/json',
    'User-Agent' => "ruby/#{device_properties.lib_version}"
  }
  @path = uri.path + '/impression/' + device_properties.rollout_key
  @retries = RETRIES
  @backoff_policy = Rox::Core::Analytics::BackoffPolicy.new

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = uri.scheme == 'https'
  http.read_timeout = 8
  http.open_timeout = 4

  @http = http
end

Class Attribute Details

.stubObject



137
138
139
# File 'lib/rox/core/analytics/transport.rb', line 137

def stub
  @stub || ENV['STUB']
end

Instance Method Details

#send(batch) ⇒ Response

Sends a batch of messages to the API

Returns:



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rox/core/analytics/transport.rb', line 42

def send(batch)
  logger.debug("Sending request for #{batch.length} items")

  last_response, exception = retry_with_backoff(@retries) do
    status_code, body = send_request(batch)
    should_retry = should_retry_request?(status_code, body)
    logger.debug("Response status code: #{status_code}")
    logger.debug("Response error: #{body}") if status_code != 200

    [Response.new(status_code, body), should_retry]
  end

  if exception
    logger.error(exception.message)
    exception.backtrace.each { |line| logger.error(line) }
    Response.new(-1, exception.to_s)
  else
    last_response
  end
end

#shutdownObject

Closes a persistent connection if it exists



64
65
66
# File 'lib/rox/core/analytics/transport.rb', line 64

def shutdown
  @http.finish if @http.started?
end