Class: Orderspace::Client

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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_tokenObject (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_urlString

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_urlString

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 add_custom_request_options(custom_options)
  base_request_options.tap do |options|
    custom_options.each do |key, value|
      options[key] = value
    end
  end
end

#base_request_optionsObject



147
148
149
150
151
152
153
154
155
# File 'lib/orderspace/client.rb', line 147

def base_request_options
  {
    format: :json,
    headers: {
      'Accept' => 'application/json',
      'User-Agent' => USER_AGENT
    }
  }
end

#customersObject

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, options = {})
  execute(:delete, path, data, options)
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, options = {})
  uri = Client.versioned_url + path
  response = request(method, uri, data, options)

  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, options = {})
  execute(:get, path, nil, options.to_h)
end

#oauthObject

Returns the oauth endpoint



48
49
50
# File 'lib/orderspace/client.rb', line 48

def oauth
  OauthEndpoint.new(self)
end

#ordersObject

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, options = {})
  execute(:post, path, data, options)
end

#put(path, data = nil, options = {}) ⇒ Object



94
95
96
# File 'lib/orderspace/client.rb', line 94

def put(path, data = nil, options = {})
  execute(:put, path, data, options)
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, options = {})
  request_options = add_custom_request_options(options)

  request_options[:headers]['Authorization'] = "Bearer #{@access_token}" if @access_token

  if data
    request_options[:headers]['Content-Type'] = 'application/json'
    request_options[:body] = JSON.dump(data)
  end

  HTTParty.send(method, uri, request_options)
end

#webhooksObject

Returns the webhooks endpoint



66
67
68
# File 'lib/orderspace/client.rb', line 66

def webhooks
  WebhooksEndpoint.new(self)
end