class Kickbox::HttpClient::HttpClient
Main HttpClient
which is used by Api
classes
Attributes
headers[RW]
options[RW]
Public Class Methods
new(auth = {}, options = {})
click to toggle source
# File lib/kickbox/http_client.rb, line 16 def initialize(auth = {}, options = {}) if auth.is_a?(String) auth = { :http_header => auth } end @options = { :base => "https://api.kickbox.io", :api_version => "v1", :user_agent => "alpaca/0.2.1 (https://github.com/pksunkara/alpaca)" } @options.update(options) @headers = { "user-agent" => @options[:user_agent] } if @options.has_key?(:headers) @headers.update(Hash[@options[:headers].map { |k, v| [k.downcase, v] }]) @options.delete(:headers) end @client = Faraday.new(@options[:base]) do |conn| conn.use(Kickbox::HttpClient::AuthHandler, auth) conn.use(Kickbox::HttpClient::ErrorHandler) conn.adapter(Faraday.default_adapter) end end
Public Instance Methods
create_request(method, path, options)
click to toggle source
Creating a request with the given arguments
If api_version is set, appends it immediately after host
# File lib/kickbox/http_client.rb, line 95 def create_request(method, path, options) version = options.has_key?(:api_version) ? "/#{options[:api_version]}" : "" path = "#{version}#{path}" instance_eval <<-RUBY, __FILE__, __LINE__ + 1 @client.#{method}(path) do |req| req.body = options[:body] req.headers.update(options[:headers]) req.params.update(options[:query]) if options[:query] end RUBY end
delete(path, body = {}, options = {})
click to toggle source
# File lib/kickbox/http_client.rb, line 59 def delete(path, body = {}, options = {}) request(path, body, "delete", options) end
get(path, params = {}, options = {})
click to toggle source
# File lib/kickbox/http_client.rb, line 47 def get(path, params = {}, options = {}) request(path, nil, "get", options.merge({ :query => params })) end
get_body(response)
click to toggle source
Get response body in correct format
# File lib/kickbox/http_client.rb, line 110 def get_body(response) Kickbox::HttpClient::ResponseHandler.get_body(response) end
patch(path, body = {}, options = {})
click to toggle source
# File lib/kickbox/http_client.rb, line 55 def patch(path, body = {}, options = {}) request(path, body, "patch", options) end
post(path, body = {}, options = {})
click to toggle source
# File lib/kickbox/http_client.rb, line 51 def post(path, body = {}, options = {}) request(path, body, "post", options) end
put(path, body = {}, options = {})
click to toggle source
# File lib/kickbox/http_client.rb, line 63 def put(path, body = {}, options = {}) request(path, body, "put", options) end
request(path, body, method, options)
click to toggle source
Intermediate function which does three main things
-
Transforms the body of request into correct format
-
Creates the requests with give parameters
-
Returns response body after parsing it into correct format
# File lib/kickbox/http_client.rb, line 72 def request(path, body, method, options) options = @options.merge(options) options[:headers] = options[:headers] || {} options[:headers] = @headers.merge(Hash[options[:headers].map { |k, v| [k.downcase, v] }]) options[:body] = body if method != "get" options[:body] = options[:body] || {} options = set_body(options) end response = create_request(method, path, options) body = get_body(response) Kickbox::HttpClient::Response.new(body, response.status, response.headers) end
set_body(options)
click to toggle source
Set request body in correct format
# File lib/kickbox/http_client.rb, line 115 def set_body(options) Kickbox::HttpClient::RequestHandler.set_body(options) end