Class: Unleashed::Client

Inherits:
Object
  • Object
show all
Includes:
Configurable
Defined in:
lib/unleashed/client.rb

Overview

Client for the Unleashed API

Instance Attribute Summary

Attributes included from Configurable

#api_domain, #api_id, #api_key, #client_type_header, #errors_format

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Configurable

#api_endpoint, keys, #reset!

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



35
36
37
38
39
40
41
42
# File 'lib/unleashed/client.rb', line 35

def initialize(options = {})
  # Use options passed in, but fall back to module defaults
  Unleashed::Configurable.keys.each do |key|
    instance_variable_set(
      :"@#{key}", options[key] || Unleashed.instance_variable_get(:"@#{key}")
    )
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object

Catch calls for resources



153
154
155
156
157
158
159
160
# File 'lib/unleashed/client.rb', line 153

def method_missing(name, *args, &block)
  if self.class.resources.keys.include?(name)
    resources[name] ||= self.class.resources[name].new(self)
    resources[name]
  else
    super
  end
end

Class Method Details

.resourcesHash

Available resources for Unleashed::Client

Returns:

  • (Hash)


141
142
143
144
145
146
147
148
149
# File 'lib/unleashed/client.rb', line 141

def self.resources
  {
    customers: CustomerResource,
    invoices: InvoiceResource,
    currencies: CurrencyResource,
    payment_terms: PaymentTermResource,
    companies: CompanyResource
  }
end

Instance Method Details

#connectionFaraday::Connection

Create a new Faraday connection

Returns:

  • (Faraday::Connection)


47
48
49
50
51
# File 'lib/unleashed/client.rb', line 47

def connection
  Faraday.new(url: @api_endpoint, request: { params_encoder: CustomParamsEncoder }) do |faraday|
    faraday.adapter :net_http
  end
end

#get(url, parameters = {}, headers = {}, skip_status_check = false) ⇒ Faraday::Response

Make a HTTP GET request

Parameters:

Returns:

  • (Faraday::Response)


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/unleashed/client.rb', line 83

def get(url, parameters = {}, headers = {}, skip_status_check = false)
  response = connection.get do |request|
    request.url "#{api_endpoint}#{url}"
    request.params = parameters.sort.to_h

    # Set headers
    request = init_default_headers(request)

    # Assign more custom headers
    headers.each do |key, value|
      request.headers[key] = value
    end

    # Print log
    time = Time.zone.now.strftime('%Y-%m-%d %H:%M:%S %z')
    puts "[#{time}] Client -- : HEADERS #{request.headers}"
    puts "[#{time}] Client -- : GET #{request.path}"
    puts "[#{time}] Client -- : PARAMS #{request.params}"
  end

  on_complete(response) unless skip_status_check
  response
end

#hmac_input(params_hash) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/unleashed/client.rb', line 53

def hmac_input(params_hash)
  hmac_input_a = []

  params_hash.each do |k, v|
    hmac_input_a << "#{k}=#{v}"
  end

  hmac_input_a.join('&')
end

#init_default_headers(request) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/unleashed/client.rb', line 69

def init_default_headers(request)
  request.headers['client-type'] = @client_type_header
  request.headers['Accept'] = 'application/json'
  request.headers['Content-Type'] = 'application/json'
  request.headers['api-auth-id'] = @api_id
  request.headers['api-auth-signature'] = signature(hmac_input(request.params))
  request
end

#post(url, parameters = {}, headers = {}, skip_status_check = false) ⇒ Faraday::Response

Make a HTTP POST request

Parameters:

Returns:

  • (Faraday::Response)


112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/unleashed/client.rb', line 112

def post(url, parameters = {}, headers = {}, skip_status_check = false)
  response = connection.post do |request|
    request.url "#{api_endpoint}#{url}"

    # Set headers
    init_default_headers(request)

    # Assign more custom headers
    headers.each do |key, value|
      request.headers[key] = value
    end

    request.body = parameters.to_json

    # Print log
    time = Time.zone.now.strftime('%Y-%m-%d %H:%M:%S %z')
    puts "[#{time}] Client -- : HEADERS #{request.headers}"
    puts "[#{time}] Client -- : POST #{request.path}"
    puts "[#{time}] Client -- : PARAMS #{request.params}"
    puts "[#{time}] Client -- : BODY #{request.body}"
  end

  on_complete(response) unless skip_status_check
  response
end

#resourcesHash

Resources being currently used

Returns:

  • (Hash)


165
166
167
# File 'lib/unleashed/client.rb', line 165

def resources
  @resources ||= {}
end

#signature(params = '') ⇒ Object

Create a signature for request



64
65
66
67
# File 'lib/unleashed/client.rb', line 64

def signature(params = '')
  hash = OpenSSL::HMAC.digest('sha256', @api_key, params)
  Base64.strict_encode64(hash)
end