Class: EspnPub::Entities::League

Inherits:
Base
  • Object
show all
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

Attributes inherited from Base

#client

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ League

Initialize a League instance.

Parameters:

  • name (String)

    The league identifier string.



28
29
30
31
# File 'lib/espn_pub/entities/league.rb', line 28

def initialize(name)
  @name = name
  super()
end

Instance Attribute Details

#nameObject (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.

Parameters:

  • date (Date, DateTime, nil) (defaults to: nil)

    An optional date to filter games.

Returns:



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.message}"
    return []
  end
end

#sportString?

Return the sport name for this league.

Returns:

  • (String, nil)

    The sport name or nil when unknown.



86
87
88
# File 'lib/espn_pub/entities/league.rb', line 86

def sport
  NAME_TO_SPORT[name]
end

#teamsArray<EspnPub::Entities::Team>

Fetch the teams for this league.

Returns:



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.message}"
      return []
    end
  end

  @teams
end