module Spree::API::Client::Request

Public Instance Methods

delete(path, options={}) click to toggle source
# File lib/spree-api-client/request.rb, line 53
def delete(path, options={})
  request(:delete, path, options).body
end
get(path, options = {}) click to toggle source
# File lib/spree-api-client/request.rb, line 41
def get(path, options = {})
  request(:get, path, options).body
end
post(path, options={}) click to toggle source
# File lib/spree-api-client/request.rb, line 45
def post(path, options={})
  request(:post, path, options).body
end
put(path, options={}) click to toggle source
# File lib/spree-api-client/request.rb, line 49
def put(path, options={})
  request(:put, path, options).body
end
request(method, path, options = {}) click to toggle source
# File lib/spree-api-client/request.rb, line 8
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

      options.merge!(:locale => locale)

      case method
      when :get
        options[:per_page] = per_page unless options[: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