national-rail 0.3.11 → 0.3.12

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.
@@ -0,0 +1,54 @@
1
+ module NationalRail
2
+ class JourneyPlanner
3
+ class DetailsPageParser
4
+
5
+ def initialize(doc, date)
6
+ @doc, @date = doc, date
7
+ end
8
+
9
+ def parse
10
+ details = {}
11
+ description_divs = (@doc/"table#journeyLegDetails tbody tr.lastRow td[@colspan='6'] div")
12
+ return details unless description_divs.any?
13
+ description = description_divs.last.inner_text.gsub(/\s+/, " ").strip
14
+ company_matches = /(.*) service from .* to .*/.match(description)
15
+ return details unless company_matches
16
+ details[:company] = company_matches[1].strip
17
+ origins, destinations = (/.* service from (.*) to (.*)/.match(description)[1,2]).map{ |s| s.split(",").map(&:strip) }
18
+ details[:origins], details[:destinations] = origins, destinations
19
+ parser = TimeParser.new(@date)
20
+
21
+ origin_code = (@doc/"td.origin abbr").inner_html.strip
22
+ departs_at = (@doc/"td.leaving").inner_html.strip
23
+ details[:initial_stop] = {
24
+ :station_code => origin_code,
25
+ :departs_at => parser.time(departs_at)
26
+ }
27
+
28
+ details[:stops] = []
29
+ (@doc/".callingpoints table > tbody > tr").each do |tr|
30
+ if (tr/".calling-points").length > 0
31
+ station_code = (tr/".calling-points > a > abbr").inner_html.strip
32
+ arrives_at = (tr/".arrives").inner_html.strip
33
+ departs_at = (tr/".departs").inner_html.strip
34
+ departs_at = arrives_at if arrives_at.present? && departs_at.blank?
35
+ arrives_at = departs_at if arrives_at.blank? && departs_at.present?
36
+ details[:stops] << {
37
+ :station_code => station_code,
38
+ :arrives_at => parser.time(arrives_at),
39
+ :departs_at => parser.time(departs_at)
40
+ }
41
+ end
42
+ end
43
+
44
+ destination_code = (@doc/"td.destination abbr").inner_html.strip
45
+ arrives_at = (@doc/"td.arriving").inner_html.strip
46
+ details[:final_stop] = {
47
+ :station_code => destination_code,
48
+ :arrives_at => parser.time(arrives_at)
49
+ }
50
+ details
51
+ end
52
+ end
53
+ end
54
+ end
@@ -3,6 +3,8 @@ require 'nokogiri'
3
3
  require 'active_support'
4
4
  require 'tidy_ffi'
5
5
 
6
+ require 'national-rail/journey_planner/details_page_parser'
7
+
6
8
  module NationalRail
7
9
 
8
10
  class ParseError < StandardError
@@ -81,55 +83,17 @@ module NationalRail
81
83
  @agent.transact do
82
84
  details_page = @link.click
83
85
  JourneyPlanner.capture(details_page, "details.html")
84
- parse_details(details_page, @date)
85
- end
86
- end
87
-
88
- def parse_details(page, date)
89
- details = {}
90
- description = (page.doc/"table#journeyLegDetails tbody tr.lastRow td[@colspan='6'] div").last.inner_text.gsub(/\s+/, " ").strip
91
- company_matches = /(.*) service from .* to .*/.match(description)
92
- return details unless company_matches
93
- details[:company] = company_matches[1].strip
94
- origins, destinations = (/.* service from (.*) to (.*)/.match(description)[1,2]).map{ |s| s.split(",").map(&:strip) }
95
- details[:origins], details[:destinations] = origins, destinations
96
- parser = TimeParser.new(date)
97
-
98
- origin_code = (page.doc/"td.origin abbr").inner_html.strip
99
- departs_at = (page.doc/"td.leaving").inner_html.strip
100
- details[:initial_stop] = {
101
- :station_code => origin_code,
102
- :departs_at => parser.time(departs_at)
103
- }
104
-
105
- details[:stops] = []
106
- (page.doc/".callingpoints table > tbody > tr").each do |tr|
107
- if (tr/".calling-points").length > 0
108
- station_code = (tr/".calling-points > a > abbr").inner_html.strip
109
- arrives_at = (tr/".arrives").inner_html.strip
110
- departs_at = (tr/".departs").inner_html.strip
111
- departs_at = arrives_at if arrives_at.present? && departs_at.blank?
112
- arrives_at = departs_at if arrives_at.blank? && departs_at.present?
113
- details[:stops] << {
114
- :station_code => station_code,
115
- :arrives_at => parser.time(arrives_at),
116
- :departs_at => parser.time(departs_at)
117
- }
86
+ parser = DetailsPageParser.new(details_page.doc, @date)
87
+ begin
88
+ parser.parse
89
+ rescue => e
90
+ JourneyPlanner.capture(details_page, "details-error.html")
91
+ page_html = TidyFFI::Tidy.new(details_page.parser.to_html).clean
92
+ raise ParseError.new(e, page_html)
118
93
  end
119
94
  end
120
-
121
- destination_code = (page.doc/"td.destination abbr").inner_html.strip
122
- arrives_at = (page.doc/"td.arriving").inner_html.strip
123
- details[:final_stop] = {
124
- :station_code => destination_code,
125
- :arrives_at => parser.time(arrives_at)
126
- }
127
- details
128
- rescue => e
129
- JourneyPlanner.capture(page, "details-error.html")
130
- page_html = TidyFFI::Tidy.new(page.parser.to_html).clean
131
- raise ParseError.new(e, page_html)
132
95
  end
96
+
133
97
  end
134
98
 
135
99
  def initialize
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: national-rail
3
3
  version: !ruby/object:Gem::Version
4
- hash: 5
4
+ hash: 11
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 3
9
- - 11
10
- version: 0.3.11
9
+ - 12
10
+ version: 0.3.12
11
11
  platform: ruby
12
12
  authors:
13
13
  - James Mead
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-12-05 00:00:00 +00:00
18
+ date: 2010-12-16 00:00:00 +00:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -162,7 +162,7 @@ dependencies:
162
162
  version: 1.6.1
163
163
  type: :development
164
164
  version_requirements: *id009
165
- description:
165
+ description: Includes journey planner, live departure boards (both National Rail & Virgin), and a station list with GPS co-ordinates.
166
166
  email: james@floehopper.org
167
167
  executables: []
168
168
 
@@ -174,12 +174,13 @@ files:
174
174
  - lib/national-rail.rb
175
175
  - lib/national-rail/index.html
176
176
  - lib/national-rail/journey_planner.rb
177
+ - lib/national-rail/journey_planner/details_page_parser.rb
177
178
  - lib/national-rail/live_departure_boards.rb
178
179
  - lib/national-rail/station_list.rb
179
180
  - lib/national-rail/stations.kml
180
181
  - lib/national-rail/virgin_live_departure_boards.rb
181
182
  has_rdoc: true
182
- homepage:
183
+ homepage: http://github.com/floehopper/national-rail
183
184
  licenses: []
184
185
 
185
186
  post_install_message: