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
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Attributes inherited from Base
Instance Method Summary collapse
-
#games(date: nil) ⇒ Array<EspnPub::Entities::Game>
Fetch games for this league.
-
#initialize(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(name) ⇒ League
Initialize a League instance.
28 29 30 31 |
# File 'lib/espn_pub/entities/league.rb', line 28 def initialize(name) @name = name super() end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
23 24 25 |
# File 'lib/espn_pub/entities/league.rb', line 23 def name @name end |
Instance Method Details
#games(date: nil) ⇒ Array<EspnPub::Entities::Game>
Fetch games for this league.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/espn_pub/entities/league.rb', line 64 def games(date: nil) begin path = format GAMES_PATH, client.version, self.sport, name path += "?dates=#{date.strftime('%Y%m%d')}" if date games_resp = client.send_request(path) (games_resp.dig('events') || []).map do |game_data| EspnPub::Entities::Game.new( id: game_data['id'], home_team_id: game_data.dig('competitions', 0, 'competitors', 0, 'id'), away_team_id: game_data.dig('competitions', 0, 'competitors', 1, 'id'), date: DateTime.parse(game_data['date']) ) end rescue Client::UnexpectedResponseCodeError => e warn "Failed to fetch games for league #{name}: #{e.}" return [] end end |
#sport ⇒ String?
Return the sport name for this league.
86 87 88 |
# File 'lib/espn_pub/entities/league.rb', line 86 def sport NAME_TO_SPORT[name] end |
#teams ⇒ Array<EspnPub::Entities::Team>
Fetch the teams for this league.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/espn_pub/entities/league.rb', line 36 def teams unless defined?(@teams) begin path = format TEAMS_PATH, client.version, self.sport, 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 ) end rescue Client::UnexpectedResponseCodeError => e warn "Failed to fetch teams for league #{name}: #{e.}" return [] end end @teams end |