Class: Bart::Client::Estimates

Inherits:
API
  • Object
show all
Defined in:
lib/bart_api/client/estimates.rb

Constant Summary collapse

RESOLUTION =

seconds

30

Instance Method Summary collapse

Methods inherited from API

#get_request, include_api, #post_request, #refresh, require_all, singleton

Methods included from Bart::Connection

#adapter, #connection, #register_adapter

Instance Method Details

#get(stop_id, platform: nil, direction: nil) ⇒ Object Also known as: find

We need to memoize but never for longer than ‘RESOLUTION`, so rather than use Memoist this API maintains its own memo.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/bart_api/client/estimates.rb', line 11

def get stop_id, platform: nil, direction: nil
  @memo ||= {}
  model, stored_at = @memo[[stop_id, platform, direction]]
  return model if stored_at && 
    Time.now - Bart.configuration.refresh_time < stored_at

  parsed = get_request '/api/etd.aspx', query: { 
    cmd: :etd, 
    orig: stop_id,
    plat: platform,
    dir: direction
  }.select { |_,v| v != nil }

  parsed['root']['station'].tap do |station|
    station['etd'] = [station['etd']].flatten
    station['etd'].each do |etd|
      etd['estimate'] = [etd['estimate']].flatten
    end
  end

  model = Stop.new parsed['root']['station']
  @memo[[stop_id, platform, direction]] = [model, Time.now]
  model
end

#key(stop_id, platform, direction) ⇒ Object



5
6
7
# File 'lib/bart_api/client/estimates.rb', line 5

def key stop_id, platform, direction
  [stop_id, platform, direction]
end