Module: Tube::StatusParser

Extended by:
StatusParser
Included in:
StatusParser
Defined in:
lib/tube/status_parser.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#parse(html_doc) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/tube/status_parser.rb', line 10

def parse( html_doc )
  html_doc.gsub!( / /, " " )
  doc = Nokogiri::HTML( html_doc )
  
  updated_element = doc.at_css( "div.hd-row > h2" )
  updated = parse_updated( updated_element )
  
  service_board = doc.at_css( "#service-board" )
  
  line_elements = service_board.css( "ul#lines > li.ltn-line" )
  lines = line_elements.map( &method( :parse_line ) )
  
  station_group_elements = service_board.css( "ul#stations > li" )
  station_groups = station_group_elements.map(&method(:parse_station_group))
  
  {:updated => updated, :lines => lines, :station_groups => station_groups}
end

#parse_line(line_element) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/tube/status_parser.rb', line 35

def parse_line( line_element )
  name_element = line_element.at_css( "h3.ltn-name" )
  name = name_element.content
  html_class = name_element["class"].split.first
  status = parse_status( line_element.at_css( "div.status" ) )
  
  {:name => name, :html_class => html_class, :status => status}
end

#parse_station(station_element) ⇒ Object



70
71
72
73
74
75
# File 'lib/tube/status_parser.rb', line 70

def parse_station( station_element )
  name = station_element.at_css( "h4.ltn-name" ).content.strip
  message = parse_status_message( station_element.css( "div.message > p" ) )
  
  {:name => name, :message => message}
end

#parse_station_group(station_group_element) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/tube/status_parser.rb', line 58

def parse_station_group( station_group_element )
  name = station_group_element.at_css( "h3" ).content
  
  station_elements = station_group_element.css( "ul > li.ltn-station" )
  
  stations = station_elements.map do |station_element|
    parse_station( station_element )
  end
  
  {:name => name, :stations => stations}
end

#parse_status(status_element) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/tube/status_parser.rb', line 44

def parse_status( status_element )
  header = status_element.at_css( "h4.ltn-title" )
  
  if header
    headline = header.content.strip
    message = parse_status_message( status_element.css("div.message > p") )
  else
    headline = status_element.content.strip
  end
  problem = status_element["class"].split.include?( "problem" )
  
  {:headline => headline, :problem => problem, :message => message}
end

#parse_status_message(messages) ⇒ Object



77
78
79
80
81
82
83
84
85
86
# File 'lib/tube/status_parser.rb', line 77

def parse_status_message( messages )
  text_messages = messages.map do |message|
    if message.children
      message.children.select {|child| child.text?}.join( " " )
    end
  end.compact
  text_messages.reject! {|m| m.empty?}
  
  text_messages.map {|m| m.gsub( /\s+/, " " ).strip}.join( "\n" )
end

#parse_updated(updated_element) ⇒ Object



28
29
30
31
32
33
# File 'lib/tube/status_parser.rb', line 28

def parse_updated( updated_element )
  time_text = updated_element.content.match( /(\d?\d:\d\d)/ )[0]
  time_zone = if is_bst? then "+0100" else "+0000" end
  
  Time.parse( "#{time_text} #{time_zone}" )
end