Module: Tube::StatusParser

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

Instance Method Summary collapse

Instance Method Details

#parse(hpricot_doc) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/status_parser.rb', line 5

def parse( hpricot_doc )
  service_board = hpricot_doc.at( "#service-board" )
  
  updated_element = service_board.previous_sibling.children.first
  updated = parse_updated( updated_element )
  
  lines = service_board.search( "dl#lines dt" ).map do |line_element|
    parse_line( line_element )
  end
  
  station_group_elements = service_board.search( "dl#stations dt" )
  station_groups = station_group_elements.map do |station_group_element|
    parse_station_group( station_group_element )
  end
  
  {:updated => updated, :lines => lines, :station_groups => station_groups}
end

#parse_line(line_element) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/status_parser.rb', line 30

def parse_line( line_element )
  name = line_element.inner_text.strip
  html_class = line_element.attributes["class"]
  status = parse_status( line_element.next_sibling )
  
  {:name => name, :html_class => html_class, :status => status}
end

#parse_station(station_element) ⇒ Object



68
69
70
71
72
73
# File 'lib/status_parser.rb', line 68

def parse_station( station_element )
  name = station_element.at( "h3" ).inner_text.strip
  message = parse_status_message( station_element.search( "div.message p" ) )
  
  {:name => name, :message => message}
end

#parse_station_group(station_group_element) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/status_parser.rb', line 52

def parse_station_group( station_group_element )
  name = station_group_element.inner_text.strip
  stations = []
  
  station_element = station_group_element
  while station_element = station_element.next_sibling
    if station_element.to_html =~ /^<dd/
      stations.push( parse_station( station_element ) )
    elsif station_element.to_html =~ /^<dt/
      break
    end
  end
  
  {:name => name, :stations => stations}
end

#parse_status(status_element) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/status_parser.rb', line 38

def parse_status( status_element )
  header = status_element.at( "h3" )
  
  if header
    headline = header.inner_text.strip
    message = parse_status_message( status_element.search( "div.message p" ) )
  else
    headline = status_element.inner_text.strip
  end
  problem = status_element.attributes["class"] == "problem"
  
  {:headline => headline, :problem => problem, :message => message}
end

#parse_status_message(messages) ⇒ Object



75
76
77
78
79
80
81
82
83
84
# File 'lib/status_parser.rb', line 75

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



23
24
25
26
27
28
# File 'lib/status_parser.rb', line 23

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