ierail 0.4.1 → 0.5.0

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/ierail.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rest-client'
2
4
  require 'nokogiri'
3
5
  require 'uri'
@@ -10,19 +12,21 @@ require 'train_movement'
10
12
  require 'core_ext'
11
13
 
12
14
  class IERail
13
-
15
+
14
16
  URL = "http://api.irishrail.ie/realtime/realtime.asmx"
15
-
17
+
16
18
  class IERailGet < Nokogiri::XML::SAX::Document
17
-
19
+
18
20
  attr_reader :result
19
-
21
+
20
22
  def initialize(url, array_name, object_name)
21
- @ws_url = URI.encode(url)
23
+ @ws_url = url.gsub(' ', '%20')
22
24
  @ws_array_name = array_name.downcase
23
25
  @ws_object_name = object_name.downcase
26
+ @result = nil
27
+ @current = nil
24
28
  end
25
-
29
+
26
30
  def response
27
31
  unless @result
28
32
  parser = Nokogiri::XML::SAX::Parser.new(self)
@@ -31,27 +35,27 @@ class IERail
31
35
  @result
32
36
  end
33
37
 
34
- def start_document
38
+ def start_document
35
39
  @result = []
36
40
  end
37
-
41
+
38
42
  def characters string
39
- string.strip!
40
- @result.last[@current] << string unless string.empty?
43
+ str = string.strip
44
+ @result.last[@current] << str unless str.empty?
41
45
  end
42
-
46
+
43
47
  def start_element name, attrs = []
44
48
  case name.downcase
45
- when @ws_object_name
49
+ when @ws_object_name
46
50
  @result << Hash.new
47
51
  when @ws_array_name
48
52
  ;
49
53
  else
50
- @current = name
51
- @result.last[@current] = ""
54
+ @current = name
55
+ @result.last[@current] = String.new("")
52
56
  end
53
57
  end
54
-
58
+
55
59
  def end_element name
56
60
  if @current && @result.last[@current].empty?
57
61
  @result.last.delete(@current)
@@ -59,7 +63,7 @@ class IERail
59
63
  @current = nil
60
64
  end
61
65
  end
62
-
66
+
63
67
  # Get ALL the stations!
64
68
  # Returns array of Station objects, and each object responds to
65
69
  # {
@@ -72,10 +76,10 @@ class IERail
72
76
  #
73
77
  def stations
74
78
  ier = IERailGet.new("getAllStationsXML?", "arrayofobjstation", "objstation")
75
-
79
+
76
80
  ier.response.map { |s| Station.new(s) }
77
81
  end
78
-
82
+
79
83
  # Get ALL the trains! That are on the go at the moment.
80
84
  # Returns array of Train objects, and each object responds to
81
85
  # {
@@ -90,10 +94,10 @@ class IERail
90
94
  #
91
95
  def trains
92
96
  ier = IERailGet.new("getCurrentTrainsXML?", "arrayofobjtrainpositions", "objtrainpositions")
93
-
97
+
94
98
  ier.response.map { |t| Train.new(t) }
95
99
  end
96
-
100
+
97
101
  # Get train information for a particular station, by station name. This gives data on trains thru that station
98
102
  # Returns array of StationData objects, and each object responds to
99
103
  # {
@@ -122,21 +126,21 @@ class IERail
122
126
  #
123
127
  def station(name)
124
128
  ier = IERailGet.new("getStationDataByNameXML?StationDesc=#{name}", "arrayofobjstationdata", "objstationdata")
125
-
129
+
126
130
  ier.response.map { |sd| StationData.new(sd) }
127
131
  end
128
-
129
- # Get train information for a particular station, by station name, within the time period in minutes from now.
132
+
133
+ # Get train information for a particular station, by station name, within the time period in minutes from now.
130
134
  # This gives data on trains thru that station.
131
135
  # Returns array of StationData objects, and each obj looks like the one for IERail#station
132
136
  # Will return an empty array if no information.
133
137
  #
134
138
  def station_times(name, mins)
135
139
  ier = IERailGet.new("getStationDataByNameXML_withNumMins?StationDesc=#{name}&NumMins=#{mins}", "arrayofobjstationdata", "objstationdata")
136
-
140
+
137
141
  ier.response.map { |sd| StationData.new(sd) }
138
142
  end
139
-
143
+
140
144
  # Get the movements of a particular train, by train code, on a date.
141
145
  # If no date is supplied assume train has run/is running today.
142
146
  #
@@ -153,10 +157,10 @@ class IERail
153
157
  #
154
158
  def train_movements(code, date=Time.now)
155
159
  ier = IERailGet.new("getTrainMovementsXML?TrainId=#{code}&TrainDate=#{date.strftime("%d/%m/%Y")}", "ArrayOfObjTrainMovements", "ObjTrainMovements")
156
-
160
+
157
161
  ier.response.map{ |tm| TrainMovement.new(tm) }
158
162
  end
159
-
163
+
160
164
  # Find station codes and descriptions using a partial string to match the station name
161
165
  # Returns an array of Structs that each respond to
162
166
  # {
@@ -168,8 +172,11 @@ class IERail
168
172
  #
169
173
  def find_station(partial)
170
174
  ier = IERailGet.new("getStationsFilterXML?StationText=#{partial}", "ArrayOfObjStationFilter", "objStationFilter")
171
- Struct.new("Station", :name, :description, :code)
172
-
175
+
176
+ unless defined?(Struct::Station)
177
+ Struct.new("Station", :name, :description, :code)
178
+ end
179
+
173
180
  ier.response.map do |st|
174
181
  Struct::Station.new(st['StationDesc_sp'],
175
182
  st['StationDesc'],
@@ -189,7 +196,7 @@ class IERail
189
196
  direction = name.to_s.split('_').first.capitalize
190
197
 
191
198
  ier = IERailGet.new("getStationDataByNameXML?StationDesc=#{args.first}", "arrayofobjstationdata", "objstationdata")
192
-
199
+
193
200
  ier.response.select { |sd| sd['Direction'] == direction }.map { |sd| StationData.new(sd) }
194
201
  end
195
202
  end
data/lib/station.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Station
2
4
  attr_reader :description, :code, :id
3
5
 
data/lib/station_data.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class StationData
2
4
  attr_reader :server_time, :train_code, :station_name, :station_code,
3
5
  :status, :last_location, :due_in, :minutes_late, :minutes_early,
data/lib/train.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Train
2
4
  attr_reader :status, :code, :date, :message, :direction
3
5
 
@@ -1,11 +1,13 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class TrainMovement
2
-
4
+
3
5
  attr_reader :location_full_name
4
6
 
5
7
  def initialize hash
6
8
  @train_code = hash['TrainCode']
7
9
  @train_date = Time.parse hash['TrainDate']
8
- @location_code = hash['LocationCode']
10
+ @location_code = hash['LocationCode']
9
11
  @location_full_name = hash['LocationFullName']
10
12
  @location_order = hash['LocationOrder'].to_i
11
13
  @location_type = hash['LocationType']
@@ -19,7 +21,7 @@ class TrainMovement
19
21
  @arrival = arrival_time.nil? ? @expected_arrival : Time.parse(arrival_time)
20
22
  @departure = departure_time.nil? ? @expected_departure : Time.parse(departure_time)
21
23
  end
22
-
24
+
23
25
  def location
24
26
  {code: @location_code, name: @location_full_name, stop_number: @location_order, type: @location_type}
25
27
  end
@@ -27,11 +29,11 @@ class TrainMovement
27
29
  def arrival
28
30
  {scheduled: @scheduled_arrival, expected: @expected_arrival, actual: @arrival}
29
31
  end
30
-
32
+
31
33
  def departure
32
34
  {scheduled: @scheduled_departure, expected: @expected_departure, actual: @departure}
33
35
  end
34
-
36
+
35
37
  def train
36
38
  {code: @train_code, date: @train_date, origin: @train_origin}
37
39
  end
data/test/unit/helper.rb CHANGED
@@ -1,13 +1,12 @@
1
1
  require 'simplecov'
2
- require 'coveralls'
3
2
  require 'tzinfo'
4
3
  require 'vcr'
5
4
  require 'timecop'
6
- require 'time'
5
+ require 'date'
7
6
  require 'minitest/autorun'
8
7
  require 'ierail'
9
8
 
10
- def get_original_time(t)
9
+ def helper_get_original_time(t)
11
10
  t = Time.now if t.nil?
12
11
  @when = Time.parse(t.to_s)
13
12
  if TZInfo::Timezone.get('Europe/Dublin').current_period.dst?
@@ -17,12 +16,17 @@ def get_original_time(t)
17
16
  else
18
17
  @when -= @when.utc_offset
19
18
  end
20
-
19
+
21
20
  @when
22
21
  end
23
22
 
24
- SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
25
- SimpleCov::Formatter::HTMLFormatter,
26
- Coveralls::SimpleCov::Formatter
27
- ]
23
+ def helper_get_thirty_mins
24
+ thirty_mins = Time.now + (60 * 30)
25
+ " #{thirty_mins.hour}:#{thirty_mins.min}" # "HH:MM"
26
+ end
27
+
28
+ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter.new([
29
+ SimpleCov::Formatter::HTMLFormatter
30
+ ])
31
+
28
32
  SimpleCov.start
data/test/unit/ierail.rb CHANGED
@@ -1,8 +1,9 @@
1
+ # frozen_string_literal: true
1
2
  $:.unshift(File.join(File.dirname(__FILE__), '..','..', 'lib'))
2
3
 
3
4
  require_relative 'helper'
4
5
 
5
- class IERailTest < MiniTest::Unit::TestCase
6
+ class IERailTest < Minitest::Test
6
7
  def setup
7
8
  @ir = IERail.new
8
9
 
@@ -14,7 +15,7 @@ class IERailTest < MiniTest::Unit::TestCase
14
15
 
15
16
  def test_that_the_train_directions_are_correct
16
17
  VCR.use_cassette('northbound') do |cassette|
17
- time = get_original_time(cassette.originally_recorded_at)
18
+ time = helper_get_original_time(cassette.originally_recorded_at)
18
19
 
19
20
  Timecop.freeze(time || Time.now) do
20
21
  northbound_train = @ir.northbound_from('Dublin Connolly').sample
@@ -22,7 +23,7 @@ class IERailTest < MiniTest::Unit::TestCase
22
23
  end
23
24
  end
24
25
  VCR.use_cassette('northbound') do |cassette|
25
- time = get_original_time(cassette.originally_recorded_at)
26
+ time = helper_get_original_time(cassette.originally_recorded_at)
26
27
 
27
28
  Timecop.freeze(time || Time.now) do
28
29
  southbound_train = @ir.southbound_from('Dublin Connolly').sample
@@ -33,7 +34,7 @@ class IERailTest < MiniTest::Unit::TestCase
33
34
 
34
35
  def test_that_an_empty_array_is_returned_when_no_data
35
36
  VCR.use_cassette('westbound') do |cassette|
36
- time = get_original_time(cassette.originally_recorded_at)
37
+ time = helper_get_original_time(cassette.originally_recorded_at)
37
38
 
38
39
  Timecop.freeze(time || Time.now) do
39
40
  nonexistant = @ir.westbound_from('Clongriffin')
@@ -44,35 +45,27 @@ class IERailTest < MiniTest::Unit::TestCase
44
45
 
45
46
  def test_that_the_before_time_constraint_works
46
47
  VCR.use_cassette('northbound') do |cassette|
47
- time = get_original_time(cassette.originally_recorded_at)
48
-
48
+ time = helper_get_original_time(cassette.originally_recorded_at)
49
49
  Timecop.freeze(time || Time.now) do
50
- #Thirty minutes from now
51
- thirty_mins = Time.now + (60 * 30)
52
- time = "#{thirty_mins.hour}:#{thirty_mins.min}" # "HH:MM"
53
- before_train = @ir.southbound_from('Dublin Connolly').before(time).sample
54
- assert before_train.expected_arrival <= thirty_mins
50
+ before_train = @ir.southbound_from('Dublin Connolly').before(helper_get_thirty_mins()).sample
51
+ assert before_train.expected_arrival.to_i - before_train.server_time.to_i < (30 * 60)
55
52
  end
56
53
  end
57
54
  end
58
55
 
59
56
  def test_that_the_after_time_constraint_works
60
57
  VCR.use_cassette('southbound') do |cassette|
61
- time = get_original_time(cassette.originally_recorded_at)
62
-
58
+ time = helper_get_original_time(cassette.originally_recorded_at)
63
59
  Timecop.freeze(time || Time.now) do
64
- #Thirty minutes from now
65
- thirty_mins = Time.now + (60 * 30)
66
- time = "#{thirty_mins.hour}:#{thirty_mins.min}" # "HH:MM"
67
- after_train = @ir.southbound_from('Dublin Connolly').after(time).sample
68
- assert after_train.expected_arrival >= thirty_mins
60
+ after_train = @ir.southbound_from('Dublin Connolly').after(helper_get_thirty_mins()).sample
61
+ assert after_train.expected_arrival.to_i - after_train.server_time.to_i >= (30 * 60)
69
62
  end
70
63
  end
71
64
  end
72
65
 
73
66
  def test_that_the_in_constraint_works
74
67
  VCR.use_cassette('southbound_from') do |cassette|
75
- time = get_original_time(cassette.originally_recorded_at)
68
+ time = helper_get_original_time(cassette.originally_recorded_at)
76
69
 
77
70
  Timecop.freeze(time || Time.now) do
78
71
  mins = 30
@@ -92,7 +85,7 @@ class IERailTest < MiniTest::Unit::TestCase
92
85
 
93
86
  def test_station_times
94
87
  VCR.use_cassette('station_times') do |cassette|
95
- time = get_original_time(cassette.originally_recorded_at)
88
+ time = helper_get_original_time(cassette.originally_recorded_at)
96
89
 
97
90
  Timecop.freeze(time || Time.now) do
98
91
  station_data = @ir.station_times('Dublin Connolly',30).sample
@@ -103,7 +96,7 @@ class IERailTest < MiniTest::Unit::TestCase
103
96
 
104
97
  def test_find_station
105
98
  VCR.use_cassette('find_station') do |cassette|
106
- time = get_original_time(cassette.originally_recorded_at)
99
+ time = helper_get_original_time(cassette.originally_recorded_at)
107
100
 
108
101
  Timecop.freeze(time || Time.now) do
109
102
  station = @ir.find_station('Dublin Connolly').sample
@@ -114,7 +107,7 @@ class IERailTest < MiniTest::Unit::TestCase
114
107
 
115
108
  def test_that_station_times_returns_station_data
116
109
  VCR.use_cassette('station_times') do |cassette|
117
- time = get_original_time(cassette.originally_recorded_at)
110
+ time = helper_get_original_time(cassette.originally_recorded_at)
118
111
 
119
112
  Timecop.freeze(time || Time.now) do
120
113
  train = @ir.station_times('Dublin Connolly', 30).sample #random train in next 30 mins
@@ -124,10 +117,10 @@ class IERailTest < MiniTest::Unit::TestCase
124
117
  end
125
118
 
126
119
  def test_that_station_times_equivalent_to_in
127
- VCR.use_cassette('station_times') do |cassette|
120
+ VCR.use_cassette('station_times') do
128
121
  trains = @ir.station_times('Dublin Connolly', 30)
129
122
 
130
- VCR.use_cassette('station') do |cassette|
123
+ VCR.use_cassette('station') do
131
124
  in_half_an_hour = @ir.station('Dublin Connolly').in(30)
132
125
 
133
126
  assert_equal trains.count, in_half_an_hour.count
@@ -139,7 +132,7 @@ class IERailTest < MiniTest::Unit::TestCase
139
132
  end
140
133
 
141
134
  def test_that_found_station_is_a_struct_with_name_description_code
142
- VCR.use_cassette('find_station') do |cassette|
135
+ VCR.use_cassette('find_station') do
143
136
  station = @ir.find_station('Dublin Connolly').sample
144
137
  assert_equal station.class, Struct::Station
145
138
  refute_nil station.name
data/test/unit/station.rb CHANGED
@@ -2,7 +2,7 @@ $:.unshift(File.join(File.dirname(__FILE__), '..','..', 'lib'))
2
2
 
3
3
  require_relative 'helper'
4
4
 
5
- class StationTest < MiniTest::Unit::TestCase
5
+ class StationTest < Minitest::Test
6
6
  def setup
7
7
  ir = IERail.new
8
8
 
@@ -2,7 +2,7 @@ $:.unshift(File.join(File.dirname(__FILE__), '..','..', 'lib'))
2
2
 
3
3
  require_relative 'helper'
4
4
 
5
- class StationDataTest < MiniTest::Unit::TestCase
5
+ class StationDataTest < Minitest::Test
6
6
  def setup
7
7
  ir = IERail.new
8
8
 
@@ -29,10 +29,6 @@ class StationDataTest < MiniTest::Unit::TestCase
29
29
  refute_empty @station_data.code
30
30
  end
31
31
 
32
- def test_that_there_is_a_query_time
33
- refute_nil @station_data.query_time
34
- end
35
-
36
32
  def test_that_there_is_a_status
37
33
  refute_empty @station_data.status
38
34
  end
data/test/unit/train.rb CHANGED
@@ -2,7 +2,7 @@ $:.unshift(File.join(File.dirname(__FILE__), '..','..', 'lib'))
2
2
 
3
3
  require_relative 'helper'
4
4
 
5
- class TrainTest < MiniTest::Unit::TestCase
5
+ class TrainTest < Minitest::Test
6
6
  def setup
7
7
  ir = IERail.new
8
8
 
@@ -2,7 +2,7 @@ $:.unshift(File.join(File.dirname(__FILE__), '..','..', 'lib'))
2
2
 
3
3
  require_relative 'helper'
4
4
 
5
- class TrainMovementTest < MiniTest::Unit::TestCase
5
+ class TrainMovementTest < Minitest::Test
6
6
  def setup
7
7
  ir = IERail.new
8
8