Class: NationalRail::VirginLiveDepartureBoards::TimeParser

Inherits:
Object
  • Object
show all
Defined in:
lib/national-rail/virgin_live_departure_boards.rb

Instance Method Summary collapse

Constructor Details

#initialize(time = Time.zone.now) ⇒ TimeParser

Returns a new instance of TimeParser.



16
17
18
# File 'lib/national-rail/virgin_live_departure_boards.rb', line 16

def initialize(time = Time.zone.now)
  @time = time
end

Instance Method Details

#parse(value) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/national-rail/virgin_live_departure_boards.rb', line 19

def parse(value)
  value = value.gsub(%r{&#\d+;}, '').gsub(%r{\*+$}, '')
  return value if ['On time', 'Starts here', 'No report', 'Cancelled', 'Delayed'].include?(value)
  parts = value.scan(%r{\d{2}})
  return nil unless parts.length == 2
  hhmm = parts.join(':')
  time = Time.zone.parse("#{@time.to_date} #{hhmm}")
  if time > 12.hours.from_now(@time)
    time = Time.zone.parse("#{@time.to_date - 1} #{hhmm}")
  elsif time < 12.hours.ago(@time)
    time = Time.zone.parse("#{@time.to_date + 1} #{hhmm}")
  end
  time
end