Module: Football::Butler::Configuration
- Included in:
- Football::Butler
- Defined in:
- lib/football/butler/configuration.rb
Constant Summary collapse
- API_URL_FOOTBALL_DATA =
MULTI-API
'https://api.football-data.org'
- API_URL_APIFOOTBALL =
'https://apiv2.apifootball.com/?'
- API_URL_API_FOOTBALL =
'https://v3.football.api-sports.io'
- API_VERSION_FOOTBALL_DATA =
2
- API_VERSION_APIFOOTBALL =
2
- API_VERSION_API_FOOTBALL =
3
- AVAILABLE_APIS =
API
[:football_data_org, :apifootball_com, :api_football_com]
- DEFAULT_API_NAME =
:football_data_org
- DEFAULT_API_URL =
API_URL_FOOTBALL_DATA
- DEFAULT_API_TOKEN =
nil
- DEFAULT_API_VERSION =
API_VERSION_FOOTBALL_DATA
- DEFAULT_API_ENDPOINT =
"#{DEFAULT_API_URL}/v#{DEFAULT_API_VERSION}"
- DEFAULT_TIER_PLAN =
ADDITIONAL
nil
- DEFAULT_WAIT_ON_LIMIT =
false
- MSG_REACHED_LIMIT =
MESSAGES
{ football_data_org: 'You reached your request limit.', # code: 429 api_football_com: 'Too many requests. Your rate limit is 10 requests per minute.' # code: 200 }
Class Attribute Summary collapse
-
.api_endpoint ⇒ Object
Returns the value of attribute api_endpoint.
-
.api_name ⇒ Object
Returns the value of attribute api_name.
-
.api_token ⇒ Object
Returns the value of attribute api_token.
-
.api_version ⇒ Object
Returns the value of attribute api_version.
-
.header_additional ⇒ Object
Returns the value of attribute header_additional.
-
.header_token_name ⇒ Object
Returns the value of attribute header_token_name.
-
.init_done ⇒ Object
Returns the value of attribute init_done.
-
.tier_plan ⇒ Object
Returns the value of attribute tier_plan.
-
.wait_on_limit ⇒ Object
Returns the value of attribute wait_on_limit.
Class Method Summary collapse
-
.api_class ⇒ Object
- :football_data_org, :apifootball_com
- .api_name_valid?(api_name) ⇒ Boolean
- .api_result(klass) ⇒ Object
- .class_converter(klass) ⇒ Object
- .configure {|_self| ... } ⇒ Object
- .http_party_headers ⇒ Object
- .http_party_response(response, result) ⇒ Object
- .http_party_url(path) ⇒ Object
- .reached_limit?(response) ⇒ Boolean
- .reconfigure(api_token: nil, api_version: nil, api_endpoint: nil, tier_plan: nil, wait_on_limit: nil, api_name: nil, header_token_name: nil, header_additional: nil) ⇒ Object
- .reset ⇒ Object
- .response_api_football_com(response, result) ⇒ Object
- .response_apifootball_com(response, result) ⇒ Object
- .response_football_data_org(response, result) ⇒ Object
- .set_api_endpoint(api_name, api_version = DEFAULT_API_VERSION) ⇒ Object
- .set_api_name(api_name) ⇒ Object
- .set_header_token_name(api_name) ⇒ Object
- .set_wait_on_limit(wait_on_limit, api_name) ⇒ Object
- .tier_from_response(response) ⇒ Object
-
.tier_plan_filter ⇒ Object
plan = [ TIER_ONE | TIER_TWO | TIER_THREE | TIER_FOUR ].
Class Attribute Details
.api_endpoint ⇒ Object
Returns the value of attribute api_endpoint.
35 36 37 |
# File 'lib/football/butler/configuration.rb', line 35 def api_endpoint @api_endpoint end |
.api_name ⇒ Object
Returns the value of attribute api_name.
35 36 37 |
# File 'lib/football/butler/configuration.rb', line 35 def api_name @api_name end |
.api_token ⇒ Object
Returns the value of attribute api_token.
35 36 37 |
# File 'lib/football/butler/configuration.rb', line 35 def api_token @api_token end |
.api_version ⇒ Object
Returns the value of attribute api_version.
35 36 37 |
# File 'lib/football/butler/configuration.rb', line 35 def api_version @api_version end |
.header_additional ⇒ Object
Returns the value of attribute header_additional.
35 36 37 |
# File 'lib/football/butler/configuration.rb', line 35 def header_additional @header_additional end |
.header_token_name ⇒ Object
Returns the value of attribute header_token_name.
35 36 37 |
# File 'lib/football/butler/configuration.rb', line 35 def header_token_name @header_token_name end |
.init_done ⇒ Object
Returns the value of attribute init_done.
35 36 37 |
# File 'lib/football/butler/configuration.rb', line 35 def init_done @init_done end |
.tier_plan ⇒ Object
Returns the value of attribute tier_plan.
35 36 37 |
# File 'lib/football/butler/configuration.rb', line 35 def tier_plan @tier_plan end |
.wait_on_limit ⇒ Object
Returns the value of attribute wait_on_limit.
35 36 37 |
# File 'lib/football/butler/configuration.rb', line 35 def wait_on_limit @wait_on_limit end |
Class Method Details
.api_class ⇒ Object
- :football_data_org, :apifootball_com
117 118 119 120 121 122 123 124 125 126 |
# File 'lib/football/butler/configuration.rb', line 117 def api_class case api_name when :apifootball_com 'Apifootball' when :football_data_org 'FootballData' when :api_football_com 'ApiFootball' end end |
.api_name_valid?(api_name) ⇒ Boolean
90 91 92 |
# File 'lib/football/butler/configuration.rb', line 90 def api_name_valid?(api_name) AVAILABLE_APIS.include?(api_name) end |
.api_result(klass) ⇒ Object
238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/football/butler/configuration.rb', line 238 def api_result(klass) case api_name when :apifootball_com :parsed_response when :football_data_org klass::PATH when :api_football_com :response end rescue return nil end |
.class_converter(klass) ⇒ Object
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
# File 'lib/football/butler/configuration.rb', line 260 def class_converter(klass) case api_name when :apifootball_com case klass when 'Areas' return 'Countries' when 'Matches' return 'Events' when 'Fixtures' return 'Events' when 'Scorers' return 'TopScorers' when 'Leagues' return 'Competitions' end when :football_data_org case klass when 'Countries' return 'Areas' when 'Events' return 'Matches' when 'Fixtures' return 'Matches' when 'TopScorers' return 'Scorers' when 'Leagues' return 'Competitions' end when :api_football_com case klass when 'Areas' return 'Countries' when 'Competitions' return 'Leagues' when 'Events' return 'Fixtures' when 'Matches' return 'Fixtures' when 'Scorers' return 'TopScorers' end end klass end |
.configure {|_self| ... } ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/football/butler/configuration.rb', line 38 def configure raise "You need to configure football-butler first, see readme." unless block_given? yield self # api_name: AVAILABLE_APIS @api_name = set_api_name(self.api_name) @api_token ||= DEFAULT_API_TOKEN @api_version ||= DEFAULT_API_VERSION @api_endpoint ||= set_api_endpoint(@api_name, @api_version) @tier_plan ||= DEFAULT_TIER_PLAN @wait_on_limit ||= set_wait_on_limit(self.wait_on_limit, @api_name) @header_token_name ||= set_header_token_name(@api_name) @header_additional ||= {} @init_done = true api_name_valid?(self.api_name) end |
.http_party_headers ⇒ Object
168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/football/butler/configuration.rb', line 168 def http_party_headers result = case api_name when :apifootball_com {} when :football_data_org, :api_football_com { Configuration.header_token_name => Configuration.api_token } end result.merge!(Configuration.header_additional) result end |
.http_party_response(response, result) ⇒ Object
191 192 193 194 195 196 197 198 199 200 |
# File 'lib/football/butler/configuration.rb', line 191 def http_party_response(response, result) case api_name when :apifootball_com response_apifootball_com(response, result) when :football_data_org response_football_data_org(response, result) when :api_football_com response_api_football_com(response, result) end end |
.http_party_url(path) ⇒ Object
180 181 182 183 184 185 186 187 188 189 |
# File 'lib/football/butler/configuration.rb', line 180 def http_party_url(path) case api_name when :apifootball_com "#{Configuration.api_endpoint}#{path}&APIkey=#{Configuration.api_token}" when :football_data_org "#{Configuration.api_endpoint}/#{path}" when :api_football_com "#{Configuration.api_endpoint}/#{path}" end end |
.reached_limit?(response) ⇒ Boolean
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 |
# File 'lib/football/butler/configuration.rb', line 306 def reached_limit?(response) case api_name when :apifootball_com false when :football_data_org return false if !response.is_a?(Hash) && (response.respond_to?(:parsed_response) && !response.parsed_response.is_a?(Hash)) return response.dig('message') ? response['message'].start_with?(MSG_REACHED_LIMIT[api_name]) : false when :api_football_com if response.is_a?(HTTParty::Response) && response&.headers.present? if response.headers['x-ratelimit-remaining'] == '0' || response.headers['x-ratelimit-requests-remaining'] == '0' return true end elsif response.is_a?(HTTParty::Response) && response.parsed_response.dig('errors', 'rateLimit').present? return true end end false rescue return false end |
.reconfigure(api_token: nil, api_version: nil, api_endpoint: nil, tier_plan: nil, wait_on_limit: nil, api_name: nil, header_token_name: nil, header_additional: nil) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/football/butler/configuration.rb', line 60 def reconfigure( api_token: nil, api_version: nil, api_endpoint: nil, tier_plan: nil, wait_on_limit: nil, api_name: nil, header_token_name: nil, header_additional: nil ) reset unless @init_done @api_name = set_api_name(api_name) unless api_name.nil? @api_token = api_token if api_token if api_version @api_version = api_version @api_endpoint = set_api_endpoint(@api_name, @api_version) if api_endpoint.nil? end if api_endpoint.nil? @api_endpoint = set_api_endpoint(@api_name, @api_version) if api_name else @api_endpoint = api_endpoint end @tier_plan = tier_plan if tier_plan @wait_on_limit = set_wait_on_limit(wait_on_limit, @api_name) unless wait_on_limit.nil? @header_token_name = header_token_name ? header_token_name : set_header_token_name(@api_name) @header_additional = header_additional if header_additional api_name_valid?(api_name ? api_name : @api_name) end |
.reset ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/football/butler/configuration.rb', line 94 def reset @api_name = DEFAULT_API_NAME @api_version = DEFAULT_API_VERSION @api_endpoint = DEFAULT_API_ENDPOINT @tier_plan = DEFAULT_TIER_PLAN @wait_on_limit = DEFAULT_WAIT_ON_LIMIT @header_token_name = set_header_token_name(@api_name) @header_additional = {} @init_done = true true end |
.response_api_football_com(response, result) ⇒ Object
224 225 226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/football/butler/configuration.rb', line 224 def response_api_football_com(response, result) case result when :default response when :parsed_response response.parsed_response when :array_first response&.keys&.include?('response') && response['response'].is_a?(Array) ? response['response'].first : nil else response&.keys&.include?('response') ? response['response'] : nil end end |
.response_apifootball_com(response, result) ⇒ Object
202 203 204 205 206 207 208 209 210 211 |
# File 'lib/football/butler/configuration.rb', line 202 def response_apifootball_com(response, result) case result when :default response when :array_first response.parsed_response.is_a?(Array) ? response.parsed_response.first : nil else response.parsed_response end end |
.response_football_data_org(response, result) ⇒ Object
213 214 215 216 217 218 219 220 221 222 |
# File 'lib/football/butler/configuration.rb', line 213 def response_football_data_org(response, result) case result when :default response when :parsed_response response.parsed_response else response&.keys&.include?(result.to_s) ? response[result.to_s] : nil end end |
.set_api_endpoint(api_name, api_version = DEFAULT_API_VERSION) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/football/butler/configuration.rb', line 132 def set_api_endpoint(api_name, api_version = DEFAULT_API_VERSION) return if api_name.nil? case api_name when :apifootball_com API_URL_APIFOOTBALL when :football_data_org "#{API_URL_FOOTBALL_DATA}/v#{api_version}" when :api_football_com API_URL_API_FOOTBALL end end |
.set_api_name(api_name) ⇒ Object
128 129 130 |
# File 'lib/football/butler/configuration.rb', line 128 def set_api_name(api_name) api_name_valid?(api_name) ? api_name : DEFAULT_API_NAME end |
.set_header_token_name(api_name) ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/football/butler/configuration.rb', line 156 def set_header_token_name(api_name) case api_name when :apifootball_com # not used in header nil when :football_data_org "X-Auth-Token" when :api_football_com "x-apisports-key" end end |
.set_wait_on_limit(wait_on_limit, api_name) ⇒ Object
145 146 147 148 149 150 151 152 153 154 |
# File 'lib/football/butler/configuration.rb', line 145 def set_wait_on_limit(wait_on_limit, api_name) case api_name when :apifootball_com false when :football_data_org wait_on_limit when :api_football_com wait_on_limit end end |
.tier_from_response(response) ⇒ Object
251 252 253 254 255 256 257 258 |
# File 'lib/football/butler/configuration.rb', line 251 def tier_from_response(response) case api_name when :apifootball_com # n/a when :football_data_org, :api_football_com Tier.set_from_response_headers(response) end end |
.tier_plan_filter ⇒ Object
plan = [ TIER_ONE | TIER_TWO | TIER_THREE | TIER_FOUR ]
110 111 112 |
# File 'lib/football/butler/configuration.rb', line 110 def tier_plan_filter tier_plan.nil? ? {} : { plan: tier_plan } end |