Sha256: 5d630876a12e92087c5206bd45a15a55acdd59cb623f41805dd856328dacfe20

Contents?: true

Size: 1.42 KB

Versions: 1

Compression:

Stored size: 1.42 KB

Contents

require 'spree-api-client/error'

module Spree
  module API
    class Client
      module Request
        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

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

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

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

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
spree-api-client-0.0.2 lib/spree-api-client/request.rb