football-butler 2.0.0 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (57) hide show
  1. checksums.yaml +4 -4
  2. data/lib/football/butler/api.rb +2 -0
  3. data/lib/football/butler/api_football/base_api_football.rb +17 -0
  4. data/lib/football/butler/api_football/coachs.rb +46 -0
  5. data/lib/football/butler/api_football/countries.rb +39 -0
  6. data/lib/football/butler/api_football/fixtures.rb +80 -0
  7. data/lib/football/butler/api_football/head_to_head.rb +56 -0
  8. data/lib/football/butler/api_football/injuries.rb +62 -0
  9. data/lib/football/butler/api_football/leagues.rb +101 -0
  10. data/lib/football/butler/api_football/lineups.rb +39 -0
  11. data/lib/football/butler/api_football/odds.rb +55 -0
  12. data/lib/football/butler/api_football/players.rb +47 -0
  13. data/lib/football/butler/api_football/predictions.rb +27 -0
  14. data/lib/football/butler/api_football/sidelineds.rb +37 -0
  15. data/lib/football/butler/api_football/standings.rb +35 -0
  16. data/lib/football/butler/api_football/statistics.rb +22 -0
  17. data/lib/football/butler/api_football/teams.rb +66 -0
  18. data/lib/football/butler/api_football/timezones.rb +21 -0
  19. data/lib/football/butler/api_football/top_scorers.rb +22 -0
  20. data/lib/football/butler/api_football/transfers.rb +36 -0
  21. data/lib/football/butler/api_football/trophies.rb +37 -0
  22. data/lib/football/butler/api_football/venues.rb +66 -0
  23. data/lib/football/butler/apifootball/competitions.rb +2 -2
  24. data/lib/football/butler/apifootball/odds.rb +2 -2
  25. data/lib/football/butler/areas.rb +11 -2
  26. data/lib/football/butler/base.rb +2 -5
  27. data/lib/football/butler/coachs.rb +26 -0
  28. data/lib/football/butler/competitions.rb +24 -2
  29. data/lib/football/butler/configuration.rb +123 -17
  30. data/lib/football/butler/countries.rb +1 -0
  31. data/lib/football/butler/events.rb +1 -0
  32. data/lib/football/butler/fixtures.rb +11 -0
  33. data/lib/football/butler/football_data/areas.rb +1 -1
  34. data/lib/football/butler/football_data/odds.rb +1 -1
  35. data/lib/football/butler/football_data/teams.rb +1 -1
  36. data/lib/football/butler/head_to_head.rb +1 -0
  37. data/lib/football/butler/injuries.rb +31 -0
  38. data/lib/football/butler/leagues.rb +11 -0
  39. data/lib/football/butler/lineups.rb +1 -0
  40. data/lib/football/butler/matches.rb +1 -0
  41. data/lib/football/butler/odds.rb +3 -2
  42. data/lib/football/butler/players.rb +5 -0
  43. data/lib/football/butler/predictions.rb +1 -0
  44. data/lib/football/butler/scorers.rb +5 -0
  45. data/lib/football/butler/sidelineds.rb +20 -0
  46. data/lib/football/butler/standings.rb +1 -0
  47. data/lib/football/butler/statistics.rb +1 -0
  48. data/lib/football/butler/teams.rb +9 -0
  49. data/lib/football/butler/tier.rb +26 -11
  50. data/lib/football/butler/timezones.rb +16 -0
  51. data/lib/football/butler/top_scorers.rb +1 -0
  52. data/lib/football/butler/transfers.rb +21 -0
  53. data/lib/football/butler/trophies.rb +21 -0
  54. data/lib/football/butler/venues.rb +34 -0
  55. data/lib/football/butler/version.rb +6 -3
  56. data/lib/football/butler.rb +9 -0
  57. metadata +33 -3
@@ -10,9 +10,9 @@ module Football
10
10
  class << self
11
11
  ## COMPETITION
12
12
  # action=get_leagues&country_id={id}
13
- def by_country(id:)
13
+ def by_country(id:, result:)
14
14
  filters = { country_id: id }
15
- Api.get(path: build_path(PATH), filters: filters)
15
+ Api.get(path: build_path(PATH), filters: filters, result: result)
16
16
  end
17
17
 
18
18
  ## COMPETITIONS
@@ -14,9 +14,9 @@ module Football
14
14
  # to Stop date (yyyy-mm-dd)
15
15
  #
16
16
  # action=get_odds&match_id={id}&from={from}&to={to}
17
- def by_match(id:, from:, to:)
17
+ def by_match(id:, from:, to:, result:)
18
18
  filters = { match_id: id, from: from, to: to }
19
- Api.get(path: build_path(PATH), filters: filters, result: :parsed_response)
19
+ Api.get(path: build_path(PATH), filters: filters, result: result)
20
20
  end
21
21
  end
22
22
  end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
  require 'football/butler/apifootball/countries'
3
3
  require 'football/butler/football_data/areas'
4
+ require 'football/butler/api_football/countries'
4
5
 
5
6
  module Football
6
7
  module Butler
@@ -18,8 +19,16 @@ module Football
18
19
  end
19
20
 
20
21
  ## ADDITIONAL
21
- def by_name(name:)
22
- api_switch_method(__method__, { name: name })
22
+ def by_name(name:, result: api_switch_result, filters: {})
23
+ api_switch_method(__method__, { name: name, result: result, filters: filters })
24
+ end
25
+
26
+ def by_code(code:, result: api_switch_result, filters: {})
27
+ api_switch_method(__method__, { code: code, result: result, filters: filters })
28
+ end
29
+
30
+ def search_by_name(name:, result: api_switch_result, filters: {})
31
+ api_switch_method(__method__, { name: name, result: result, filters: filters })
23
32
  end
24
33
  end
25
34
  end
@@ -3,17 +3,13 @@
3
3
  module Football
4
4
  module Butler
5
5
  class Base
6
- MSG_REACHED_LIMIT = 'You reached your request limit.' # code: 429
7
6
  MSG_INVALID_CONFIG = 'Invalid Configuration, check empty api_token or empty / invalid api_endpoint!'
8
7
 
9
8
  class << self
10
9
 
11
10
  # MESSAGES
12
11
  def reached_limit?(response)
13
- return false if !response.is_a?(Hash) &&
14
- (response.respond_to?(:parsed_response) &&
15
- !response.parsed_response.is_a?(Hash))
16
- response.dig('message') ? response['message'].start_with?(MSG_REACHED_LIMIT) : false
12
+ Configuration.reached_limit?(response)
17
13
  end
18
14
 
19
15
  # RESULT MESSAGES
@@ -42,6 +38,7 @@ module Football
42
38
  # MULTI-API
43
39
  def api_switch_method(method, named_params)
44
40
  klass = api_switch
41
+
45
42
  if klass
46
43
  klass.respond_to?(method) ?
47
44
  klass.send(method, **named_params) :
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+ require 'football/butler/api_football/coachs'
3
+
4
+ module Football
5
+ module Butler
6
+ class Coachs < Base
7
+
8
+ class << self
9
+ ## COACHS
10
+ #
11
+ def by_id(id:, result: api_switch_result, filters: {})
12
+ api_switch_method(__method__, { id: id, result: result, filters: filters })
13
+ end
14
+
15
+ def search_by_name(name:, result: api_switch_result, filters: {})
16
+ api_switch_method(__method__, { name: name, result: result, filters: filters })
17
+ end
18
+
19
+ def by_team(team:, result: api_switch_result, filters: {})
20
+ api_switch_method(__method__, { team: team, result: result, filters: filters })
21
+ end
22
+
23
+ end
24
+ end
25
+ end
26
+ end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
  require 'football/butler/apifootball/competitions'
3
3
  require 'football/butler/football_data/competitions'
4
+ require 'football/butler/api_football/leagues'
4
5
 
5
6
  module Football
6
7
  module Butler
@@ -12,8 +13,12 @@ module Football
12
13
  api_switch_method(__method__, { id: id })
13
14
  end
14
15
 
15
- def by_country(id:)
16
- api_switch_method(__method__, { id: id })
16
+ def by_country(id:, result: api_switch_result)
17
+ api_switch_method(__method__, { id: id, result: result })
18
+ end
19
+
20
+ def by_country_name(name:, result: api_switch_result)
21
+ api_switch_method(__method__, { name: name, result: result })
17
22
  end
18
23
 
19
24
  ## COMPETITIONS
@@ -42,6 +47,23 @@ module Football
42
47
  def seasons(id:)
43
48
  api_switch_method(__method__, { id: id })
44
49
  end
50
+
51
+ def all_seasons(result: api_switch_result)
52
+ api_switch_method(__method__, { result: result })
53
+ end
54
+
55
+ def search_by_name(name:, result: api_switch_result, filters: {})
56
+ api_switch_method(__method__, { name: name, result: result, filters: filters })
57
+ end
58
+
59
+ def all_leagues(result: api_switch_result, filters: {})
60
+ api_switch_method(__method__, { result: result, filters: filters })
61
+ end
62
+
63
+ # /leagues?type=cup
64
+ def all_cups(result: api_switch_result, filters: {})
65
+ api_switch_method(__method__, { result: result, filters: filters })
66
+ end
45
67
  end
46
68
 
47
69
  end
@@ -4,14 +4,16 @@ module Football
4
4
  module Butler
5
5
  module Configuration
6
6
  # MULTI-API
7
- API_URL_FOOTBALL_DATA = 'https://api.football-data.org'
8
- API_URL_APIFOOTBALL = 'https://apiv2.apifootball.com/?'
7
+ API_URL_FOOTBALL_DATA = 'https://api.football-data.org'
8
+ API_URL_APIFOOTBALL = 'https://apiv2.apifootball.com/?'
9
+ API_URL_API_FOOTBALL = 'https://v3.football.api-sports.io'
9
10
 
10
11
  API_VERSION_FOOTBALL_DATA = 2
11
12
  API_VERSION_APIFOOTBALL = 2
13
+ API_VERSION_API_FOOTBALL = 3
12
14
 
13
15
  # API
14
- AVAILABLE_APIS = [:football_data_org, :apifootball_com]
16
+ AVAILABLE_APIS = [:football_data_org, :apifootball_com, :api_football_com]
15
17
  DEFAULT_API_NAME = :football_data_org
16
18
  DEFAULT_API_URL = API_URL_FOOTBALL_DATA
17
19
 
@@ -23,9 +25,15 @@ module Football
23
25
  DEFAULT_TIER_PLAN = nil
24
26
  DEFAULT_WAIT_ON_LIMIT = false
25
27
 
28
+ # MESSAGES
29
+ MSG_REACHED_LIMIT = {
30
+ football_data_org: 'You reached your request limit.', # code: 429
31
+ api_football_com: 'Too many requests. Your rate limit is 10 requests per minute.' # code: 200
32
+ }
33
+
26
34
  class << self
27
35
  attr_accessor :api_version, :api_token, :api_endpoint, :tier_plan, :wait_on_limit, :init_done,
28
- :api_name
36
+ :api_name, :header_token_name, :header_additional
29
37
 
30
38
  def configure
31
39
  raise "You need to configure football-butler first, see readme." unless block_given?
@@ -41,6 +49,9 @@ module Football
41
49
  @tier_plan ||= DEFAULT_TIER_PLAN
42
50
  @wait_on_limit ||= set_wait_on_limit(self.wait_on_limit, @api_name)
43
51
 
52
+ @header_token_name ||= set_header_token_name(@api_name)
53
+ @header_additional ||= {}
54
+
44
55
  @init_done = true
45
56
 
46
57
  api_name_valid?(self.api_name)
@@ -48,15 +59,15 @@ module Football
48
59
 
49
60
  def reconfigure(
50
61
  api_token: nil, api_version: nil, api_endpoint: nil, tier_plan: nil, wait_on_limit: nil,
51
- api_name: nil
62
+ api_name: nil, header_token_name: nil, header_additional: nil
52
63
  )
53
64
 
54
65
  reset unless @init_done
55
66
 
56
67
  @api_name = set_api_name(api_name) unless api_name.nil?
57
- @api_token = api_token unless api_token.nil?
68
+ @api_token = api_token if api_token
58
69
 
59
- unless api_version.nil?
70
+ if api_version
60
71
  @api_version = api_version
61
72
  @api_endpoint = set_api_endpoint(@api_name, @api_version) if api_endpoint.nil?
62
73
  end
@@ -67,9 +78,12 @@ module Football
67
78
  @api_endpoint = api_endpoint
68
79
  end
69
80
 
70
- @tier_plan = tier_plan unless tier_plan.nil?
81
+ @tier_plan = tier_plan if tier_plan
71
82
  @wait_on_limit = set_wait_on_limit(wait_on_limit, @api_name) unless wait_on_limit.nil?
72
83
 
84
+ @header_token_name = header_token_name ? header_token_name : set_header_token_name(@api_name)
85
+ @header_additional = header_additional if header_additional
86
+
73
87
  api_name_valid?(api_name ? api_name : @api_name)
74
88
  end
75
89
 
@@ -84,6 +98,9 @@ module Football
84
98
  @tier_plan = DEFAULT_TIER_PLAN
85
99
  @wait_on_limit = DEFAULT_WAIT_ON_LIMIT
86
100
 
101
+ @header_token_name = set_header_token_name(@api_name)
102
+ @header_additional = {}
103
+
87
104
  @init_done = true
88
105
 
89
106
  true
@@ -103,6 +120,8 @@ module Football
103
120
  'Apifootball'
104
121
  when :football_data_org
105
122
  'FootballData'
123
+ when :api_football_com
124
+ 'ApiFootball'
106
125
  end
107
126
  end
108
127
 
@@ -118,6 +137,8 @@ module Football
118
137
  API_URL_APIFOOTBALL
119
138
  when :football_data_org
120
139
  "#{API_URL_FOOTBALL_DATA}/v#{api_version}"
140
+ when :api_football_com
141
+ API_URL_API_FOOTBALL
121
142
  end
122
143
  end
123
144
 
@@ -127,24 +148,43 @@ module Football
127
148
  false
128
149
  when :football_data_org
129
150
  wait_on_limit
151
+ when :api_football_com
152
+ wait_on_limit
130
153
  end
131
154
  end
132
155
 
133
- def http_party_headers
156
+ def set_header_token_name(api_name)
134
157
  case api_name
135
158
  when :apifootball_com
136
- {}
159
+ # not used in header
160
+ nil
137
161
  when :football_data_org
138
- { "X-Auth-Token": Configuration.api_token }
162
+ "X-Auth-Token"
163
+ when :api_football_com
164
+ "x-apisports-key"
139
165
  end
140
166
  end
141
167
 
168
+ def http_party_headers
169
+ result = case api_name
170
+ when :apifootball_com
171
+ {}
172
+ when :football_data_org, :api_football_com
173
+ { Configuration.header_token_name => Configuration.api_token }
174
+ end
175
+
176
+ result.merge!(Configuration.header_additional)
177
+ result
178
+ end
179
+
142
180
  def http_party_url(path)
143
181
  case api_name
144
182
  when :apifootball_com
145
183
  "#{Configuration.api_endpoint}#{path}&APIkey=#{Configuration.api_token}"
146
184
  when :football_data_org
147
185
  "#{Configuration.api_endpoint}/#{path}"
186
+ when :api_football_com
187
+ "#{Configuration.api_endpoint}/#{path}"
148
188
  end
149
189
  end
150
190
 
@@ -154,6 +194,8 @@ module Football
154
194
  response_apifootball_com(response, result)
155
195
  when :football_data_org
156
196
  response_football_data_org(response, result)
197
+ when :api_football_com
198
+ response_api_football_com(response, result)
157
199
  end
158
200
  end
159
201
 
@@ -161,6 +203,8 @@ module Football
161
203
  case result
162
204
  when :default
163
205
  response
206
+ when :array_first
207
+ response.parsed_response.is_a?(Array) ? response.parsed_response.first : nil
164
208
  else
165
209
  response.parsed_response
166
210
  end
@@ -177,12 +221,17 @@ module Football
177
221
  end
178
222
  end
179
223
 
180
- def tier_from_response(response)
181
- case api_name
182
- when :apifootball_com
183
- # n/a
184
- when :football_data_org
185
- Tier.set_from_response_headers(response)
224
+ def response_api_football_com(response, result)
225
+ case result
226
+ when :default
227
+ response
228
+ when :parsed_response
229
+ response.parsed_response
230
+ when :array_first
231
+ response&.keys&.include?('response') && response['response'].is_a?(Array) ?
232
+ response['response'].first : nil
233
+ else
234
+ response&.keys&.include?('response') ? response['response'] : nil
186
235
  end
187
236
  end
188
237
 
@@ -192,11 +241,22 @@ module Football
192
241
  :parsed_response
193
242
  when :football_data_org
194
243
  klass::PATH
244
+ when :api_football_com
245
+ :response
195
246
  end
196
247
  rescue
197
248
  return nil
198
249
  end
199
250
 
251
+ def tier_from_response(response)
252
+ case api_name
253
+ when :apifootball_com
254
+ # n/a
255
+ when :football_data_org, :api_football_com
256
+ Tier.set_from_response_headers(response)
257
+ end
258
+ end
259
+
200
260
  def class_converter(klass)
201
261
  case api_name
202
262
  when :apifootball_com
@@ -205,8 +265,12 @@ module Football
205
265
  return 'Countries'
206
266
  when 'Matches'
207
267
  return 'Events'
268
+ when 'Fixtures'
269
+ return 'Events'
208
270
  when 'Scorers'
209
271
  return 'TopScorers'
272
+ when 'Leagues'
273
+ return 'Competitions'
210
274
  end
211
275
  when :football_data_org
212
276
  case klass
@@ -214,13 +278,55 @@ module Football
214
278
  return 'Areas'
215
279
  when 'Events'
216
280
  return 'Matches'
281
+ when 'Fixtures'
282
+ return 'Matches'
217
283
  when 'TopScorers'
218
284
  return 'Scorers'
285
+ when 'Leagues'
286
+ return 'Competitions'
287
+ end
288
+ when :api_football_com
289
+ case klass
290
+ when 'Areas'
291
+ return 'Countries'
292
+ when 'Competitions'
293
+ return 'Leagues'
294
+ when 'Events'
295
+ return 'Fixtures'
296
+ when 'Matches'
297
+ return 'Fixtures'
298
+ when 'Scorers'
299
+ return 'TopScorers'
219
300
  end
220
301
  end
221
302
 
222
303
  klass
223
304
  end
305
+
306
+ def reached_limit?(response)
307
+ case api_name
308
+ when :apifootball_com
309
+ false
310
+ when :football_data_org
311
+ return false if !response.is_a?(Hash) &&
312
+ (response.respond_to?(:parsed_response) &&
313
+ !response.parsed_response.is_a?(Hash))
314
+ return response.dig('message') ? response['message'].start_with?(MSG_REACHED_LIMIT[api_name]) : false
315
+ when :api_football_com
316
+ if response.is_a?(HTTParty::Response) && response&.headers.present?
317
+ if response.headers['x-ratelimit-remaining'] == '0' ||
318
+ response.headers['x-ratelimit-requests-remaining'] == '0'
319
+ return true
320
+ end
321
+ elsif response.is_a?(HTTParty::Response) && response.parsed_response.dig('errors', 'rateLimit').present?
322
+ return true
323
+ end
324
+ end
325
+
326
+ false
327
+ rescue
328
+ return false
329
+ end
224
330
  end
225
331
  end
226
332
  end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
  require 'football/butler/apifootball/countries'
3
3
  require 'football/butler/football_data/areas'
4
+ require 'football/butler/api_football/countries'
4
5
 
5
6
  module Football
6
7
  module Butler
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
  require 'football/butler/apifootball/events'
3
3
  require 'football/butler/football_data/matches'
4
+ require 'football/butler/api_football/fixtures'
4
5
 
5
6
  module Football
6
7
  module Butler