Class: GunBroker::API

Inherits:
Object
  • Object
show all
Defined in:
lib/gun_broker/api.rb

Overview

Generic REST adapter for the GunBroker API.

Constant Summary collapse

ROOT_URL =

Root URL of the GunBroker API.

'https://api.gunbroker.com/v1'
ROOT_URL_SANDBOX =

Root URL of the GunBroker sandbox API.

'https://api.sandbox.gunbroker.com/v1'
PAGE_SIZE =

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

300
MAX_ITEMS_TIME_FRAME =
1
MAX_ORDERS_TIME_FRAME =
7
USER_AGENT =
"gun_broker rubygems.org/gems/gun_broker v(#{GunBroker::VERSION})"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, params = {}, headers = {}) ⇒ API

Returns a new instance of API.

Parameters:

  • path (String)

    The requested API endpoint.

  • params (Hash) (defaults to: {})

    (optional) URL params for GET requests; form params for POST request.

  • headers (Hash) (defaults to: {})

    (optional) Additional headers sent with the request.

Raises:



26
27
28
29
30
31
32
33
34
# 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

Class Method Details

.delete(*args) ⇒ Object

Wrapper for new(*args).delete!

Parameters:



38
39
40
# File 'lib/gun_broker/api.rb', line 38

def self.delete(*args)
  new(*args).delete!
end

.get(*args) ⇒ Object

Wrapper for new(*args).get!

Parameters:



44
45
46
# File 'lib/gun_broker/api.rb', line 44

def self.get(*args)
  new(*args).get!
end

.multipart_post(*args) ⇒ Object

Parameters:



56
57
58
# File 'lib/gun_broker/api.rb', line 56

def self.multipart_post(*args)
  new(*args).multipart_post!
end

.post(*args) ⇒ Object

Wrapper for new(*args).post!

Parameters:



50
51
52
# File 'lib/gun_broker/api.rb', line 50

def self.post(*args)
  new(*args).post!
end

.put(*args) ⇒ Object

Wrapper for new(*args).put!

Parameters:



62
63
64
# File 'lib/gun_broker/api.rb', line 62

def self.put(*args)
  new(*args).put!
end

Instance Method Details

#delete!Object

Sends a DELETE request to the given path.



67
68
69
70
71
# 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!Object

Sends a GET request to the given path.



74
75
76
77
78
79
80
# 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!Object

Sends a multipart form POST to the given path.



92
93
94
95
96
97
98
# 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!Object

Sends a POST request to the given path.



83
84
85
86
87
88
89
# 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!Object

Sends a PUT request to the given path.



101
102
103
104
105
106
107
# 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