class GunBroker::API

Generic REST adapter for the GunBroker API.

Constants

MAX_ITEMS_TIME_FRAME
MAX_ORDERS_TIME_FRAME
PAGE_SIZE

Used to return the maximum number of results from paginated responses.

ROOT_URL

Root URL of the GunBroker API.

ROOT_URL_SANDBOX

Root URL of the GunBroker sandbox API.

USER_AGENT

Public Class Methods

delete(*args) click to toggle source

Wrapper for {GunBroker::API#delete! `new(*args).delete!`} @param *args Splat arguments passed to {#initialize}.

# File lib/gun_broker/api.rb, line 38
def self.delete(*args)
  new(*args).delete!
end
get(*args) click to toggle source

Wrapper for {GunBroker::API#get! `new(*args).get!`} @param *args Splat arguments passed to {#initialize}.

# File lib/gun_broker/api.rb, line 44
def self.get(*args)
  new(*args).get!
end
multipart_post(*args) click to toggle source

Wrapper for {GunBroker::API#multipart_post! `new(*args).multipart_post!`} @param *args Splat arguments passed to {#initialize}.

# File lib/gun_broker/api.rb, line 56
def self.multipart_post(*args)
  new(*args).multipart_post!
end
new(path, params = {}, headers = {}) click to toggle source

@param path [String] The requested API endpoint. @param params [Hash] (optional) URL params for GET requests; form params for POST request. @param headers [Hash] (optional) Additional headers sent with the request.

# File lib/gun_broker/api.rb, line 26
def initialize(path, params = {}, headers = {})
  raise GunBroker::Error.new("Path must start with '/': #{path}") unless path.start_with?('/')

  @base_api_url = GunBroker.sandbox ? ROOT_URL_SANDBOX : ROOT_URL

  @path = path
  @params = params
  @headers = headers
end
post(*args) click to toggle source

Wrapper for {GunBroker::API#post! `new(*args).post!`} @param *args Splat arguments passed to {#initialize}.

# File lib/gun_broker/api.rb, line 50
def self.post(*args)
  new(*args).post!
end
put(*args) click to toggle source

Wrapper for {GunBroker::API#put! `new(*args).put!`} @param *args Splat arguments passed to {#initialize}.

# File lib/gun_broker/api.rb, line 62
def self.put(*args)
  new(*args).put!
end

Public Instance Methods

delete!() click to toggle source

Sends a DELETE request to the given `path`.

# File lib/gun_broker/api.rb, line 67
def delete!
  request = Net::HTTP::Delete.new(uri)
  response = get_response(request)
  GunBroker::Response.new(response)
end
get!() click to toggle source

Sends a GET request to the given `path`.

# File lib/gun_broker/api.rb, line 74
def get!
  uri.query = URI.encode_www_form(@params)

  request = Net::HTTP::Get.new(uri)
  response = get_response(request)
  GunBroker::Response.new(response)
end
multipart_post!() click to toggle source

Sends a multipart form POST to the given `path`.

# File lib/gun_broker/api.rb, line 92
def multipart_post!
  request = Net::HTTP::Post.new(uri)
  request.body = build_request_body

  response = get_response(request)
  GunBroker::Response.new(response)
end
post!() click to toggle source

Sends a POST request to the given `path`.

# File lib/gun_broker/api.rb, line 83
def post!
  request = Net::HTTP::Post.new(uri)
  request.body = @params.to_json

  response = get_response(request)
  GunBroker::Response.new(response)
end
put!() click to toggle source

Sends a PUT request to the given `path`.

# File lib/gun_broker/api.rb, line 101
def put!
  request = Net::HTTP::Put.new(uri)
  request.body = @params.to_json

  response = get_response(request)
  GunBroker::Response.new(response)
end

Private Instance Methods

build_request_body() click to toggle source
# File lib/gun_broker/api.rb, line 111
def build_request_body
  boundary = ::SecureRandom.hex(15)

  @headers['Content-Type'] = "multipart/form-data; boundary=#{boundary}"

  body = []
  body << "--#{boundary}\r\n"
  body << "Content-Disposition: form-data; name=\"data\"\r\n"
  body << "\r\n"
  body << "#{@params.to_json}\r\n"
  body << "--#{boundary}--\r\n"

  body.join
end
get_response(request) click to toggle source
# File lib/gun_broker/api.rb, line 126
def get_response(request)
  request['Content-Type'] = 'application/json'
  request['X-DevKey']     = GunBroker.dev_key
  request["User-Agent"]   = USER_AGENT

  @headers.each { |header, value| request[header] = value }

  # using std-lib Timeout module
  # The GunBroker API is so fickle that the 'read_timeout' option might never even get a chance
  Timeout.timeout(GunBroker.timeout) do
    net_http_class.start(uri.host, uri.port, net_http_options) do |http|
      http.ssl_version = :TLSv1
      http.ciphers = ['RC4-SHA']
      http.request(request)
    end
  end
rescue Timeout::Error, Net::ReadTimeout => e
  raise GunBroker::Error::TimeoutError.new("waited for #{GunBroker.timeout} seconds with no response (#{uri}) #{e.inspect}")
end
net_http_class() click to toggle source
# File lib/gun_broker/api.rb, line 146
def net_http_class
  if GunBroker.proxy_url?
    proxy_uri = URI.parse(GunBroker.proxy_url)
    Net::HTTP::Proxy(proxy_uri.host, proxy_uri.port, proxy_uri.user, proxy_uri.password)
  else
    Net::HTTP
  end
end
net_http_options() click to toggle source
# File lib/gun_broker/api.rb, line 155
def net_http_options
  {
    use_ssl: uri.scheme == 'https',
    read_timeout: GunBroker.timeout
  }
end
uri() click to toggle source
# File lib/gun_broker/api.rb, line 162
def uri
  @uri ||= URI([@base_api_url, @path].join)
end