class Lightspeed::Request

Constants

SECONDS_TO_WAIT_WHEN_THROTTLED

Attributes

verbose[W]
bucket_level[RW]
bucket_max[RW]
raw_request[RW]

Public Class Methods

base_host() click to toggle source
# File lib/lightspeed/request.rb, line 20
def self.base_host
  "api.merchantos.com"
end
base_path() click to toggle source
# File lib/lightspeed/request.rb, line 24
def self.base_path
  "/API"
end
new(client, method:, path:, params: nil, body: nil) click to toggle source
# File lib/lightspeed/request.rb, line 28
def initialize(client, method:, path:, params: nil, body: nil)
  @method = method
  @params = params
  @path = path
  @bucket_max = Float::INFINITY
  @bucket_level = 0
  @http = Net::HTTP.new(self.class.base_host, 443)
  @http.use_ssl = true
  @raw_request = request_class.new(uri)
  @raw_request.body = body if body
  @raw_request.set_form_data(@params) if @params && @method != :get
  @client = client
  set_authorization_header
end
verbose?() click to toggle source
# File lib/lightspeed/request.rb, line 16
def self.verbose?
  !! @verbose
end

Public Instance Methods

perform() click to toggle source
# File lib/lightspeed/request.rb, line 57
def perform
  perform_raw
rescue Lightspeed::Error::Throttled
  retry_throttled_request
rescue Lightspeed::Error::Unauthorized => e
  raise e if @attempted_oauth_token_refresh
  @client.refresh_oauth_token
  set_authorization_header
  @attempted_oauth_token_refresh = true
  perform
end
perform_raw() click to toggle source
# File lib/lightspeed/request.rb, line 47
def perform_raw
  response = @http.request(raw_request)
  extract_rate_limits(response)
  if response.code == "200"
    handle_success(response)
  else
    handle_error(response)
  end
end
set_authorization_header() click to toggle source
# File lib/lightspeed/request.rb, line 43
def set_authorization_header
  @raw_request["Authorization"] = "Bearer #{@client.oauth_token}" if @client.oauth_token
end

Private Instance Methods

extract_rate_limits(response) click to toggle source
# File lib/lightspeed/request.rb, line 103
def extract_rate_limits(response)
  if bucket_headers = response["X-LS-API-Bucket-Level"]
    @bucket_level, @bucket_max = bucket_headers.split("/").map(&:to_f)
  end
end
handle_error(response) click to toggle source
# File lib/lightspeed/request.rb, line 83
def handle_error(response)
  error = case response.code.to_s
  when '400' then Lightspeed::Error::BadRequest
  when '401' then Lightspeed::Error::Unauthorized
  when '403' then Lightspeed::Error::NotAuthorized
  when '404' then Lightspeed::Error::NotFound
  when '429' then Lightspeed::Error::Throttled
  when /5../ then Lightspeed::Error::InternalServerError
  else Lightspeed::Error
  end

  # We may not get back valid JSON for a failed request
  begin
    data = Yajl::Parser.parse(response.body)
    raise error, data["message"]
  rescue Yajl::ParseError
    raise error, response.code
  end
end
handle_success(response) click to toggle source
# File lib/lightspeed/request.rb, line 71
def handle_success(response)
  json = Yajl::Parser.parse(response.body)
  pp json if self.class.verbose?
  json
end
request_class() click to toggle source
# File lib/lightspeed/request.rb, line 115
def request_class
  case @method
  when :get then Net::HTTP::Get
  when :put then Net::HTTP::Put
  when :post then Net::HTTP::Post
  when :delete then Net::HTTP::Delete
  end
end
retry_throttled_request() click to toggle source
# File lib/lightspeed/request.rb, line 77
def retry_throttled_request
  puts 'retrying throttled request after 60s.' if self.class.verbose?
  sleep SECONDS_TO_WAIT_WHEN_THROTTLED
  perform
end
uri() click to toggle source
# File lib/lightspeed/request.rb, line 109
def uri
  uri = self.class.base_path + @path
  uri += "?#{URI.encode_www_form(@params)}" if @params && @method == :get
  uri
end