Class: MLBStatsAPI::LiveFeed
- Defined in:
- lib/mlb_stats_api/live_feed.rb
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
Attributes inherited from Base
Instance Method Summary collapse
- #boxscore ⇒ Object
- #decisions ⇒ Object
- #game_data ⇒ Object
-
#initialize(api, data) ⇒ LiveFeed
constructor
A new instance of LiveFeed.
- #leaders ⇒ Object
- #linescore ⇒ Object
- #live_data ⇒ Object
- #metadata ⇒ Object
- #plays ⇒ Object
- #process_diffs(diffs) ⇒ Object
- #reload! ⇒ Object
- #timestamps ⇒ Object
- #update! ⇒ Object
Methods inherited from Base
Constructor Details
#initialize(api, data) ⇒ LiveFeed
Returns a new instance of LiveFeed.
7 8 9 10 11 12 13 14 |
# File 'lib/mlb_stats_api/live_feed.rb', line 7 def initialize(api, data) super(data) @api = api # If we need to nuke and start over, keep this piece @id = data['gamePk'] end |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
5 6 7 |
# File 'lib/mlb_stats_api/live_feed.rb', line 5 def id @id end |
Instance Method Details
#boxscore ⇒ Object
22 |
# File 'lib/mlb_stats_api/live_feed.rb', line 22 def boxscore = @data.dig('liveData', 'boxscore') |
#decisions ⇒ Object
24 |
# File 'lib/mlb_stats_api/live_feed.rb', line 24 def decisions = @data.dig('liveData', 'decisions') |
#game_data ⇒ Object
16 |
# File 'lib/mlb_stats_api/live_feed.rb', line 16 def game_data = @data['gameData'] |
#leaders ⇒ Object
26 |
# File 'lib/mlb_stats_api/live_feed.rb', line 26 def leaders = @data.dig('liveData', 'leaders') |
#linescore ⇒ Object
28 |
# File 'lib/mlb_stats_api/live_feed.rb', line 28 def linescore = @data.dig('liveData', 'linescore') |
#live_data ⇒ Object
18 |
# File 'lib/mlb_stats_api/live_feed.rb', line 18 def live_data = @data['liveData'] |
#metadata ⇒ Object
20 |
# File 'lib/mlb_stats_api/live_feed.rb', line 20 def = @data['metaData'] |
#plays ⇒ Object
30 |
# File 'lib/mlb_stats_api/live_feed.rb', line 30 def plays = @data.dig('liveData', 'plays') |
#process_diffs(diffs) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/mlb_stats_api/live_feed.rb', line 60 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! ⇒ Object
34 35 36 37 38 39 40 |
# File 'lib/mlb_stats_api/live_feed.rb', line 34 def reload! @data = @api.get("/game/#{@id}/feed/live", version: '1.1') true rescue Net::OpenTimeout false end |
#timestamps ⇒ Object
32 |
# File 'lib/mlb_stats_api/live_feed.rb', line 32 def = @api.(@id) |
#update! ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/mlb_stats_api/live_feed.rb', line 42 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 |