Class: EspnPub::Entities::League
- Defined in:
- lib/espn_pub/entities/league.rb
Overview
Represents a sports league, eg. NBA, NFL, etc.
Defined Under Namespace
Modules: NAME
Constant Summary collapse
- TEAMS_PATH =
'/apis/site/%s/sports/%s/%s/teams'- GAMES_PATH =
'/apis/site/%s/sports/%s/%s/scoreboard'- NAME_TO_SPORT =
{ 'nba' => 'basketball', 'nfl' => 'football' }.freeze
Instance Attribute Summary collapse
-
#league_name ⇒ Object
readonly
Returns the value of attribute league_name.
Attributes inherited from Base
Class Method Summary collapse
Instance Method Summary collapse
-
#games(date: nil) ⇒ Array<EspnPub::Entities::Game>
Fetch games for this league.
-
#initialize(league_name) ⇒ League
constructor
Initialize a League instance.
-
#sport ⇒ String?
Return the sport name for this league.
-
#teams ⇒ Array<EspnPub::Entities::Team>
Fetch the teams for this league.
Constructor Details
#initialize(league_name) ⇒ League
Initialize a League instance.
46 47 48 49 |
# File 'lib/espn_pub/entities/league.rb', line 46 def initialize(league_name) @league_name = normalize_name(league_name) super() end |
Instance Attribute Details
#league_name ⇒ Object (readonly)
Returns the value of attribute league_name.
23 24 25 |
# File 'lib/espn_pub/entities/league.rb', line 23 def league_name @league_name end |
Class Method Details
.fetch_season_details(league_name, season_year) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/espn_pub/entities/league.rb', line 25 def self.fetch_season_details(league_name, season_year) raise ArgumentError, "Unknown league name: #{league_name}" unless NAME_TO_SPORT.key?(league_name) raise ArgumentError, 'Season year must be an integer' unless season_year.to_i.positive? path = format GAMES_PATH, EspnPub::Client::API_VERSION, NAME_TO_SPORT[league_name], league_name path += "?dates=#{Time.new(season_year).strftime('%Y%m%d')}" resp = EspnPub::Client.new.send_request(path) season = resp.dig('leagues', 0, 'season') return {} unless season { year: season['year'], start_date: Date.parse(season['startDate']), end_date: Date.parse(season['endDate']) } end |
Instance Method Details
#games(date: nil) ⇒ Array<EspnPub::Entities::Game>
Fetch games for this league.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/espn_pub/entities/league.rb', line 82 def games(date: nil) path = format GAMES_PATH, client.version, sport, league_name path += "?dates=#{date.strftime('%Y%m%d')}" if date games_resp = client.send_request(path) (games_resp['events'] || []).map do |game_data| EspnPub::Entities::Game.new( id: game_data['id'], type: Game::GAME_TYPES[game_data.dig('season', 'type')], home_team: EspnPub::Entities::Team.fetch_by_id( id: game_data.dig('competitions', 0, 'competitors', 0, 'id'), sport: sport, league_name: league_name ), away_team: EspnPub::Entities::Team.fetch_by_id( id: game_data.dig('competitions', 0, 'competitors', 1, 'id'), sport: sport, league_name: league_name ), date: DateTime.parse(game_data['date']) ) end rescue Client::UnexpectedResponseCodeError => e warn "Failed to fetch games for league #{league_name}: #{e.}" [] end |
#sport ⇒ String?
Return the sport name for this league.
111 112 113 |
# File 'lib/espn_pub/entities/league.rb', line 111 def sport NAME_TO_SPORT[league_name] end |
#teams ⇒ Array<EspnPub::Entities::Team>
Fetch the teams for this league.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/espn_pub/entities/league.rb', line 54 def teams unless defined?(@teams) begin path = format TEAMS_PATH, client.version, sport, league_name teams_resp = client.send_request(path) @teams = (teams_resp.dig('sports', 0, 'leagues', 0, 'teams') || []).map do |team_data| EspnPub::Entities::Team.new( id: team_data.dig('team', 'id'), name: team_data.dig('team', 'name'), location: team_data.dig('team', 'location'), abbreviation: team_data.dig('team', 'abbreviation'), sport: sport, league_name: league_name ) end rescue Client::UnexpectedResponseCodeError => e warn "Failed to fetch teams for league #{league_name}: #{e.}" return [] end end @teams end |