Class: FootballApi::BaseRequest

Inherits:
Object
  • Object
show all
Includes:
Symbolizer, HTTParty
Defined in:
lib/football_api/base_request.rb

Direct Known Subclasses

Comment, Commentary, Competition, Fixture, Match, Standing

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

Methods included from Symbolizer

included

Class Method Details

.action_query(options = {}) ⇒ Object



63
64
65
66
67
# File 'lib/football_api/base_request.rb', line 63

def action_query(options = {})
  { query: { "Action" => action }.merge(options) }.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!(options = {}, &block)
  attempts ||= RETRIES
  query = action_query(options)
  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_parametersObject



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(options = {})
  data = get!(options) || Hash.new

  data = custom_deep_symbolic_hash(data)
  data.present? ? data[json_id] : data
end