class MLBStatsAPI::LiveFeed

Attributes

id[R]

Public Class Methods

new(api, data) click to toggle source
# File lib/mlb_stats_api/live_feed.rb, line 7
def initialize(api, data)
  @api = api
  @data = data

  # If we need to nuke and start over, keep this piece
  @id = data['gamePk']
end

Public Instance Methods

boxscore() click to toggle source
# File lib/mlb_stats_api/live_feed.rb, line 15
def boxscore
  @data['liveData']['boxscore']
end
decisions() click to toggle source
# File lib/mlb_stats_api/live_feed.rb, line 19
def decisions
  @data['liveData']['decisions']
end
game_data() click to toggle source
# File lib/mlb_stats_api/live_feed.rb, line 35
def game_data
  @data['gameData']
end
leaders() click to toggle source
# File lib/mlb_stats_api/live_feed.rb, line 23
def leaders
  @data['liveData']['leaders']
end
linescore() click to toggle source
# File lib/mlb_stats_api/live_feed.rb, line 27
def linescore
  @data['liveData']['linescore']
end
live_data() click to toggle source
# File lib/mlb_stats_api/live_feed.rb, line 39
def live_data
  @data['liveData']
end
metadata() click to toggle source
# File lib/mlb_stats_api/live_feed.rb, line 43
def metadata
  @data['metaData']
end
plays() click to toggle source
# File lib/mlb_stats_api/live_feed.rb, line 31
def plays
  @data['liveData']['plays']
end
process_diffs(diffs) click to toggle source
# File lib/mlb_stats_api/live_feed.rb, line 77
def process_diffs(diffs)
  diffs.each do |diff_set|
    Hana::Patch.new(diff_set['diff']).apply(@data)
  end

  @api.logger&.info 'Successfully processed live feed diff'

  true
rescue Hana::Patch::Exception
  @api.logger&.info 'Failed to process live feed diff; nuking'

  # Nuke it!
  @data = nil

  reload!

  false
end
reload!() click to toggle source
# File lib/mlb_stats_api/live_feed.rb, line 51
def reload!
  @data = @api.get("/game/#{@id}/feed/live", version: '1.1')

  true
rescue Net::OpenTimeout
  false
end
timestamps() click to toggle source
# File lib/mlb_stats_api/live_feed.rb, line 47
def timestamps
  @api.live_feed_timestamps(@id)
end
update!() click to toggle source
# File lib/mlb_stats_api/live_feed.rb, line 59
def update!
  return reload! unless @data

  diffs = @api.live_feed_diff(
    @data['gamePk'],
    timecode: @data['metaData']['timeStamp']
  )

  return process_diffs(diffs) if diffs.is_a?(Array)

  # If the diff is too large or too old, a new feed is returned
  @data = diffs if diffs.is_a?(Hash)

  true
rescue Net::OpenTimeout
  false
end