class Salesmachine::Api::Request
Attributes
stub[RW]
Public Class Methods
new(options = {})
click to toggle source
public: Creates a new request object to send analytics batch
# 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
Public Instance Methods
post(api_key, batch)
click to toggle source
public: Posts the write key and batch of messages to the API.
returns - Response
of the status and error if it exists
# 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