Class: Sportradar::Api::Baseball::Team

Inherits:
Data
  • Object
show all
Defined in:
lib/sportradar/api/baseball/team.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Data

#all_attributes, #attributes, #create_data, #parse_into_array, #parse_into_array_with_options, #parse_out_hashes, #structure_links, #update_data

Constructor Details

#initialize(data, **opts) ⇒ Team

Returns a new instance of Team.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/sportradar/api/baseball/team.rb', line 7

def initialize(data, **opts)
  @response = data # comment this out when done developing
  @api      = opts[:api]
  @id = data['id']

  @games_hash   = {}
  @players_hash = {}
  @player_stats = {}
  @records      = {}
  @stats        = nil
  @runs         = nil

  update(data, **opts)
end

Instance Attribute Details

#aliasObject

Returns the value of attribute alias.



5
6
7
# File 'lib/sportradar/api/baseball/team.rb', line 5

def alias
  @alias
end

#full_nameObject

Returns the value of attribute full_name.



5
6
7
# File 'lib/sportradar/api/baseball/team.rb', line 5

def full_name
  @full_name
end

#idObject

Returns the value of attribute id.



5
6
7
# File 'lib/sportradar/api/baseball/team.rb', line 5

def id
  @id
end

#marketObject

Returns the value of attribute market.



5
6
7
# File 'lib/sportradar/api/baseball/team.rb', line 5

def market
  @market
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/sportradar/api/baseball/team.rb', line 5

def name
  @name
end

#player_statsObject

Returns the value of attribute player_stats.



5
6
7
# File 'lib/sportradar/api/baseball/team.rb', line 5

def player_stats
  @player_stats
end

#recordsObject

Returns the value of attribute records.



5
6
7
# File 'lib/sportradar/api/baseball/team.rb', line 5

def records
  @records
end

#responseObject

Returns the value of attribute response.



5
6
7
# File 'lib/sportradar/api/baseball/team.rb', line 5

def response
  @response
end

#team_statsObject

Returns the value of attribute team_stats.



5
6
7
# File 'lib/sportradar/api/baseball/team.rb', line 5

def team_stats
  @team_stats
end

#venueObject

Returns the value of attribute venue.



5
6
7
# File 'lib/sportradar/api/baseball/team.rb', line 5

def venue
  @venue
end

Instance Method Details

#add_game(game) ⇒ Object



67
68
69
# File 'lib/sportradar/api/baseball/team.rb', line 67

def add_game(game)
  @games_hash[game.id] = game.id if game
end

#apiObject



140
141
142
# File 'lib/sportradar/api/baseball/team.rb', line 140

def api
  @api || Sportradar::Api::Baseball::Mlb::Api.new
end

#gamesObject



64
65
66
# File 'lib/sportradar/api/baseball/team.rb', line 64

def games
  @games_hash.values
end

#get_rosterObject

data retrieval



101
102
103
104
# File 'lib/sportradar/api/baseball/team.rb', line 101

def get_roster
  data = api.get_data(path_roster).to_h
  ingest_roster(data)
end

#get_season_stats(year = Date.today.year) ⇒ Object



114
115
116
117
# File 'lib/sportradar/api/baseball/team.rb', line 114

def get_season_stats(year = Date.today.year)
  data = api.get_data(path_season_stats(year)).to_h
  ingest_season_stats(data)
end

#handle_names(data) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/sportradar/api/baseball/team.rb', line 49

def handle_names(data)
  # need to do some more work here
  @name = data['name'] if data['name']
  if data['name'] && data['market']
    @market = data['market']
    @full_name = [@market, data['name']].join(' ')
  elsif data['name'] && !data.key?('market')
    @full_name = data['name']
  end
end

#ingest_roster(data) ⇒ Object



105
106
107
108
# File 'lib/sportradar/api/baseball/team.rb', line 105

def ingest_roster(data)
  update(data)
  data
end

#ingest_season_stats(data) ⇒ Object



118
119
120
# File 'lib/sportradar/api/baseball/team.rb', line 118

def ingest_season_stats(data)
  parse_season_stats(data)
end

#parse_players(data, game) ⇒ Object

parsing response data



80
81
82
# File 'lib/sportradar/api/baseball/team.rb', line 80

def parse_players(data, game)
  create_data(@players_hash, data, klass: Player, api: api, team: self, game: game)
end

#parse_records(data) ⇒ Object



86
87
88
# File 'lib/sportradar/api/baseball/team.rb', line 86

def parse_records(data)
  @records['overall'] = Record.new(data, type: 'overall')
end

#parse_season_stats(data) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/sportradar/api/baseball/team.rb', line 90

def parse_season_stats(data)
  @team_stats = data.dig('statistics')
  update(data)
  player_data = data.dig('players')
  create_data(@players_hash, player_data, klass: Player, api: api, team: self)
  data
end

#path_baseObject

url path helpers



127
128
129
# File 'lib/sportradar/api/baseball/team.rb', line 127

def path_base
  "teams/#{ id }"
end

#path_base_stats(season_year = api.default_year, default_season = api.default_season) ⇒ Object



130
131
132
# File 'lib/sportradar/api/baseball/team.rb', line 130

def path_base_stats(season_year = api.default_year, default_season = api.default_season)
  "seasons/#{season_year}/#{default_season}/teams/#{id}"
end

#path_rosterObject



133
134
135
# File 'lib/sportradar/api/baseball/team.rb', line 133

def path_roster
  "#{ path_base }/profile"
end

#path_season_stats(year) ⇒ Object



136
137
138
# File 'lib/sportradar/api/baseball/team.rb', line 136

def path_season_stats(year)
  "#{ path_base_stats(year) }/statistics"
end

#playersObject Also known as: roster



71
72
73
74
# File 'lib/sportradar/api/baseball/team.rb', line 71

def players
  get_roster if @players_hash.empty?
  @players_hash.values
end

#queue_rosterObject



109
110
111
112
# File 'lib/sportradar/api/baseball/team.rb', line 109

def queue_roster
  url, headers, options, timeout = api.get_request_info(path_roster)
  {url: url, headers: headers, params: options, timeout: timeout, callback: method(:ingest_roster)}
end

#queue_season_stats(year = Date.today.year) ⇒ Object



121
122
123
124
# File 'lib/sportradar/api/baseball/team.rb', line 121

def queue_season_stats(year = Date.today.year)
  url, headers, options, timeout = api.get_request_info(path_season_stats(year))
  {url: url, headers: headers, params: options, timeout: timeout, callback: method(:ingest_season_stats)}
end

#record(type = 'overall') ⇒ Object



60
61
62
# File 'lib/sportradar/api/baseball/team.rb', line 60

def record(type = 'overall')
  @records[type]
end

#update(data, **opts) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/sportradar/api/baseball/team.rb', line 22

def update(data, **opts)
  handle_names(data)
  @venue    = Venue.new(data['venue']) if data['venue']

  @alias    = data['abbr']                if data['abbr']
  @runs     = data['runs'].to_i           if data['runs']
  # # @home     = data['home'] == 'true'        if data['home']
  # # @away     = data['away'] == 'true'        if data['away']
  # # @scoring  = data.dig('scoring', 'quarter') if data.dig('scoring', 'quarter')

  parse_records(data)                               if data['win']
  parse_players(data.dig('players'), opts[:game])   if data.dig('players')

  if opts[:game]
    # opts[:game].update_from_team(id, "runs"=>6)
    # opts[:game].update_from_team(id, "hits"=>11)
    # opts[:game].update_from_team(id, "errors"=>1)
    # opts[:game].update_from_team(id, "win"=>11)
    # opts[:game].update_from_team(id, "loss"=>16)
    # opts[:game].update_from_team(id, "probable_pitcher"=>{"jersey_number"=>"45", "id"=>"c1f19b5a-9dee-4053-9cad-ee4196f921e1", "win"=>2, "loss"=>3, "era"=>5.0})
    # opts[:game].update_from_team(id, "starting_pitcher"=>{"last_name"=>"Cotton", "first_name"=>"Jharel", "preferred_name"=>"Jharel", "jersey_number"=>"45"})
    # opts[:game].update_from_team(id, "current_pitcher"=>{"last_name"=>"Dull", "first_name"=>"Ryan", "preferred_name"=>"Ryan", "jersey_number"=>"66"})
    # add_game(opts[:game])
    # opts[:game].update_score(id => @runs)             if @runs
    opts[:game].update_stats(self, data['statistics'])  if data['statistics']
  end
end

#update_player_stats(player, stats, game = nil) ⇒ Object



83
84
85
# File 'lib/sportradar/api/baseball/team.rb', line 83

def update_player_stats(player, stats, game = nil)
  game ? game.update_player_stats(player, stats) : @player_stats.merge!(player.id => stats.merge(player: player))
end