Class: Dodgeball::Client::Request

Inherits:
Object
  • Object
show all
Includes:
Defaults::Request, Logging, Utils
Defined in:
lib/dodgeball/client/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::DEFAULT_OPEN_TIMEOUT, Defaults::Request::DEFAULT_READ_TIMEOUT, Defaults::Request::DODGEBALL_API_URL, Defaults::Request::HEADERS, Defaults::Request::HOST, Defaults::Request::PATH_ROOT, Defaults::Request::PORT, Defaults::Request::RETRIES, Defaults::Request::SECRET_KEY_HEADER, Defaults::Request::SOURCE_ID_HEADER, Defaults::Request::SSL, Defaults::Request::VERIFICATION_ID_HEADER

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 dodgeball api request



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/dodgeball/client/request.rb', line 24

def initialize(options = {})
  options[:host] ||= HOST
  options[:port] ||= PORT

  options[:ssl] ||= SSL

  @headers = options[:headers] || HEADERS
  @retries = options[:retries] || RETRIES
  @backoff_policy =
    options[:backoff_policy] || Dodgeball::Client::BackoffPolicy.new

  uri = URI(options[:dodgeball_api_url] || DODGEBALL_API_URL)

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = options[:ssl]
  http.read_timeout = DEFAULT_READ_TIMEOUT
  http.open_timeout = DEFAULT_OPEN_TIMEOUT

  @http = http
end

Class Attribute Details

.stubObject



137
138
139
# File 'lib/dodgeball/client/request.rb', line 137

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

Instance Method Details

#post(write_key, path, request_body, request_specific_headers = nil) ⇒ Object

public: Posts the write key and path to the API.

returns - Response of the status and error if it exists



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/dodgeball/client/request.rb', line 49

def post(write_key, path, request_body, request_specific_headers=nil)

 last_response, exception = retry_with_backoff(@retries) do
    status_code, response_body = send_request(write_key, path, request_body, request_specific_headers)
    should_retry = should_retry_request?(status_code, response_body)
    logger.debug("Response status code: #{status_code}")
    logger.debug("Response response body: #{response_body}") if response_body

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

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