Module: Spree::API::Client::Request

Included in:
Spree::API::Client
Defined in:
lib/spree-api-client/request.rb

Instance Method Summary collapse

Instance Method Details

#delete(path, options = {}) ⇒ Object



50
51
52
# File 'lib/spree-api-client/request.rb', line 50

def delete(path, options={})
  request(:delete, path, options).body
end

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



38
39
40
# File 'lib/spree-api-client/request.rb', line 38

def get(path, options = {})
  request(:get, path, options).body
end

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



42
43
44
# File 'lib/spree-api-client/request.rb', line 42

def post(path, options={})
  request(:post, path, options).body
end

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



46
47
48
# File 'lib/spree-api-client/request.rb', line 46

def put(path, options={})
  request(:put, path, options).body
end

#request(method, path, options = {}) ⇒ Object



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
33
34
35
36
# File 'lib/spree-api-client/request.rb', line 7

def request(method, path, options = {})
  token = options.delete(:api_token) || api_token

  begin
    response = connection.send(method) do |request|

      request.headers['Accept'] =  options.delete(:accept) || 'application/json'

      if token
        request.headers['X-Spree-Token'] = token
      end

      case method
      when :get
        options.merge(:per_page => per_page)
        request.url(path, options)
      when :delete, :head
        request.url(path, options)
      when :patch, :post, :put
        request.path = path
        request.body = MultiJson.dump(options) unless options.empty?
      end
    end

  rescue Faraday::Error::ClientError => error
    raise Spree::API::Client::Error::ClientError.new(error)
  end

  response
end