Class: Salesmachine::Api::Request

Inherits:
Object
  • Object
show all
Includes:
Config::Request, Logging, Utils
Defined in:
lib/salesmachine/api/request.rb

Constant Summary

Constants included from Utils

Utils::UTC_OFFSET_WITHOUT_COLON, Utils::UTC_OFFSET_WITH_COLON

Constants included from Config::Request

Config::Request::BACKOFF, Config::Request::HEADERS, Config::Request::HOST, Config::Request::PATH, Config::Request::PORT, Config::Request::RETRIES, Config::Request::SSL

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(options = {}) ⇒ Request

public: Creates a new request object to send analytics batch



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/salesmachine/api/request.rb', line 18

def initialize(options = {})
  options[:host] ||= HOST
  options[:port] ||= PORT
  # ||= not working on boolean ;-)
  options[:ssl] = SSL if options[:ssl].nil?

  options[:headers] ||= HEADERS
  @path = options[:path] || PATH
  @retries = options[:retries] || RETRIES
  @backoff = options[:backoff] || BACKOFF
  http = Net::HTTP.new(options[:host], options[:port])
  http.use_ssl = options[:ssl]
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  http.read_timeout = 8
  http.open_timeout = 4

  @http = http
end

Class Attribute Details

.stubObject

Returns the value of attribute stub.



82
83
84
# File 'lib/salesmachine/api/request.rb', line 82

def stub
  @stub
end

Instance Method Details

#post(api_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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/salesmachine/api/request.rb', line 40

def post(api_key, batch)
  status = nil
  error = nil
  remaining_retries = @retries
  backoff = @backoff
  headers = { 'Content-Type' => 'application/json', 'accept' => 'application/json' }
  begin
    #          payload = JSON.generate  :api_token=>api_key, :encode=>"base64", :data=>batch
    payload = batch.to_json

    request = Net::HTTP::Post.new(@path, headers)
    request.basic_auth api_key, api_key

    if self.class.stub
      status = 200
      error = nil
      logger.debug "stubbed request to #{@path}: write key = #{api_key}, payload = #{payload}"
    else
      res = @http.request(request, payload)

      status = res.code.to_i
      unless status == 200 || status == 201
        body = JSON.parse(res.body)
        error = body['error']
      end
    end
  rescue Exception => e
    unless (remaining_retries -= 1).zero?
      sleep(backoff)
      retry
    end

    logger.error e.message
    e.backtrace.each { |line| logger.error line }
    status = -1
    error = "Connection error: #{e}"
  end

  Response.new status, error
end