Class: FootballApi::BaseRequest
- Inherits:
-
Object
- Object
- FootballApi::BaseRequest
- Includes:
- Symbolizer, HTTParty
- Defined in:
- lib/football_api/base_request.rb
Direct Known Subclasses
Constant Summary collapse
- RETRIES =
How many times should we retry the request if Timeout::Error is raised?
3
Constants included from Symbolizer
Symbolizer::HASH_OR_ARRAY_KEYS
Class Method Summary collapse
- .action_query(options = {}) ⇒ Object
-
.get!(options = {}, &block) ⇒ Object
(also: request!)
The generic get method for all requests.
- .get_parameters ⇒ Object
-
.response(options = {}) ⇒ Object
This is an handy method for response parsing.
Methods included from Symbolizer
Class Method Details
.action_query(options = {}) ⇒ Object
63 64 65 66 67 |
# File 'lib/football_api/base_request.rb', line 63 def action_query( = {}) { query: { "Action" => action }.merge() }.tap do |hs| hs[:query].merge!(get_parameters) if params_method end end |
.get!(options = {}, &block) ⇒ Object Also known as: request!
The generic get method for all requests. Uses httparty get and makes no assumptions about the response, simply returns it
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/football_api/base_request.rb', line 37 def get!( = {}, &block) attempts ||= RETRIES query = action_query() get("/api/", query, &block) rescue Timeout::Error => e puts "Timeout! Retrying... (#{RETRIES - attempts})" retry if (attempts -= 1) > 0 raise FootballApi::RequestError.new('Request timeout') end |
.get_parameters ⇒ Object
69 70 71 |
# File 'lib/football_api/base_request.rb', line 69 def get_parameters send(params_method) end |
.response(options = {}) ⇒ Object
This is an handy method for response parsing. Subclasses that include Requestable can simply use the json_key option and response will only contain that field. It also deep symbolizes the response keys
56 57 58 59 60 61 |
# File 'lib/football_api/base_request.rb', line 56 def response( = {}) data = get!() || Hash.new data = custom_deep_symbolic_hash(data) data.present? ? data[json_id] : data end |