Class: Shipvine::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/shipvine/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Client

Returns a new instance of Client.



6
7
8
# File 'lib/shipvine/client.rb', line 6

def initialize(opts = {})
  @options = opts
end

Instance Method Details

#request(method, resource, params = {}, exclude_merchant_code: false) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/shipvine/client.rb', line 10

def request(method, resource, params = {}, exclude_merchant_code: false)
  options = default_options.deep_merge(@options)

  options = if method == :get
    options.merge(query:
      sanitize_query_parameters(params, exclude_merchant_code: exclude_merchant_code)
    )
  else
    # TODO a sanitization method is needed here
    options.merge(body: params)
  end

  response = self.class.send(method, resource, options)

  # NOTE response will be nil if the body of the response is nil

  if response.code != 200
    # TODO response.response.message == 'Unauthorized' in the case of 401
    #      possibly try to sniff out more descriptive error information

    fail Shipvine::Error.new(response), "#{response.code} #{response.body}"
  else
    response
  end
end