Class: Tube::Status

Inherits:
Object
  • Object
show all
Defined in:
lib/tube/status.rb

Overview

Models the status of the London Underground network as displayed on www.tfl.gov.uk/tfl/livetravelnews/realtime/tube/default.html.

It is a very thin abstraction over the tfl website, as a result the access to data on stations is somewhat different to lines due to the differing presentation. However it is very dynamic, for example should the East London line return it will show up in the lines array automatically.

Example Usage

require 'tube/status'

status = Tube::Status.new

broken_lines = status.lines.select {|line| line.problem?}
broken_lines.collect {|line| line.name}
#=> ["Circle", "District", "Jubilee", "Metropolitan", "Northern"]

status.lines.detect {|line| line.id == :circle}.message
#=> "Saturday 7 and Sunday 8 March, suspended."

closed_stations = status.station_groups["Closed stations"]
closed_stations.collect {|station| station.name}
#=> ["Blackfriars", "Hatton Cross"]

stations = status.station_groups.values.flatten
stations.detect {|station| station.name =~ "hatton"}.message
#=> "Saturday 7 and Sunday 8 March, closed."

status.updated.strftime("%I:%M%p")
#=> "04:56PM"

status.reload
status.updated.strftime("%I:%M%p")
#=> "05:00PM"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url = "http://www.tfl.gov.uk/tfl/livetravelnews/realtime/tube/default.html") ⇒ Status Also known as: reload

:call-seq: Status.new -> status

Request and parse the status of the London Underground network from the tfl.gov.uk “Live travel news” page.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/tube/status.rb', line 53

def initialize( url=
    "http://www.tfl.gov.uk/tfl/livetravelnews/realtime/tube/default.html" )
  results = Tube::StatusParser.parse( open( url ).read )
  @updated = results[:updated]
  
  @lines = results[:lines].map do |line|
    id = line[:html_class].to_sym
    name = line[:name]
    status = line[:status][:headline]
    problem = line[:status][:problem]
    message = line[:status][:message]
    
    Line.new( id, name, status, problem, message )
  end
  
  @station_groups = results[:station_groups].inject( {} ) do |memo, group|
    stations = group[:stations].map do |station|
      Station.new( station[:name], station[:message] )
    end
    
    memo[group[:name]] = stations
    memo
  end
  
  self
end

Instance Attribute Details

#linesObject (readonly)

Returns the value of attribute lines.



46
47
48
# File 'lib/tube/status.rb', line 46

def lines
  @lines
end

#station_groupsObject (readonly)

Returns the value of attribute station_groups.



46
47
48
# File 'lib/tube/status.rb', line 46

def station_groups
  @station_groups
end

#updatedObject (readonly)

Returns the value of attribute updated.



46
47
48
# File 'lib/tube/status.rb', line 46

def updated
  @updated
end