Sha256: 0d227028b61b0fafb4a675d2edbeccd3a879c75b82a7f71cd3dcd4abe2e370c2

Contents?: true

Size: 1.59 KB

Versions: 1

Compression:

Stored size: 1.59 KB

Contents

module Reviewed
  class Client
    attr_accessor :api_key, :base_uri, :api_version, :request_params

    BASE_URI = "http://localhost:3000/api/v1"

    def initialize(opts={})
      @api_key = opts[:api_key] || ENV['REVIEWED_API_KEY']
      @base_uri = opts[:base_uri] || BASE_URI
      @request_params = opts[:request_params] || {}
    end

    def configure
      yield self
      self
    end

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

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

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

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

    def resource(name)
      klass_string = "Reviewed::#{name.to_s.singularize.classify}"
      klass_string.constantize rescue name
    end

    def method_missing(method, *args, &block)
      Reviewed::Request.new(resource: resource(method), client: self)
    end

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

    private

    def perform(method, path, params={})
      self.connection.send(method.to_sym, path, params) do |request|
        request.params.merge!(self.request_params)
        request.headers['X-Reviewed-Authorization'] ||= self.api_key
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
reviewed-0.1.25 lib/reviewed/client.rb