class TowerBridgeLifts::Base
Constants
- ALLOWED_COMMANDS
- EXPIRE_TIME
- LIFTS_URL
- T_CLEAR_DOWN
- T_CLEAR_UP
Lift
timings (seconds)- T_FULL_LIFT
- T_MOVE_DOWN
- T_MOVE_UP
- T_UP
Attributes
error[RW]
lifts[RW]
updated[RW]
Public Class Methods
new()
click to toggle source
# File lib/tower_bridge_lifts/base.rb, line 23 def initialize @tz = TZInfo::Timezone.get('Europe/London') @lifts = [] @error = nil @updated = nil fetch end
Public Instance Methods
bascules()
click to toggle source
# File lib/tower_bridge_lifts/base.rb, line 74 def bascules return :down unless next_lift t_start = next_lift.timestamp t_end = t_start + T_FULL_LIFT t_now = time case when t_now < t_start + T_CLEAR_UP then :down when t_now >= t_end - T_CLEAR_DOWN then :down when t_now >= t_end - T_CLEAR_DOWN - T_MOVE_DOWN then :moving_down when t_now >= t_end - T_CLEAR_DOWN - T_MOVE_DOWN - T_UP then :up when t_now >= t_end - T_CLEAR_DOWN - T_MOVE_DOWN - T_UP - T_MOVE_UP then :moving_up end end
expired?()
click to toggle source
# File lib/tower_bridge_lifts/base.rb, line 50 def expired? Time.now > ( @updated + EXPIRE_TIME) end
fetch()
click to toggle source
# File lib/tower_bridge_lifts/base.rb, line 31 def fetch @lifts = [] error = nil page = Nokogiri::HTML(open(LIFTS_URL)) page.css(('table tbody tr')).each do |line| td = line.css('td').map{|td| td.text()} @lifts << Lift.new( timestamp: Time.parse("#{td[1]} #{year?(td[1])} #{td[2]}"), vessel: td[3], direction: { "Up river" => :up_river, "Down river" => :down_river }[td[4]] ) end rescue error = "Unable to fetch data: #{$!}" ensure @error = error @updated = Time.now end
next_lift()
click to toggle source
# File lib/tower_bridge_lifts/base.rb, line 69 def next_lift return if @lifts.empty? @lifts.find{ |lift| (lift.timestamp + T_FULL_LIFT) >= time } end
status()
click to toggle source
# File lib/tower_bridge_lifts/base.rb, line 54 def status { time: time, lifts_count: @lifts.count, next_lift: next_lift, bascules: bascules, traffic: traffic, updated: @updated, } end
time()
click to toggle source
# File lib/tower_bridge_lifts/base.rb, line 65 def time @tz.now end
traffic()
click to toggle source
# File lib/tower_bridge_lifts/base.rb, line 88 def traffic return :allowed unless next_lift ( time < next_lift.timestamp ) ? :allowed : :blocked end
Private Instance Methods
month_i(ts)
click to toggle source
# File lib/tower_bridge_lifts/base.rb, line 110 def month_i(ts) ts.strftime('%m').to_i end
year?(str)
click to toggle source
# File lib/tower_bridge_lifts/base.rb, line 104 def year?(str) date = Time.parse(str) now = time month_i(date) >= month_i(now) ? year_i(now) : (year_i(now) + 1) end
year_i(ts)
click to toggle source
# File lib/tower_bridge_lifts/base.rb, line 114 def year_i(ts) ts.strftime('%Y').to_i end