class RainforestQA::Client

Constants

API_PREFIX
API_URL
API_VERSION

Attributes

api_key[RW]

Public Class Methods

new(options = {}) { |self| ... } click to toggle source
# File lib/rainforestqa/client.rb, line 11
def initialize(options = {})
  options.each do | k, v |
    if v.is_a?(Integer)
      raise RainforestQA::APIConfigurationError
    end
    send(:"#{k}=", v)
  end
  yield(self) if block_given?
end

Public Instance Methods

delete(path, params = {}) click to toggle source
# File lib/rainforestqa/client.rb, line 33
def delete(path, params = {})
  request(:delete, path, params)
end
get(path, params = {}) click to toggle source
# File lib/rainforestqa/client.rb, line 25
def get(path, params = {})
  request(:get, path, params)
end
post(path, params = {}) click to toggle source
# File lib/rainforestqa/client.rb, line 21
def post(path, params = {})
  request(:post, path, params)
end
put(path, params = {}) click to toggle source
# File lib/rainforestqa/client.rb, line 29
def put(path, params = {})
  request(:put, path, params)
end

Private Instance Methods

connection(options = {}) click to toggle source
# File lib/rainforestqa/client.rb, line 39
def connection(options = {})
  @connection ||= Faraday.new(API_URL, options) do | conn |
    conn.request   :encode_rfqa_json
    conn.adapter   :net_http
    conn.response  :parse_rfqa_json
    conn.response  :raise_error_rfqa_from_json
  end
end
request(method, path, params = {}) click to toggle source
# File lib/rainforestqa/client.rb, line 48
def request(method, path, params = {})
  connection.send(method.to_sym) do | req |
    req.url API_PREFIX + path
    req.headers['CLIENT_TOKEN'] = api_key
    req.headers['Content-Type'] = 'application/json'
    req.body = params.to_json
  end
rescue Faraday::Error::TimeoutError, Timeout::Error => error
  raise(RainforestQA::APITimeoutError.new(error))
end