ierail 0.2

Sign up to get free protection for your applications and to get access to all the features.
data/lib/ierail.rb ADDED
@@ -0,0 +1,165 @@
1
+ require 'rest-client'
2
+ require 'nokogiri'
3
+
4
+ require 'train'
5
+ require 'station'
6
+ require 'station_data'
7
+
8
+ class IERail
9
+
10
+ URL = "http://api.irishrail.ie/realtime/realtime.asmx"
11
+
12
+ class IERailGet < Nokogiri::XML::SAX::Document
13
+
14
+ attr_reader :result
15
+
16
+ def initialize(url, array_name, object_name)
17
+ @ws_url = url
18
+ @ws_array_name = array_name.downcase
19
+ @ws_object_name = object_name.downcase
20
+ end
21
+
22
+ def response
23
+ unless @result
24
+ parser = Nokogiri::XML::SAX::Parser.new(self)
25
+ parser.parse(RestClient.get(URL + "/" + @ws_url))
26
+ end
27
+ @result
28
+ end
29
+
30
+ def start_document
31
+ @result = []
32
+ end
33
+
34
+ def characters string
35
+ string.strip!
36
+ @result.last[@current] << string unless string.empty?
37
+ end
38
+
39
+ def start_element name, attrs = []
40
+ case name.downcase
41
+ when @ws_object_name
42
+ @result << Hash.new
43
+ when @ws_array_name
44
+ ;
45
+ else
46
+ @current = name
47
+ @result.last[@current] = ""
48
+ end
49
+ end
50
+
51
+ def end_element name
52
+ if @current && @result.last[@current].empty?
53
+ @result.last.delete(@current)
54
+ end
55
+ @current = nil
56
+ end
57
+ end
58
+
59
+ # Get ALL the stations!
60
+ # Returns array of Station objects, and each object responds to
61
+ # {
62
+ # obj#name =>"Belfast Central",
63
+ # obj#location =>["-5.91744", "54.6123"]
64
+ # obj#code =>"BFSTC",
65
+ # obj#id =>"228"
66
+ # }
67
+ # Returns empty array if no data, but that would be odd.
68
+ #
69
+ def stations
70
+ ier = IERailGet.new("getAllStationsXML?", "arrayofobjstation", "objstation")
71
+ retval = []
72
+ ier.response.each do |s|
73
+ retval << Station.new(s)
74
+ end
75
+ retval
76
+ end
77
+
78
+ # Get ALL the trains! That are on the go at the moment.
79
+ # Returns array of Train objects, and each object responds to
80
+ # {
81
+ # obj#status =>"R",
82
+ # obj#location =>["-6.23929, ""53.3509"]
83
+ # obj#code =>"D303",
84
+ # obj#date =>"20 Jan 2012",
85
+ # obj#message =>"D303\\n09:30 - Docklands to M3 Parkway (1 mins late)\\nDeparted Docklands next stop Broombridge",
86
+ # obj#direction =>"Northbound"
87
+ # }
88
+ # Returns empty array if no data
89
+ #
90
+ def trains
91
+ ier = IERailGet.new("getCurrentTrainsXML?", "arrayofobjtrainpositions", "objtrainpositions")
92
+ retval = []
93
+ ier.response.each do |t|
94
+ retval << Train.new(t)
95
+ end
96
+ retval
97
+ end
98
+
99
+ # Get train information for a particular station, by station name. This gives data on trains thru that station
100
+ # Returns array of StationData objects, and each object responds to
101
+ # {
102
+ # obj#servertime =>"2012-01-20T10:03:33.777",
103
+ # obj#traincode =>"E909",
104
+ # obj#name / obj#station_name =>"Glenageary",
105
+ # obj#code / obj#station_code =>"GLGRY",
106
+ # obj#query_time =>"10:03:33",
107
+ # obj#train_date =>"20 Jan 2012",
108
+ # obj#origin => {:name => "Bray", :time => "09:55"}
109
+ # obj#destination => {:name => "Howth", :time => "11:03"}
110
+ # obj#status =>"En Route",
111
+ # obj#last_location =>"Arrived Killiney",
112
+ # obj#duein / obj#due_in =>"6",
113
+ # obj#late =>"0",
114
+ # obj#late? => 0 / 1
115
+ # obj#arrival => {:scheduled => "10:09", :expected => "10:09"}
116
+ # obj#departure => {:scheduled => "10:09", :expected => "10:09"}
117
+ # obj#direction => "Northbound",
118
+ # obj#train_type => "DART",
119
+ # }
120
+ # Returns empty array if no data.
121
+ #
122
+ def station(name)
123
+ ier = IERailGet.new("getStationDataByNameXML?StationDesc=#{name}", "arrayofobjstationdata", "objstationdata")
124
+ retval = []
125
+ ier.response.each do |sd|
126
+ retval << StationData.new(sd)
127
+ end
128
+ retval
129
+ end
130
+
131
+ # Get train information for a particular station, by station name, within the time period in minutes from now.
132
+ # This gives data on trains thru that station.
133
+ # Returns array of StationData objects, and each obj looks like the one for IERail#station
134
+ # Will return an empty array if no information.
135
+ #
136
+ def station_times(name, mins)
137
+ ier = IERailGet.new("getStationDataByNameXML_withNumMins?StationDesc=#{name}&NumMins=#{mins}", "arrayofobjstationdata", "objstationdata")
138
+ retval = []
139
+ ier.response.each do |sd|
140
+ retval << StationData.new(sd)
141
+ end
142
+ retval
143
+ end
144
+
145
+ # Find station codes and descriptions using a partial string to match the station name
146
+ # Returns an array of Structs that each respond to
147
+ # {
148
+ # struct#name =>"Sandycove",
149
+ # struct#description =>"Glasthule (Sandycove )",
150
+ # struct#code =>"SCOVE"
151
+ # }
152
+ # or an empty array if no matches.
153
+ #
154
+ def find_station(partial)
155
+ ier = IERailGet.new("getStationsFilterXML?StationText=#{partial}", "ArrayOfObjStationFilter", "objStationFilter")
156
+ Struct.new("Station", :name, :description, :code)
157
+ retval = []
158
+ ier.response.each do |st|
159
+ retval << Struct::Station.new(st['StationDesc_sp'],
160
+ st['StationDesc'],
161
+ st['StationCode'])
162
+ end
163
+ retval
164
+ end
165
+ end
data/lib/station.rb ADDED
@@ -0,0 +1,17 @@
1
+ class Station
2
+ attr_reader :description, :code, :id
3
+
4
+ def initialize hash
5
+ @description = hash['StationDesc']
6
+ @latitude = hash['StationLatitude']
7
+ @longitude = hash['StationLongitude']
8
+ @code = hash['StationCode']
9
+ @id = hash['StationId']
10
+ end
11
+
12
+ def location
13
+ [@longitude,@latitude]
14
+ end
15
+
16
+ alias :name :description
17
+ end
@@ -0,0 +1,52 @@
1
+ class StationData
2
+ attr_reader :servertime, :traincode, :station_name, :station_code,
3
+ :status, :last_location, :duein, :late,
4
+ :train_type, :direction, :query_time, :train_date
5
+
6
+ def initialize hash
7
+ @servertime = hash['Servertime']
8
+ @traincode = hash['Traincode']
9
+ @station_name = hash['Stationfullname']
10
+ @station_code = hash['Stationcode']
11
+ @query_time = hash['Querytime']
12
+ @train_date = hash['Traindate']
13
+ @origin = hash['Origin']
14
+ @destination = hash['Destination']
15
+ @origin_time = hash['Origintime']
16
+ @destination_time = hash['Destinationtime']
17
+ @status = hash['Status']
18
+ @last_location = hash['Lastlocation']
19
+ @duein = hash['Duein']
20
+ @late = hash['Late']
21
+ @exparrival = hash['Exparrival']
22
+ @expdepart = hash['Expdepart']
23
+ @scharrival = hash['Scharrival']
24
+ @schdepart = hash['Schdepart']
25
+ @direction = hash['Direction']
26
+ @train_type = hash['Traintype']
27
+ end
28
+
29
+ def origin
30
+ {name: @origin, time: @origin_time}
31
+ end
32
+
33
+ def destination
34
+ {name: @destination, time: @destination_time}
35
+ end
36
+
37
+ def arrival
38
+ {scheduled: @scharrival, expected: @exparrival}
39
+ end
40
+
41
+ def departure
42
+ {scheduled: @schdepart, expected: @expdepart}
43
+ end
44
+
45
+ def late?
46
+ @late.to_i > 0
47
+ end
48
+
49
+ alias :name :station_name
50
+ alias :code :station_code
51
+ alias :due_in :duein
52
+ end
data/lib/train.rb ADDED
@@ -0,0 +1,17 @@
1
+ class Train
2
+ attr_reader :status, :code, :date, :message, :direction
3
+
4
+ def initialize hash
5
+ @status = hash['TrainStatus']
6
+ @longitude = hash['TrainLongitude']
7
+ @latitude = hash['TrainLatitude']
8
+ @code = hash['TrainCode']
9
+ @date = hash['TrainDate']
10
+ @message = hash['PublicMessage']
11
+ @direction = hash['Direction']
12
+ end
13
+
14
+ def location
15
+ [@longitude,@latitude]
16
+ end
17
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ierail
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: "0.2"
6
+ platform: ruby
7
+ authors:
8
+ - Oisin Hurley
9
+ - Gary Rafferty
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+
14
+ date: 2012-07-09 00:00:00 +01:00
15
+ default_executable:
16
+ dependencies: []
17
+
18
+ description: Irish Rail Train Schedule and Status API
19
+ email: oi.sin@nis.io
20
+ executables: []
21
+
22
+ extensions: []
23
+
24
+ extra_rdoc_files: []
25
+
26
+ files:
27
+ - lib/ierail.rb
28
+ - lib/station.rb
29
+ - lib/station_data.rb
30
+ - lib/train.rb
31
+ has_rdoc: true
32
+ homepage: http://rubygems.org/gems/ierail
33
+ licenses: []
34
+
35
+ post_install_message:
36
+ rdoc_options: []
37
+
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: "0"
52
+ requirements: []
53
+
54
+ rubyforge_project:
55
+ rubygems_version: 1.6.2
56
+ signing_key:
57
+ specification_version: 3
58
+ summary: Irish Rail Train Schedule and Status API
59
+ test_files: []
60
+