Sha256: b7a1feb884177af461d729872829406924fefd1b26a7ef77b27fdf42331b54e8

Contents?: true

Size: 1.21 KB

Versions: 4

Compression:

Stored size: 1.21 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
      # always return the {@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

4 entries across 4 versions & 1 rubygems

Version Path
passworks-0.0.4 lib/passworks/request.rb
passworks-0.0.3 lib/passworks/request.rb
passworks-0.0.2 lib/passworks/request.rb
passworks-0.0.1 lib/passworks/request.rb