Class: Sports::Butler::Configuration
- Inherits:
-
Object
- Object
- Sports::Butler::Configuration
- Defined in:
- lib/sports/butler/configuration.rb
Constant Summary collapse
- AVAILABLE_SPORTS =
Sports
[:soccer, :basketball]
- AVAILABLE_APIS =
APIs
[ # Soccer :football_data_org, :apifootball_com, :api_football_com, # Basketball :api_basketball_com ]
- AVAILABLE_SPORT_API =
{ soccer: [:football_data_org, :apifootball_com, :api_football_com], basketball: [:api_basketball_com] }
Class Attribute Summary collapse
-
.api_base_url ⇒ Object
Returns the value of attribute api_base_url.
-
.api_token ⇒ Object
Returns the value of attribute api_token.
-
.header_additional ⇒ Object
Returns the value of attribute header_additional.
-
.header_token_name ⇒ Object
Returns the value of attribute header_token_name.
Class Method Summary collapse
- .configure {|_self| ... } ⇒ Object
- .default_api_hash ⇒ Object
- .http_party_headers(sport, api_name) ⇒ Object
- .http_party_url(path, sport, api_name) ⇒ Object
- .http_party_url_suffix(url, sport, api_name) ⇒ Object
- .invalid_config?(sport, api_name) ⇒ Boolean
- .set_header_token_name ⇒ Object
- .valid_sport_api?(sport, api_name) ⇒ Boolean
Class Attribute Details
.api_base_url ⇒ Object
Returns the value of attribute api_base_url.
24 25 26 |
# File 'lib/sports/butler/configuration.rb', line 24 def api_base_url @api_base_url end |
.api_token ⇒ Object
Returns the value of attribute api_token.
24 25 26 |
# File 'lib/sports/butler/configuration.rb', line 24 def api_token @api_token end |
.header_additional ⇒ Object
Returns the value of attribute header_additional.
24 25 26 |
# File 'lib/sports/butler/configuration.rb', line 24 def header_additional @header_additional end |
.header_token_name ⇒ Object
Returns the value of attribute header_token_name.
24 25 26 |
# File 'lib/sports/butler/configuration.rb', line 24 def header_token_name @header_token_name end |
Class Method Details
.configure {|_self| ... } ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/sports/butler/configuration.rb', line 27 def configure raise "You need to configure sports-butler first, see readme." unless block_given? yield self @api_token ||= default_api_hash @api_base_url ||= default_api_hash @header_token_name ||= set_header_token_name @header_additional ||= {} end |
.default_api_hash ⇒ Object
40 41 42 |
# File 'lib/sports/butler/configuration.rb', line 40 def default_api_hash { soccer: {}, basketball: {} } end |
.http_party_headers(sport, api_name) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/sports/butler/configuration.rb', line 79 def http_party_headers(sport, api_name) result = case api_name # SOCCER when :apifootball_com {} when :football_data_org, :api_football_com { Configuration.header_token_name[sport][api_name] => Configuration.api_token[sport][api_name] } # BASKETBALL when :api_basketball_com { Configuration.header_token_name[sport][api_name] => Configuration.api_token[sport][api_name] } end result.merge!(Configuration.header_additional) result end |
.http_party_url(path, sport, api_name) ⇒ Object
61 62 63 64 65 66 67 68 |
# File 'lib/sports/butler/configuration.rb', line 61 def http_party_url(path, sport, api_name) case api_name when :apifootball_com "#{Configuration.api_base_url[sport][api_name]}?#{path}&APIkey=#{Configuration.api_token[sport][api_name]}" else "#{Configuration.api_base_url[sport][api_name]}/#{path}" end end |
.http_party_url_suffix(url, sport, api_name) ⇒ Object
70 71 72 73 74 75 76 77 |
# File 'lib/sports/butler/configuration.rb', line 70 def http_party_url_suffix(url, sport, api_name) case api_name when :apifootball_com "#{url}&APIkey=#{Configuration.api_token[sport][api_name]}" else "#{url}" end end |
.invalid_config?(sport, api_name) ⇒ Boolean
56 57 58 59 |
# File 'lib/sports/butler/configuration.rb', line 56 def invalid_config?(sport, api_name) Configuration.api_token[sport][api_name].blank? || !Configuration.valid_sport_api?(sport, api_name) end |
.set_header_token_name ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/sports/butler/configuration.rb', line 44 def set_header_token_name { soccer: { football_data_org: 'X-Auth-Token', api_football_com: 'x-apisports-key' }, basketball: { api_basketball_com: 'x-apisports-key' } } end |
.valid_sport_api?(sport, api_name) ⇒ Boolean
97 98 99 |
# File 'lib/sports/butler/configuration.rb', line 97 def valid_sport_api?(sport, api_name) AVAILABLE_SPORT_API[sport]&.any?{|name| name == api_name} end |