Class: Sportradar::Api::Baseball::Mlb

Inherits:
Data
  • Object
show all
Defined in:
lib/sportradar/api/baseball/mlb.rb,
lib/sportradar/api/baseball/mlb/api.rb,
lib/sportradar/api/baseball/mlb/league.rb,
lib/sportradar/api/baseball/mlb/division.rb

Defined Under Namespace

Classes: Api, Division, League

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Data

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

Constructor Details

#initialize(data = {}, **opts) ⇒ Mlb

Returns a new instance of Mlb.



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/sportradar/api/baseball/mlb.rb', line 10

def initialize(data = {}, **opts)
  # @response = data
  @api      = opts[:api]
  @id       = data['id']
  @season   = opts[:year]
  @type     = opts[:type]

  @leagues_hash = {}
  @games_hash = {}
  @teams_hash = {}

  update(data, **opts)
end

Instance Attribute Details

#aliasObject

Returns the value of attribute alias.



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

def alias
  @alias
end

#idObject

Returns the value of attribute id.



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

def id
  @id
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#responseObject

Returns the value of attribute response.



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

def response
  @response
end

#typeObject

Returns the value of attribute type.



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

def type
  @type
end

#yearObject

Returns the value of attribute year.



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

def year
  @year
end

Instance Method Details

#all_attributesObject



6
7
8
# File 'lib/sportradar/api/baseball/mlb.rb', line 6

def all_attributes
  [:name, :alias, :leagues, :divisions, :teams]
end

#apiObject

api stuff



92
93
94
# File 'lib/sportradar/api/baseball/mlb.rb', line 92

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

#daily_scheduleObject



58
59
60
# File 'lib/sportradar/api/baseball/mlb.rb', line 58

def daily_schedule
  # TODO
end

#default_dateObject



99
100
101
# File 'lib/sportradar/api/baseball/mlb.rb', line 99

def default_date
  Date.today
end

#default_seasonObject



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

def default_season
  'reg'
end

#default_yearObject



96
97
98
# File 'lib/sportradar/api/baseball/mlb.rb', line 96

def default_year
  Date.today.year
end

#divisionsObject



69
70
71
# File 'lib/sportradar/api/baseball/mlb.rb', line 69

def divisions
  leagues.flat_map(&:divisions)
end

#gamesObject



62
63
64
# File 'lib/sportradar/api/baseball/mlb.rb', line 62

def games
  @games_hash.values
end

#get_daily_summary(date = Date.today) ⇒ Object

daily summary



226
227
228
229
# File 'lib/sportradar/api/baseball/mlb.rb', line 226

def get_daily_summary(date = Date.today)
  data = api.get_data(path_daily_summary(date)).to_h
  ingest_daily_summary(data)
end

#get_depth_chartsObject

depth_charts



178
179
180
181
# File 'lib/sportradar/api/baseball/mlb.rb', line 178

def get_depth_charts
  data = api.get_data(path_depth_charts).to_h
  ingest_depth_charts(data)
end

#get_hierarchyObject

hierarchy



162
163
164
165
# File 'lib/sportradar/api/baseball/mlb.rb', line 162

def get_hierarchy
  data = api.get_data(path_hierarchy).to_h
  ingest_hierarchy(data)
end

#get_scheduleObject

schedule



146
147
148
149
# File 'lib/sportradar/api/baseball/mlb.rb', line 146

def get_schedule
  data = api.get_data(path_schedule).to_h
  ingest_schedule(data)
end

#get_standingsObject

standings



194
195
196
197
# File 'lib/sportradar/api/baseball/mlb.rb', line 194

def get_standings
  data = api.get_data(path_standings).to_h
  ingest_standings(data)
end

#hierarchyObject



53
54
55
56
# File 'lib/sportradar/api/baseball/mlb.rb', line 53

def hierarchy
  get_hierarchy if divisions.empty?
  self
end

#ingest_daily_summary(data) ⇒ Object



231
232
233
234
# File 'lib/sportradar/api/baseball/mlb.rb', line 231

def ingest_daily_summary(data)
  update(data.dig('league'), source: :games)
  data
end

#ingest_depth_charts(data) ⇒ Object



183
184
185
186
# File 'lib/sportradar/api/baseball/mlb.rb', line 183

def ingest_depth_charts(data)
  update(data, source: :teams)
  data
end

#ingest_hierarchy(data) ⇒ Object



167
168
169
170
# File 'lib/sportradar/api/baseball/mlb.rb', line 167

def ingest_hierarchy(data)
  update(data, source: :teams)
  data
end

#ingest_schedule(data) ⇒ Object



151
152
153
154
# File 'lib/sportradar/api/baseball/mlb.rb', line 151

def ingest_schedule(data)
  update(data, source: :games)
  data
end

#ingest_standings(data) ⇒ Object



199
200
201
202
# File 'lib/sportradar/api/baseball/mlb.rb', line 199

def ingest_standings(data)
  update(data.dig('league','season'), source: :teams)
  data
end

#leaguesObject



66
67
68
# File 'lib/sportradar/api/baseball/mlb.rb', line 66

def leagues
  @leagues_hash.values
end

#mlb_seasonObject



108
109
110
# File 'lib/sportradar/api/baseball/mlb.rb', line 108

def mlb_season
  @type || default_season
end

#path_baseObject

url paths



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

def path_base
  "league"
end

#path_daily_boxscore(date) ⇒ Object



138
139
140
# File 'lib/sportradar/api/baseball/mlb.rb', line 138

def path_daily_boxscore(date)
  "#{ path_base }/games/#{date.year}/#{date.month}/#{date.day}/boxscore"
end

#path_daily_summary(date) ⇒ Object



135
136
137
# File 'lib/sportradar/api/baseball/mlb.rb', line 135

def path_daily_summary(date)
  "games/#{date.year}/#{date.month}/#{date.day}/summary"
end

#path_depth_chartsObject



129
130
131
# File 'lib/sportradar/api/baseball/mlb.rb', line 129

def path_depth_charts
  "#{ path_base }/depth_charts"
end

#path_hierarchyObject



126
127
128
# File 'lib/sportradar/api/baseball/mlb.rb', line 126

def path_hierarchy
  "#{ path_base }/hierarchy"
end

#path_rankingsObject



123
124
125
# File 'lib/sportradar/api/baseball/mlb.rb', line 123

def path_rankings
  "seasontd/#{season_year}/#{mlb_season}/rankings"
end

#path_scheduleObject



117
118
119
# File 'lib/sportradar/api/baseball/mlb.rb', line 117

def path_schedule
  "games/#{season_year}/#{mlb_season}/schedule"
end

#path_seriesObject



120
121
122
# File 'lib/sportradar/api/baseball/mlb.rb', line 120

def path_series
  "series/#{season_year}/#{mlb_season}/schedule"
end

#path_standingsObject



132
133
134
# File 'lib/sportradar/api/baseball/mlb.rb', line 132

def path_standings
  "seasontd/#{season_year}/#{mlb_season}/standings"
end

#queue_daily_summary(date = Date.today) ⇒ Object



236
237
238
239
# File 'lib/sportradar/api/baseball/mlb.rb', line 236

def queue_daily_summary(date = Date.today)
  url, headers, options, timeout = api.get_request_info(path_daily_summary(date))
  {url: url, headers: headers, params: options, timeout: timeout, callback: method(:ingest_daily_summary)}
end

#queue_depth_chartsObject



188
189
190
191
# File 'lib/sportradar/api/baseball/mlb.rb', line 188

def queue_depth_charts
  url, headers, options, timeout = api.get_request_info(path_depth_charts)
  {url: url, headers: headers, params: options, timeout: timeout, callback: method(:ingest_depth_charts)}
end

#queue_hierarchyObject



172
173
174
175
# File 'lib/sportradar/api/baseball/mlb.rb', line 172

def queue_hierarchy
  url, headers, options, timeout = api.get_request_info(path_hierarchy)
  {url: url, headers: headers, params: options, timeout: timeout, callback: method(:ingest_hierarchy)}
end

#queue_scheduleObject



156
157
158
159
# File 'lib/sportradar/api/baseball/mlb.rb', line 156

def queue_schedule
  url, headers, options, timeout = api.get_request_info(path_schedule)
  {url: url, headers: headers, params: options, timeout: timeout, callback: method(:ingest_schedule)}
end

#queue_standingsObject



204
205
206
207
# File 'lib/sportradar/api/baseball/mlb.rb', line 204

def queue_standings
  url, headers, options, timeout = api.get_request_info(path_standings)
  {url: url, headers: headers, params: options, timeout: timeout, callback: method(:ingest_standings)}
end

#scheduleObject



43
44
45
46
# File 'lib/sportradar/api/baseball/mlb.rb', line 43

def schedule
  get_schedule if games.empty?
  self
end

#season_yearObject



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

def season_year
  @year || default_year
end

#standingsObject



48
49
50
51
# File 'lib/sportradar/api/baseball/mlb.rb', line 48

def standings
  get_standings if teams.first&.record.nil?
  self
end

#team(team_id) ⇒ Object



85
86
87
# File 'lib/sportradar/api/baseball/mlb.rb', line 85

def team(team_id)
  teams.detect { |team| team.id == team_id }
end

#teamsObject



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/sportradar/api/baseball/mlb.rb', line 72

def teams
  teams = divisions.flat_map(&:teams)
  if teams.empty?
    if @teams_hash.empty?
      get_hierarchy
      divisions.flat_map(&:teams)
    else
      @teams_hash.values
    end
  else
    teams
  end
end

#update(data, source: nil, **opts) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/sportradar/api/baseball/mlb.rb', line 24

def update(data, source: nil, **opts)
  # update stuff
  @id       = data.dig('league', 'id')    if data.dig('league', 'id')
  @name     = data.dig('league', 'name')  if data.dig('league', 'name')
  @alias    = data.dig('league', 'alias') if data.dig('league', 'alias')

  @year     = data.dig('season', 'year')  if data.dig('season', 'year')
  @type     = data.dig('season', 'type')  if data.dig('season', 'type')

  @leagues_hash = create_data({}, data['leagues'], klass: League, hierarchy: self, api: api) if data['leagues']
  @teams_hash   = create_data({}, data['teams'],   klass: Team,   hierarchy: self, api: api) if data['teams']
  if data['games'] && !data['games'].empty?
    if data['games'].first.keys == ['game']
      data['games'].map! { |hash| hash['game'] }
    end
    @games_hash = create_data({}, data['games'],   klass: Game,   hierarchy: self, api: api)
  end
end