lib/reviewed/client.rb in reviewed-0.2.2 vs lib/reviewed/client.rb in reviewed-0.4.0
- old
+ new
@@ -1,22 +1,35 @@
module Reviewed
class Client
- attr_accessor :api_key, :base_uri, :api_version, :request_params
+ attr_accessor :request_params
- BASE_URI = "http://localhost:3000/api/v1"
+ DEFAULT_BASE_URI = "http://localhost:3000/api/v1"
+ class << self
+ attr_accessor :api_key, :api_base_uri, :api_version
+
+ def configure
+ yield self
+ self
+ end
+
+ end
+
def initialize(opts={})
- @api_key = opts[:api_key] || ENV['REVIEWED_API_KEY']
- @base_uri = opts[:base_uri] || BASE_URI
+ @base_uri = opts[:base_uri] || base_uri
+ @api_key = opts[:api_key]
@request_params = opts[:request_params] || {}
end
- def configure
- yield self
- self
+ def base_uri
+ self.class.api_base_uri || DEFAULT_BASE_URI
end
+ def api_key
+ self.class.api_key || ENV['REVIEWED_API_KEY']
+ end
+
# Perform an HTTP GET request
def get(path, params={})
perform(:get, path, params)
end
@@ -57,16 +70,18 @@
private
def perform(method, path, params={})
begin
- self.connection.send(method.to_sym, path, params) do |request|
+ res = 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
+ raise Reviewed::ApiError.new(msg: "API connection returned redirect or error") if res.status > 204 and res.status != 404
+ res
rescue Faraday::Error::ClientError => e
message = <<-EOS.gsub(/^[ ]*/, '')
- API Error. method: #{method}. path: #{path}. params: #{params.to_s}. api_key: #{self.api_key}
+ API Error. method: #{method} url: #{base_uri} path: #{path} params: #{params.to_s} api_key: #{self.api_key}
Original exception message:
#{e.message}
EOS
new_exception = Reviewed::ApiError.new(msg: message)
new_exception.set_backtrace(e.backtrace) # TODO not seeing in Airbrake