national-rail 0.4.9 → 0.4.10

Sign up to get free protection for your applications and to get access to all the features.
@@ -13,15 +13,22 @@ module NationalRail
13
13
  end
14
14
  end
15
15
  class TimeParser
16
- def initialize(date = Date.today)
17
- @date = date
16
+ def initialize(time = Time.zone.now)
17
+ @time = time
18
18
  end
19
19
  def parse(value)
20
20
  value = value.gsub(%r{&#\d+;}, '').gsub(%r{\*+$}, '')
21
21
  return value if ['On time', 'Starts here', 'No report', 'Cancelled', 'Delayed'].include?(value)
22
22
  parts = value.scan(%r{\d{2}})
23
23
  return nil unless parts.length == 2
24
- Time.zone.parse("#{@date} #{parts.join(':')}")
24
+ hhmm = parts.join(':')
25
+ time = Time.zone.parse("#{@time.to_date} #{hhmm}")
26
+ if time > 12.hours.from_now(@time)
27
+ time = Time.zone.parse("#{@time.to_date - 1} #{hhmm}")
28
+ elsif time < 12.hours.ago(@time)
29
+ time = Time.zone.parse("#{@time.to_date + 1} #{hhmm}")
30
+ end
31
+ time
25
32
  end
26
33
  end
27
34
  end
@@ -60,10 +67,9 @@ module NationalRail
60
67
 
61
68
  def details
62
69
  @agent.transact do
63
- time_parser = TimeParser.new
64
70
  page = @details_link.click
65
71
  VirginLiveDepartureBoards.capture(page, @details_link.href)
66
- parser = DetailsPageParser.new(page.doc, time_parser)
72
+ parser = DetailsPageParser.new(page.doc)
67
73
  parser.parse
68
74
  end
69
75
  end
@@ -92,7 +98,7 @@ module NationalRail
92
98
  @agent.get("http://realtime.nationalrail.co.uk/virgintrains/#{filename}") do |page|
93
99
  VirginLiveDepartureBoards.capture(page, filename)
94
100
  encoding = 'UTF-8'
95
- summary_rows = (page.doc/"table#TrainTable tbody tr")[2..-1].map do |tr|
101
+ summary_rows = ((page.doc/"table#TrainTable tbody tr")[2..-1] || []).map do |tr|
96
102
  tds = tr/"td"
97
103
  details_href = (tds[0]/"a").first["href"]
98
104
  details_link = page.links.detect { |l| l.attributes["href"] == details_href }
@@ -4,8 +4,9 @@ module NationalRail
4
4
 
5
5
  include CellParser
6
6
 
7
- def initialize(doc, time_parser)
8
- @doc, @time_parser = doc, time_parser
7
+ def initialize(doc)
8
+ @doc = doc
9
+ @time_parser = TimeParser.new
9
10
  end
10
11
 
11
12
  def parse
@@ -15,11 +16,11 @@ module NationalRail
15
16
  (table/"tbody tr").each do |tr|
16
17
  tds = tr/"td"
17
18
  next unless tds.length == 3
18
- will_call_at << {
19
+ will_call_at.push({
19
20
  :station => cell_text(tds[0]),
20
21
  :timetabled_arrival => @time_parser.parse(cell_text(tds[1])),
21
22
  :expected_arrival => @time_parser.parse(cell_text(tds[2]))
22
- }
23
+ })
23
24
  end
24
25
  end
25
26
  previous_calling_points = []
@@ -28,12 +29,12 @@ module NationalRail
28
29
  (table/"tbody tr").each do |tr|
29
30
  tds = tr/"td"
30
31
  next unless tds.length == 4
31
- previous_calling_points << {
32
+ previous_calling_points.push({
32
33
  :station => cell_text(tds[0]),
33
34
  :timetabled_departure => @time_parser.parse(cell_text(tds[1])),
34
35
  :expected_departure => @time_parser.parse(cell_text(tds[2])),
35
36
  :actual_departure => @time_parser.parse(cell_text(tds[3]))
36
- }
37
+ })
37
38
  end
38
39
  end
39
40
  { :will_call_at => will_call_at, :previous_calling_points => previous_calling_points }
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: 29
4
+ hash: 27
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 4
9
- - 9
10
- version: 0.4.9
9
+ - 10
10
+ version: 0.4.10
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: 2011-01-03 00:00:00 +00:00
18
+ date: 2011-04-01 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency