Class: EspnPub::Entities::Team
- 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
-
#abbreviation ⇒ Object
readonly
Returns the value of attribute abbreviation.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#league_name ⇒ Object
readonly
Returns the value of attribute league_name.
-
#location ⇒ Object
readonly
Returns the value of attribute location.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#sport ⇒ Object
readonly
Returns the value of attribute sport.
-
#venue ⇒ Object
readonly
Returns the value of attribute venue.
Attributes inherited from Base
Class Method Summary collapse
Instance Method Summary collapse
-
#full_name ⇒ String
Return the team's full name.
-
#initialize(id:, name:, location:, abbreviation:, sport:, league_name:, venue: nil) ⇒ Team
constructor
Initialize a Team entity.
-
#players ⇒ Array<EspnPub::Entities::Player>
Fetch the roster for this team.
Constructor Details
#initialize(id:, name:, location:, abbreviation:, sport:, league_name:, venue: nil) ⇒ Team
Initialize a Team entity.
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/espn_pub/entities/team.rb', line 27 def initialize(id:, name:, location:, abbreviation:, sport:, league_name:, venue: nil) @id = id @name = name @location = location @abbreviation = abbreviation @sport = sport @league_name = league_name @venue = venue super() end |
Instance Attribute Details
#abbreviation ⇒ Object (readonly)
Returns the value of attribute abbreviation.
10 11 12 |
# File 'lib/espn_pub/entities/team.rb', line 10 def abbreviation @abbreviation end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
10 11 12 |
# File 'lib/espn_pub/entities/team.rb', line 10 def id @id end |
#league_name ⇒ Object (readonly)
Returns the value of attribute league_name.
10 11 12 |
# File 'lib/espn_pub/entities/team.rb', line 10 def league_name @league_name end |
#location ⇒ Object (readonly)
Returns the value of attribute location.
10 11 12 |
# File 'lib/espn_pub/entities/team.rb', line 10 def location @location end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
10 11 12 |
# File 'lib/espn_pub/entities/team.rb', line 10 def name @name end |
#sport ⇒ Object (readonly)
Returns the value of attribute sport.
10 11 12 |
# File 'lib/espn_pub/entities/team.rb', line 10 def sport @sport end |
#venue ⇒ Object (readonly)
Returns the value of attribute venue.
10 11 12 |
# File 'lib/espn_pub/entities/team.rb', line 10 def venue @venue end |
Class Method Details
.fetch_by_id(id:, sport:, league_name:, user_agent: nil) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/espn_pub/entities/team.rb', line 38 def self.fetch_by_id(id:, sport:, league_name:, user_agent: nil) client = EspnPub::Client.new client.user_agent = user_agent if user_agent path = format TEAM_PATH, client.version, sport, league_name, id begin team_data = client.send_request(path)['team'] rescue Client::UnexpectedResponseCodeError => e warn "Failed to fetch team data for #{id}: #{e.}" 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_name: league_name, venue: EspnPub::Entities::Venue.from_api(team_data.dig('franchise', 'venue')) ) end |
Instance Method Details
#full_name ⇒ String
Return the team's full name.
101 102 103 |
# File 'lib/espn_pub/entities/team.rb', line 101 def full_name @full_name ||= "#{location} #{name}" end |
#players ⇒ Array<EspnPub::Entities::Player>
Fetch the roster for this team.
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/espn_pub/entities/team.rb', line 66 def players unless defined?(@roster) begin path = format ROSTER_PATH, client.version, sport, league_name, id roster_resp = client.send_request(path) @roster = (roster_resp['athletes'] || []).map do |athlete_data| EspnPub::Entities::Player.new( id: athlete_data['id'], sport: sport, league_name: league_name, first_name: athlete_data['firstName'], last_name: athlete_data['lastName'], position: athlete_data['position']['abbreviation'], team_id: id, birth_city: athlete_data.dig('birthPlace', 'city'), birth_state: athlete_data.dig('birthPlace', 'state'), birth_country: athlete_data.dig('birthPlace', 'country'), date_of_birth: athlete_data['dateOfBirth'], height: athlete_data['height'], weight: athlete_data['weight'], debut_year: athlete_data['debutYear'] ) end rescue Client::UnexpectedResponseCodeError => e warn "Failed to fetch roster for team #{name} (#{id}): #{e.}" return [] end end @roster end |