Class: NationalRail::VirginLiveDepartureBoards

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

Defined Under Namespace

Modules: CellParser Classes: DetailsPageParser, NokogiriParser, SummaryRow, TimeParser

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CellParser

#cell_text

Constructor Details

#initializeVirginLiveDepartureBoards

Returns a new instance of VirginLiveDepartureBoards.


88
89
90
91
92
93
# File 'lib/national-rail/virgin_live_departure_boards.rb', line 88

def initialize
  @agent = Mechanize.new
  @agent.pluggable_parser.html = NokogiriParser
  aliases = Mechanize::AGENT_ALIASES.keys - %w(Mechanize)
  @agent.user_agent_alias = aliases[rand(aliases.length)]
end

Class Attribute Details

.capture_pathObject

Returns the value of attribute capture_path


44
45
46
# File 'lib/national-rail/virgin_live_departure_boards.rb', line 44

def capture_path
  @capture_path
end

Class Method Details

.capture(page, filename) ⇒ Object


45
46
47
48
49
50
51
52
53
# File 'lib/national-rail/virgin_live_departure_boards.rb', line 45

def capture(page, filename)
  if capture_path.present?
    FileUtils.mkdir_p(capture_path)
    path = File.join(capture_path, filename)
    File.open(path, "w") { |f| f.write(TidyFFI::Tidy.new(page.parser.to_html).clean) }
  end
rescue => e
  puts e
end

Instance Method Details

#summary(station_code) ⇒ Object


95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/national-rail/virgin_live_departure_boards.rb', line 95

def summary(station_code)
  time_parser = TimeParser.new
  summary_rows = []
  filename = "summary.aspx?T=#{station_code}"
  @agent.get("http://realtime.nationalrail.co.uk/virgintrains/#{filename}") do |page|
    VirginLiveDepartureBoards.capture(page, filename)
    encoding = 'UTF-8'
    tbody = page.doc/"table#TrainTable tbody"
    if tbody
      trs = tbody/"tr"
      if trs.length > 2
        columns = ((trs[1])/"th").map { |th| th.inner_text.gsub(/\s+/, ' ') }
        summary_rows = []
        ((tbody/"tr")[2..-1] || []).each do |tr|
          tds = tr/"td"
          from = tds[columns.index("From")]
          if from.inner_text.match(/circular route/i)
            summary_rows.last.attributes[:circular_route] = true if summary_rows.any?
            next
          end
          details_href = (from/"a").first["href"]
          details_link = page.links.detect { |l| l.attributes["href"] == details_href }
          attributes = {
            :from => (tds[columns.index("From")]/"a").inner_text.gsub(/\s+/, ' '),
            :timetabled_arrival => time_parser.parse(cell_text(tds[columns.index("Timetabled Arrival")])),
            :expected_arrival => time_parser.parse(cell_text(tds[columns.index("Expected Arrival")])),
            :to => (tds[columns.index("To")]/"a").inner_text.gsub(/\s+/, ' '),
            :timetabled_departure => time_parser.parse(cell_text(tds[columns.index("Timetabled Departure")])),
            :expected_departure => time_parser.parse(cell_text(tds[columns.index("Expected Departure")])),
            :operator => (tds[columns.index("Operator")]/"a").inner_text.gsub(/\s+/, ' '),
            :details_url => "http://realtime.nationalrail.co.uk/virgintrains/#{details_href}"
          }
          if platform_index = columns.index("Platform")
            attributes[:platform] = parse_integer(cell_text(tds[platform_index]))
          end
          summary_rows << SummaryRow.new(@agent, details_link, attributes)
        end
      end
    end
  end
  summary_rows
end