Class: Magentwo::Adapter

Inherits:
Connection show all
Defined in:
lib/adapter.rb

Constant Summary collapse

DateTimeFields =
%i(created_at updated_at change_status_at)
DateFields =
%i(dob)

Instance Attribute Summary

Attributes inherited from Connection

#base_path, #host, #password, #port, #scheme, #token, #user

Instance Method Summary collapse

Methods inherited from Connection

#delete, #get, #initialize, #post, #put, #request, #request_token

Constructor Details

This class inherits a constructor from Magentwo::Connection

Instance Method Details

#call(method, path, params) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/adapter.rb', line 5

def call method, path, params
  http_method, params = case method
  when :put, :post, :delete then [method, params.to_json]
  when :get, :get_with_meta_data then [:get, params]
  else
    raise ArgumentError, "unknown method type. Expected :get, :get_with_meta_data, :post, :put or :delete. #{method} #{path}"
  end

  response = self.send(http_method, path, params)
  Magentwo.logger.debug "Response body: #{response.body}" unless response.is_a? TrueClass

  parsed_response = case method
  when :get_with_meta_data, :put, :post then transform( parse( response))
  when :get
    parsed = parse(response)
    if parsed.is_a?(Hash) && (parsed.has_key? :items)
      (parsed[:items] || []).map do |item|
        transform item
      end
    else
      transform parsed
    end
  when :delete
    response
  else
    raise ArgumentError, "unknown method type. Expected :get, :get_with_meta_data, :post, :put or :delete. #{method} #{path}"
  end
end