module Feedlr::Request
Do all http requests and call the mapper
Constants
- API_VERSION
- ENDPOINT
- SANDBOX_ENDPOINT
Private Instance Methods
Run an HTTP request and map the response to a domain class @param method [String] HTTP method @param path [String] @param params [Hash] @param headers [Hash] @return [Faraday::Response]
# File lib/feedlr/request.rb, line 21 def build_object(method, path, params = nil, headers = nil) response = send(method, path, params, headers) Mapper.build(response.body) end
Initiate and memoize the HTTP connection object @return [Faraday::Connection]
# File lib/feedlr/request.rb, line 51 def connection @connection ||= Faraday.new(end_point, connection_options) end
Build and memoize the connection options @return [Hash]
# File lib/feedlr/request.rb, line 57 def connection_options @connection_options ||= { builder: middleware, headers: request_headers, request: { open_timeout: 10, timeout: 30 } } end
Build and memoize the endpoint @return [String]
# File lib/feedlr/request.rb, line 144 def end_point @end_point ||= (sandbox ? SANDBOX_ENDPOINT : ENDPOINT) end
Build and memoize the rack middleware for the requests @return [Faraday::RackBuilder]
# File lib/feedlr/request.rb, line 70 def middleware @middleware ||= Faraday::RackBuilder.new do |faraday| faraday.request :url_encoded # Add logging faraday.response(:logger, logger) unless logger.nil? # Parse XML faraday.response :xml, content_type: /\bxml$/ # Parse JSON faraday.response :json, content_type: /\bjson$/ faraday.adapter :net_http end end
Run the desired HTTP request, verifies it, raise excpetions in
case of failure, otherwise return the response
@param method [String] HTTP method @param path [String] @param headers [Hash] @return [Faraday::Response]
# File lib/feedlr/request.rb, line 90 def request(method, path, headers, &block) response = run_request(method, path, headers, &block) logger.debug(response.inspect) unless logger.nil? verify_success(response) response rescue Faraday::Error::TimeoutError, Timeout::Error => error raise(Feedlr::Error::RequestTimeout.new, error.message) rescue Faraday::Error::ClientError, JSON::ParserError => error raise(Feedlr::Error.new, error.message) end
Build and memoize the initial request headers @return [Hash]
# File lib/feedlr/request.rb, line 125 def request_headers return unless @headers.nil? @headers = { :"Accept" => 'application/json', :"Content-Type" => 'application/json', :user_agent => user_agent } @headers[:Authorization] = "OAuth #{oauth_access_token}" if oauth_access_token @headers end
Run the actual request @param method [String] HTTP method @param path [String] @param headers [Hash] @return [Faraday::Response]
# File lib/feedlr/request.rb, line 106 def run_request(method, path, headers) connection.send(method) do |request| request.url(API_VERSION + path) request.headers.update(headers) if headers yield(request) if block_given? end end
Build and memoize the user agent @return [String]
# File lib/feedlr/request.rb, line 138 def user_agent @user_agent ||= "Feedlr Ruby Gem #{Feedlr::Version}" end
Check the response code and raise exceptions if needed param response [Faraday::Response] @return [void]
# File lib/feedlr/request.rb, line 117 def verify_success(response) status_code = response.status.to_i klass = Feedlr::Error.errors[status_code] fail(klass.from_response(response)) if klass end