Sha256: d07eb25165d9f1e5a3df053434234deffe24ae430658f7d3e0ed0e4d53952e2b

Contents?: true

Size: 1.26 KB

Versions: 3

Compression:

Stored size: 1.26 KB

Contents

module Reviewed
  class Client
    include ::Reviewed::Configurable

    attr_accessor :api_key, :base_uri, :api_version

    def initialize
      Reviewed::Configurable.options.each do |key, value|
        instance_variable_set(:"@#{key}", value)
      end
    end

    # Perform an HTTP DELETE request
    def delete(path, params={})
      request(:delete, path, params)
    end

    # Perform an HTTP GET request
    def get(path, params={})
      request(:get, path, params)
    end

    # Perform an HTTP POST request
    def post(path, params={})
      request(:post, path, params)
    end

    # Perform an HTTP PUT request
    def put(path, params={})
      request(:put, path, params)
    end

    def url
      [base_uri, api_version].join('/')
    end

    private

    def request(method, path, params={})
      verify_key!

      connection.send(method.to_sym, path, params) do |request|
        request.headers['X-Reviewed-Authorization'] ||= Reviewed.api_key
      end
    end

    def connection
      @connection ||= ::Faraday.new(url: url) do |faraday|
        faraday.response :mashify
        faraday.response :json
        faraday.request  :global_params
        faraday.request  :url_encoded
        faraday.adapter  Faraday.default_adapter
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
reviewed-0.1.2 lib/reviewed/client.rb
reviewed-0.1.1 lib/reviewed/client.rb
reviewed-0.1.0 lib/reviewed/client.rb