Sha256: e051b95dcefb8edf8a66708e69f20ebdea334f0ee60a36d102d6187716337e54

Contents?: true

Size: 1.18 KB

Versions: 2

Compression:

Stored size: 1.18 KB

Contents

module Passworks
  module Request

    def request(method, path, options={})
      response = agent.send(method) do |request|
        case method
        when :get, :delete
          request.url(path, options[:query])
        when :post, :put, :patch
          request.path = path
          request.body = options.fetch(:body, {})
        end
      end
      Response.new(self, response)
    end

    def agent
      @agent ||= ::Faraday.new(endpoint) do |connection|
        connection.basic_auth(@api_username, @api_secret)
        connection.request :multipart
        connection.request :json
        connection.response :logger
        connection.response :json, :content_type => /\bjson$/
        connection.use Passworks::Faraday::HttpExceptionMiddleware
        connection.adapter ::Faraday.default_adapter
      end
      @agent.headers[:user_agent]    = @user_agent
      @agent
    end

    def get(url, options={})
      request(:get, url, options)
    end

    def post(url, options={})
      request(:post, url, options)
    end

    def patch(url, options={})
      request(:patch, url, options)
    end

    def delete(url, options={})
      request(:delete, url, options)
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
passworks-2.0.6 lib/passworks/request.rb
passworks-2.0.5 lib/passworks/request.rb