Class: EspnPub::Entities::Team

Inherits:
Base
  • Object
show all
Defined in:
lib/espn_pub/entities/team.rb

Overview

Represents a sports team, eg. Miami Heat, New England Patriots, etc.

Constant Summary collapse

TEAM_PATH =
'/apis/site/%s/sports/%s/%s/teams/%s'
ROSTER_PATH =
'/apis/site/%s/sports/%s/%s/teams/%s/roster'

Instance Attribute Summary collapse

Attributes inherited from Base

#client

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, name:, location:, abbreviation:, sport:, league:) ⇒ Team

Initialize a Team entity.

Parameters:

  • id (String)

    The team identifier.

  • name (String)

    The team name.

  • location (String)

    The team location.

  • abbreviation (String)

    The team abbreviation.

  • sport (String)

    The sport name.

  • league (String)

    The league identifier.



26
27
28
29
30
31
32
33
34
# File 'lib/espn_pub/entities/team.rb', line 26

def initialize(id:, name:, location:, abbreviation:, sport:, league:)
  @id = id
  @name = name
  @location = location
  @abbreviation = abbreviation
  @sport = sport
  @league = league
  super()
end

Instance Attribute Details

#abbreviationObject (readonly)

Returns the value of attribute abbreviation.



11
12
13
# File 'lib/espn_pub/entities/team.rb', line 11

def abbreviation
  @abbreviation
end

#idObject (readonly)

Returns the value of attribute id.



11
12
13
# File 'lib/espn_pub/entities/team.rb', line 11

def id
  @id
end

#leagueObject (readonly)

Returns the value of attribute league.



11
12
13
# File 'lib/espn_pub/entities/team.rb', line 11

def league
  @league
end

#locationObject (readonly)

Returns the value of attribute location.



11
12
13
# File 'lib/espn_pub/entities/team.rb', line 11

def location
  @location
end

#nameObject (readonly)

Returns the value of attribute name.



11
12
13
# File 'lib/espn_pub/entities/team.rb', line 11

def name
  @name
end

#sportObject (readonly)

Returns the value of attribute sport.



11
12
13
# File 'lib/espn_pub/entities/team.rb', line 11

def sport
  @sport
end

Class Method Details

.fetch_by_id(id:, sport:, league:) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/espn_pub/entities/team.rb', line 36

def self.fetch_by_id(id:, sport:, league:)
  client = EspnPub::Client.new
  path = format TEAM_PATH, client.version, sport, league, id
  begin
    team_data = client.send_request(path).dig('team')
  rescue Client::UnexpectedResponseCodeError => e
    warn "Failed to fetch team data for #{id}: #{e.message}"
    return nil
  end

  return nil unless team_data

  new(
    id: team_data['id'],
    name: team_data['name'],
    location: team_data['location'],
    abbreviation: team_data['abbreviation'],
    sport: sport,
    league: league
  )
end

Instance Method Details

#full_nameString

Return the team’s full name.

Returns:

  • (String)

    The team’s full name.



89
90
91
# File 'lib/espn_pub/entities/team.rb', line 89

def full_name
  @full_name ||= "#{location} #{name}"
end

#playersArray<EspnPub::Entities::Player>

Fetch the roster for this team.

Returns:



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/espn_pub/entities/team.rb', line 61

def players
  unless defined?(@roster)
    begin
      path = format ROSTER_PATH, client.version, sport, league, id
      roster_resp = client.send_request(path)
      @roster = (roster_resp.dig('athletes') || []).map do |athlete_data|
        player = EspnPub::Entities::Player.new(
          id: athlete_data['id'],
          sport: sport,
          league: league,
          first_name: athlete_data['firstName'],
          last_name: athlete_data['lastName'],
          position: athlete_data['position']['abbreviation'],
          team_id: id
        )
      end
    rescue Client::UnexpectedResponseCodeError => e
      warn "Failed to fetch roster for team #{name} (#{id}): #{e.message}"
      return []
    end
  end

  @roster
end