Class: Orderspace::Client
- Inherits:
-
Object
- Object
- Orderspace::Client
- Includes:
- Default
- Defined in:
- lib/orderspace/client.rb
Overview
This class is the entrypoint to the Orderspace API
To obtain an access token you will have to have registered a private application with Orderspace (https://<your_space>.orderspace.com/admin/apps/new). This will give you the client_id and client_secret you need in order to get your access token.
client = Client.with(client_key, client_secret)
Defined Under Namespace
Classes: CustomersEndpoint, OauthEndpoint, OrdersEndpoint, WebhooksEndpoint
Constant Summary collapse
- HEADER_AUTHORIZATION =
'Authorization'- API_VERSION =
'v1'
Constants included from Default
Default::BASE_URL, Default::USER_AGENT
Instance Attribute Summary collapse
-
#access_token ⇒ Object
readonly
Returns the value of attribute access_token.
Class Method Summary collapse
-
.base_url ⇒ String
Represents the base url to Orderspace’s API (i.e. api.orderspace.com/).
-
.versioned_url ⇒ String
Represents the URL to the Orderspace API with it’s version (i.e. api.orderspace.com/v1/).
- .with(client_id, client_secret) ⇒ Object
Instance Method Summary collapse
- #add_custom_request_options(custom_options) ⇒ Object
- #base_request_options ⇒ Object
-
#customers ⇒ Object
Returns the customers endpoint.
- #delete(path, data = nil, options = {}) ⇒ Object
- #execute(method, path, data = nil, options = {}) ⇒ Object
- #get(path, options = {}) ⇒ Object
-
#initialize(access_token = nil) ⇒ Client
constructor
A new instance of Client.
-
#oauth ⇒ Object
Returns the oauth endpoint.
-
#orders ⇒ Object
Returns the orders endpoint.
- #post(path, data = nil, options = {}) ⇒ Object
- #put(path, data = nil, options = {}) ⇒ Object
- #request(method, uri, data = nil, options = {}) ⇒ Object
-
#webhooks ⇒ Object
Returns the webhooks endpoint.
Constructor Details
#initialize(access_token = nil) ⇒ Client
23 24 25 |
# File 'lib/orderspace/client.rb', line 23 def initialize(access_token = nil) @access_token = access_token end |
Instance Attribute Details
#access_token ⇒ Object (readonly)
Returns the value of attribute access_token.
21 22 23 |
# File 'lib/orderspace/client.rb', line 21 def access_token @access_token end |
Class Method Details
.base_url ⇒ String
Represents the base url to Orderspace’s API (i.e. api.orderspace.com/)
35 36 37 |
# File 'lib/orderspace/client.rb', line 35 def self.base_url BASE_URL end |
.versioned_url ⇒ String
Represents the URL to the Orderspace API with it’s version (i.e. api.orderspace.com/v1/)
42 43 44 |
# File 'lib/orderspace/client.rb', line 42 def self.versioned_url "#{base_url}#{API_VERSION}/" end |
.with(client_id, client_secret) ⇒ Object
27 28 29 30 |
# File 'lib/orderspace/client.rb', line 27 def self.with(client_id, client_secret) credentials = new.oauth.obtain_access_token(client_id, client_secret) new(credentials.access_token) end |
Instance Method Details
#add_custom_request_options(custom_options) ⇒ Object
139 140 141 142 143 144 145 |
# File 'lib/orderspace/client.rb', line 139 def () .tap do || .each do |key, value| [key] = value end end end |
#base_request_options ⇒ Object
147 148 149 150 151 152 153 154 155 |
# File 'lib/orderspace/client.rb', line 147 def { format: :json, headers: { 'Accept' => 'application/json', 'User-Agent' => USER_AGENT } } end |
#customers ⇒ Object
Returns the customers endpoint
54 55 56 |
# File 'lib/orderspace/client.rb', line 54 def customers CustomersEndpoint.new(self) end |
#delete(path, data = nil, options = {}) ⇒ Object
98 99 100 |
# File 'lib/orderspace/client.rb', line 98 def delete(path, data = nil, = {}) execute(:delete, path, data, ) end |
#execute(method, path, data = nil, options = {}) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/orderspace/client.rb', line 102 def execute(method, path, data = nil, = {}) uri = Client.versioned_url + path response = request(method, uri, data, ) case response.code when 200..299 response when 400 raise BadRequestError, response when 401 raise AuthorizationFailedError, response when 404 raise NotFoundError, response when 422 raise UnprocessableEntityError, response when 429 raise TooManyRequestsError, response when 500 raise InternalServerError, response else raise RequestError, response end end |
#get(path, options = {}) ⇒ Object
86 87 88 |
# File 'lib/orderspace/client.rb', line 86 def get(path, = {}) execute(:get, path, nil, .to_h) end |
#oauth ⇒ Object
Returns the oauth endpoint
48 49 50 |
# File 'lib/orderspace/client.rb', line 48 def oauth OauthEndpoint.new(self) end |
#orders ⇒ Object
Returns the orders endpoint
60 61 62 |
# File 'lib/orderspace/client.rb', line 60 def orders OrdersEndpoint.new(self) end |
#post(path, data = nil, options = {}) ⇒ Object
90 91 92 |
# File 'lib/orderspace/client.rb', line 90 def post(path, data = nil, = {}) execute(:post, path, data, ) end |
#put(path, data = nil, options = {}) ⇒ Object
94 95 96 |
# File 'lib/orderspace/client.rb', line 94 def put(path, data = nil, = {}) execute(:put, path, data, ) end |
#request(method, uri, data = nil, options = {}) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/orderspace/client.rb', line 126 def request(method, uri, data = nil, = {}) = () [:headers]['Authorization'] = "Bearer #{@access_token}" if @access_token if data [:headers]['Content-Type'] = 'application/json' [:body] = JSON.dump(data) end HTTParty.send(method, uri, ) end |
#webhooks ⇒ Object
Returns the webhooks endpoint
66 67 68 |
# File 'lib/orderspace/client.rb', line 66 def webhooks WebhooksEndpoint.new(self) end |