Sha256: 9b98750f683e2ba1fff76ca613c1b7d75eb0f27ca4b54d00f109e51c0dec4492

Contents?: true

Size: 1.58 KB

Versions: 1

Compression:

Stored size: 1.58 KB

Contents

# frozen_string_literal: true

module MLBStatsAPI
  class LiveFeed < Base
    attr_reader :id

    def initialize(api, data)
      super(data)

      @api = api

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

    def game_data = @data['gameData']

    def live_data = @data['liveData']

    def metadata = @data['metaData']

    def boxscore = @data.dig('liveData', 'boxscore')

    def decisions = @data.dig('liveData', 'decisions')

    def leaders = @data.dig('liveData', 'leaders')

    def linescore = @data.dig('liveData', 'linescore')

    def plays = @data.dig('liveData', 'plays')

    def timestamps = @api.live_feed_timestamps(@id)

    def reload!
      @data = @api.get("/game/#{@id}/feed/live", version: '1.1')

      true
    rescue Net::OpenTimeout
      false
    end

    def update!
      return reload! unless @data

      diffs = @api.live_feed_diff(
        @data['gamePk'],
        timecode: @data.dig('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

    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
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mlb_stats_api-0.4.0 lib/mlb_stats_api/live_feed.rb