Class: CodesWholesale::Client

Inherits:
Object
  • Object
show all
Includes:
Accounts, Orders, Products, Configurable
Defined in:
lib/codes_wholesale/client.rb,
lib/codes_wholesale/client/orders.rb,
lib/codes_wholesale/client/accounts.rb,
lib/codes_wholesale/client/products.rb

Defined Under Namespace

Modules: Accounts, Orders, Products

Instance Attribute Summary

Attributes included from Configurable

#api_version, #client_id, #client_secret, #environment, #user_agent

Instance Method Summary collapse

Methods included from Orders

#order

Methods included from Products

#products

Methods included from Accounts

#account

Methods included from Configurable

#api_endpoint, #configure, keys, #reset!

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



13
14
15
16
17
# File 'lib/codes_wholesale/client.rb', line 13

def initialize(options = {})
  CodesWholesale::Configurable.keys.each do |key|
    instance_variable_set(:"@#{key}", options[key] || CodesWholesale.instance_variable_get(:"@#{key}"))
  end
end

Instance Method Details

#agentObject



23
24
25
26
27
28
29
30
31
# File 'lib/codes_wholesale/client.rb', line 23

def agent
  @agent ||= Sawyer::Agent.new(api_endpoint, sawyer_options) do |http|
    http.headers[:content_type] = 'application/json'
    http.headers[:user_agent] = user_agent
    http.authorization :Bearer, token.token
    http.response :logger
    http.adapter Faraday.default_adapter
  end
end

#get(url, options = {}) ⇒ Object



33
34
35
# File 'lib/codes_wholesale/client.rb', line 33

def get(url, options = {})
  request(:get, url, options)
end

#post(url, options = {}) ⇒ Object



37
38
39
# File 'lib/codes_wholesale/client.rb', line 37

def post(url, options = {})
  request(:post, url, options)
end

#tokenObject



19
20
21
# File 'lib/codes_wholesale/client.rb', line 19

def token
  OAuth2::Client.new(client_id, client_secret, site: "#{api_endpoint}/oauth/token").client_credentials.get_token
end