national-rail 0.3.0 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/national-rail.rb +2 -1
- data/lib/national-rail/virgin_live_departure_boards.rb +100 -0
- metadata +4 -3
data/lib/national-rail.rb
CHANGED
@@ -0,0 +1,100 @@
|
|
1
|
+
require 'mechanize'
|
2
|
+
require 'hpricot'
|
3
|
+
|
4
|
+
module NationalRail
|
5
|
+
|
6
|
+
class VirginLiveDepartureBoards
|
7
|
+
|
8
|
+
module CellParser
|
9
|
+
def cell_text(td)
|
10
|
+
td.inner_html.gsub(" ", "")
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class SummaryRow
|
15
|
+
|
16
|
+
include CellParser
|
17
|
+
|
18
|
+
attr_reader :attributes
|
19
|
+
|
20
|
+
def initialize(agent, details_link, attributes)
|
21
|
+
@agent, @details_link, @attributes = agent, details_link, attributes
|
22
|
+
end
|
23
|
+
|
24
|
+
def details
|
25
|
+
@agent.transact do
|
26
|
+
page = @details_link.click
|
27
|
+
will_call_at = []
|
28
|
+
table = page.doc/"table[@summary='Will call at']"
|
29
|
+
if table.any?
|
30
|
+
(table/"tbody tr").each do |tr|
|
31
|
+
tds = tr/"td"
|
32
|
+
next unless tds.length == 3
|
33
|
+
will_call_at << {
|
34
|
+
:station => cell_text(tds[0]),
|
35
|
+
:timetabled_arrival => cell_text(tds[1]),
|
36
|
+
:expected_arrival => cell_text(tds[2])
|
37
|
+
}
|
38
|
+
end
|
39
|
+
end
|
40
|
+
previous_calling_points = []
|
41
|
+
table = page.doc/"table[@summary='Previous calling points']"
|
42
|
+
if table.any?
|
43
|
+
(table/"tbody tr").each do |tr|
|
44
|
+
tds = tr/"td"
|
45
|
+
next unless tds.length == 4
|
46
|
+
previous_calling_points << {
|
47
|
+
:station => cell_text(tds[0]),
|
48
|
+
:timetabled_departure => cell_text(tds[1]),
|
49
|
+
:expected_departure => cell_text(tds[2]),
|
50
|
+
:actual_departure => cell_text(tds[3])
|
51
|
+
}
|
52
|
+
end
|
53
|
+
end
|
54
|
+
{ :will_call_at => will_call_at, :previous_calling_points => previous_calling_points }
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
class HpricotParser < Mechanize::Page
|
60
|
+
attr_reader :doc
|
61
|
+
def initialize(uri = nil, response = nil, body = nil, code = nil)
|
62
|
+
@doc = Hpricot(body)
|
63
|
+
super(uri, response, body, code)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
include CellParser
|
68
|
+
|
69
|
+
def initialize
|
70
|
+
@agent = Mechanize.new
|
71
|
+
@agent.pluggable_parser.html = HpricotParser
|
72
|
+
@agent.user_agent_alias = "Mac FireFox"
|
73
|
+
end
|
74
|
+
|
75
|
+
def summary(station_code)
|
76
|
+
summary_rows = []
|
77
|
+
@agent.get("http://realtime.nationalrail.co.uk/virgintrains/summary.aspx?T=#{station_code}") do |page|
|
78
|
+
encoding = 'UTF-8'
|
79
|
+
summary_rows = (page.doc/"table#TrainTable tbody tr")[2..-1].map do |tr|
|
80
|
+
tds = tr/"td"
|
81
|
+
details_href = (tds[0]/"a").first["href"]
|
82
|
+
details_link = page.links.detect { |l| l.attributes["href"] == details_href }
|
83
|
+
SummaryRow.new(@agent, details_link, {
|
84
|
+
:from => (tds[0]/"a").inner_text,
|
85
|
+
:timetabled_arrival => cell_text(tds[1]),
|
86
|
+
:expected_arrival => cell_text(tds[2]),
|
87
|
+
:platform => cell_text(tds[3]),
|
88
|
+
:to => (tds[4]/"a").inner_text,
|
89
|
+
:timetabled_departure => cell_text(tds[5]),
|
90
|
+
:expected_departure => cell_text(tds[6]),
|
91
|
+
:details_url => "http://realtime.nationalrail.co.uk/virgintrains/#{details_href}"
|
92
|
+
})
|
93
|
+
end
|
94
|
+
end
|
95
|
+
summary_rows
|
96
|
+
end
|
97
|
+
|
98
|
+
end
|
99
|
+
|
100
|
+
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 3
|
8
|
-
-
|
9
|
-
version: 0.3.
|
8
|
+
- 1
|
9
|
+
version: 0.3.1
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- James Mead
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-11-
|
17
|
+
date: 2010-11-05 00:00:00 +00:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -73,6 +73,7 @@ files:
|
|
73
73
|
- lib/national-rail/live_departure_boards.rb
|
74
74
|
- lib/national-rail/station_list.rb
|
75
75
|
- lib/national-rail/stations.kml
|
76
|
+
- lib/national-rail/virgin_live_departure_boards.rb
|
76
77
|
- lib/national-rail.rb
|
77
78
|
has_rdoc: true
|
78
79
|
homepage:
|