class Answers::Client
Attributes
connection[R]
Public Class Methods
new(config = {})
click to toggle source
# File lib/answers/client.rb, line 48 def initialize(config = {}) # use the default url = Protocol::BASE_PATH # or use the provided url, if one is provided if config[:url] url = config[:url] end # put it in a hash faraday_defaults = { url: url } @connection = Faraday.new(faraday_defaults) do |faraday| #faraday.response :logger # log requests to STDOUT faraday.adapter Faraday.default_adapter # make requests with Net::HTTP faraday.headers['Content-Type'] = Protocol::DEFAULT_CONTENT_TYPE faraday.headers[Protocol::EMAIL_HEADER_KEY] = config[:user_email] if config[:user_email] faraday.headers[Protocol::TOKEN_HEADER_KEY] = config[:user_token] if config[:user_token] end end
Public Instance Methods
delete(path)
click to toggle source
# File lib/answers/client.rb, line 98 def delete(path) request(:delete, path) end
get(path, params=nil)
click to toggle source
# File lib/answers/client.rb, line 80 def get(path, params=nil) request(:get, path) do |req| req.params = params if params end end
handle_response(response)
click to toggle source
# File lib/answers/client.rb, line 102 def handle_response(response) if response.status == 401 raise Answers::Error.new "401 Unauthorized" end JSON.parse(response.body) end
post(path, body=nil)
click to toggle source
# File lib/answers/client.rb, line 86 def post(path, body=nil) request(:post, path) do |req| req.body = body.to_json if body end end
put(path, body=nil)
click to toggle source
# File lib/answers/client.rb, line 92 def put(path, body=nil) request(:put, path) do |req| req.body = body.to_json if body end end
request(method, path, &block)
click to toggle source
# File lib/answers/client.rb, line 71 def request(method, path, &block) response = @connection.send(method) do |req| req.url(path) block.call(req) if block_given? end handle_response(response) end