Class: Buffer::HttpClient::HttpClient
- Inherits:
-
Object
- Object
- Buffer::HttpClient::HttpClient
- Defined in:
- lib/buffer/http_client.rb
Overview
Main HttpClient which is used by Api classes
Instance Attribute Summary collapse
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
-
#create_request(method, path, options) ⇒ Object
Creating a request with the given arguments.
- #delete(path, body = {}, options = {}) ⇒ Object
- #get(path, params = {}, options = {}) ⇒ Object
-
#get_body(response) ⇒ Object
Get response body in correct format.
-
#initialize(auth = {}, options = {}) ⇒ HttpClient
constructor
A new instance of HttpClient.
- #patch(path, body = {}, options = {}) ⇒ Object
- #post(path, body = {}, options = {}) ⇒ Object
- #put(path, body = {}, options = {}) ⇒ Object
-
#request(path, body, method, options) ⇒ Object
Intermediate function which does three main things.
-
#set_body(options) ⇒ Object
Set request body in correct format.
Constructor Details
#initialize(auth = {}, options = {}) ⇒ HttpClient
Returns a new instance of HttpClient.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/buffer/http_client.rb', line 18 def initialize(auth = {}, = {}) if auth.is_a? String auth = { :access_token => auth } end @options = { :base => "https://api.bufferapp.com", :api_version => "1", :user_agent => "alpaca/0.1.0 (https://github.com/pksunkara/alpaca)" } @options.update @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 Buffer::HttpClient::AuthHandler, auth conn.use Buffer::HttpClient::ErrorHandler conn.adapter Faraday.default_adapter end end |
Instance Attribute Details
#headers ⇒ Object
Returns the value of attribute headers.
14 15 16 |
# File 'lib/buffer/http_client.rb', line 14 def headers @headers end |
#options ⇒ Object
Returns the value of attribute options.
14 15 16 |
# File 'lib/buffer/http_client.rb', line 14 def @options end |
Instance Method Details
#create_request(method, path, options) ⇒ Object
Creating a request with the given arguments
If api_version is set, appends it immediately after host
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/buffer/http_client.rb', line 97 def create_request(method, path, ) version = .has_key?(:api_version) ? "/#{[:api_version]}" : "" # Adds a suffix (ex: ".html", ".json") to url suffix = .has_key?(:response_type) ? [:response_type] : "json" path = "#{path}.#{suffix}" 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 = {}) ⇒ Object
61 62 63 |
# File 'lib/buffer/http_client.rb', line 61 def delete(path, body = {}, = {}) request path, body, "delete", end |
#get(path, params = {}, options = {}) ⇒ Object
49 50 51 |
# File 'lib/buffer/http_client.rb', line 49 def get(path, params = {}, = {}) request path, nil, "get", .merge({ :query => params }) end |
#get_body(response) ⇒ Object
Get response body in correct format
116 117 118 |
# File 'lib/buffer/http_client.rb', line 116 def get_body(response) Buffer::HttpClient::ResponseHandler.get_body response end |
#patch(path, body = {}, options = {}) ⇒ Object
57 58 59 |
# File 'lib/buffer/http_client.rb', line 57 def patch(path, body = {}, = {}) request path, body, "patch", end |
#post(path, body = {}, options = {}) ⇒ Object
53 54 55 |
# File 'lib/buffer/http_client.rb', line 53 def post(path, body = {}, = {}) request path, body, "post", end |
#put(path, body = {}, options = {}) ⇒ Object
65 66 67 |
# File 'lib/buffer/http_client.rb', line 65 def put(path, body = {}, = {}) request path, body, "put", end |
#request(path, body, method, options) ⇒ Object
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
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/buffer/http_client.rb', line 74 def request(path, body, method, ) = @options.merge [:headers] = [:headers] || {} [:headers] = @headers.merge Hash[[:headers].map { |k, v| [k.downcase, v] }] [:body] = body if method != "get" [:body] = [:body] || {} = set_body end response = create_request method, path, body = get_body response Buffer::HttpClient::Response.new body, response.status, response.headers end |
#set_body(options) ⇒ Object
Set request body in correct format
121 122 123 |
# File 'lib/buffer/http_client.rb', line 121 def set_body() Buffer::HttpClient::RequestHandler.set_body end |