Class: Algolia::Http::HttpRequester
- Inherits:
-
Object
- Object
- Algolia::Http::HttpRequester
- Defined in:
- lib/algolia/transport/http/http_requester.rb
Instance Attribute Summary collapse
-
#adapter ⇒ Object
Returns the value of attribute adapter.
-
#logger ⇒ Object
Returns the value of attribute logger.
Instance Method Summary collapse
-
#build_url(host) ⇒ String
Build url from host, path and parameters.
-
#connection(host) ⇒ Faraday::Connection
Retrieve the connection from the @connections.
-
#handle_query_params(query_params) ⇒ Object
Convert query_params to a full query string.
-
#initialize(adapter, logger) ⇒ HttpRequester
constructor
A new instance of HttpRequester.
-
#send_request(host, method, path, body, query_params, headers, timeout, connect_timeout) ⇒ Http::Response
Sends request to the engine.
-
#to_query_string(query_params) ⇒ Object
Create a query string from query_params.
Constructor Details
#initialize(adapter, logger) ⇒ HttpRequester
Returns a new instance of HttpRequester.
10 11 12 13 14 |
# File 'lib/algolia/transport/http/http_requester.rb', line 10 def initialize(adapter, logger) @adapter = adapter @logger = logger @connections = {} end |
Instance Attribute Details
#adapter ⇒ Object
Returns the value of attribute adapter.
4 5 6 |
# File 'lib/algolia/transport/http/http_requester.rb', line 4 def adapter @adapter end |
#logger ⇒ Object
Returns the value of attribute logger.
4 5 6 |
# File 'lib/algolia/transport/http/http_requester.rb', line 4 def logger @logger end |
Instance Method Details
#build_url(host) ⇒ String
Build url from host, path and parameters
70 71 72 |
# File 'lib/algolia/transport/http/http_requester.rb', line 70 def build_url(host) host.protocol + host.url end |
#connection(host) ⇒ Faraday::Connection
Retrieve the connection from the @connections
58 59 60 61 62 |
# File 'lib/algolia/transport/http/http_requester.rb', line 58 def connection(host) @connections[host.url] ||= Faraday.new(build_url(host)) do |f| f.adapter @adapter.to_sym end end |
#handle_query_params(query_params) ⇒ Object
Convert query_params to a full query string
76 77 78 |
# File 'lib/algolia/transport/http/http_requester.rb', line 76 def handle_query_params(query_params) query_params.nil? || query_params.empty? ? '' : "?#{to_query_string(query_params)}" end |
#send_request(host, method, path, body, query_params, headers, timeout, connect_timeout) ⇒ Http::Response
Sends request to the engine
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/algolia/transport/http/http_requester.rb', line 27 def send_request(host, method, path, body, query_params, headers, timeout, connect_timeout) connection = connection(host) connection..timeout = timeout / 1000 connection..open_timeout = connect_timeout / 1000 path += handle_query_params(query_params) @logger.info("Sending #{method.to_s.upcase!} request to #{path} with body #{body}") if ENV['ALGOLIA_DEBUG'] response = connection.run_request(method, path, body, headers) if response.success? @logger.info("Request succeeded. Response status: #{response.status}, body: #{response.body}") if ENV['ALGOLIA_DEBUG'] return Http::Response.new(status: response.status, body: response.body, headers: response.headers) end @logger.info("Request failed. Response status: #{response.status}, error: #{response.body}") if ENV['ALGOLIA_DEBUG'] Http::Response.new(status: response.status, error: response.body, headers: response.headers) rescue Faraday::TimeoutError => e @logger.info("Request timed out. Error: #{e.}") if ENV['ALGOLIA_DEBUG'] Http::Response.new(error: e., has_timed_out: true) rescue ::StandardError => e @logger.info("Request failed. Error: #{e.}") if ENV['ALGOLIA_DEBUG'] Http::Response.new(error: e., network_failure: true) end |
#to_query_string(query_params) ⇒ Object
Create a query string from query_params
82 83 84 85 86 |
# File 'lib/algolia/transport/http/http_requester.rb', line 82 def to_query_string(query_params) query_params.map do |key, value| "#{key}=#{value}" end.join('&') end |