Class: IERail
- Inherits:
-
Object
- Object
- IERail
- Defined in:
- lib/ierail.rb
Defined Under Namespace
Classes: IERailGet
Constant Summary collapse
- URL =
"http://api.irishrail.ie/realtime/realtime.asmx"
Instance Method Summary collapse
-
#find_station(partial) ⇒ Object
Find station codes and descriptions using a partial string to match the station name Returns an array of Structs that each respond to { struct#name =>“Sandycove”, struct#description =>“Glasthule (Sandycove )”, struct#code =>“SCOVE” } or an empty array if no matches.
-
#method_missing(name, *args, &block) ⇒ Object
Get direction-specific train information for a particular station, by station name.
-
#station(name) ⇒ Object
Get train information for a particular station, by station name.
-
#station_times(name, mins) ⇒ Object
Get train information for a particular station, by station name, within the time period in minutes from now.
-
#stations ⇒ Object
Get ALL the stations! Returns array of Station objects, and each object responds to { obj#name =>“Belfast Central”, obj#location =>[“-5.91744”, “54.6123”] obj#code =>“BFSTC”, obj#id =>“228” } Returns empty array if no data, but that would be odd.
-
#trains ⇒ Object
Get ALL the trains! That are on the go at the moment.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
Get direction-specific train information for a particular station, by station name. This gives data on trains through that station Returns array of StationData objects, and each object responds to
obj#servertime =>"2012-01-20T10:03:33.777",
obj#traincode =>"E909",
obj#name / obj#station_name =>"Glenageary",
obj#code / obj#station_code =>"GLGRY",
obj#query_time =>"10:03:33",
obj#train_date =>"20 Jan 2012",
obj#origin => {:name => "Bray", :time => "09:55"
obj#destination => => "Howth", :time => "11:03"
obj#status =>"En Route",
obj#last_location =>"Arrived Killiney",
obj#duein / obj#due_in =>"6",
obj#late =>"0",
obj#late? => 0 / 1
obj#arrival => => "10:09", :expected => "10:09"
obj#departure => => "10:09", :expected => "10:09"
obj#direction => "Northbound",
obj#train_type => "DART",
} Returns empty array if no data.
194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/ierail.rb', line 194 def method_missing(name, *args, &block) # Only handle *bound_from (e.g northbound_from / southbound_from) if name =~ /bound_from/ direction = name.to_s.split('_').first.capitalize ier = IERailGet.new("getStationDataByNameXML?StationDesc=#{args.first}", "arrayofobjstationdata", "objstationdata") retval = [] ier.response.each do |sd| retval << StationData.new(sd) if sd['Direction'] == direction end retval end end |
Instance Method Details
#find_station(partial) ⇒ Object
Find station codes and descriptions using a partial string to match the station name Returns an array of Structs that each respond to {
struct#name =>"Sandycove",
struct#description =>"Glasthule (Sandycove )",
struct#code =>"SCOVE"
}
or an empty array if no matches.
157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/ierail.rb', line 157 def find_station(partial) ier = IERailGet.new("getStationsFilterXML?StationText=#{partial}", "ArrayOfObjStationFilter", "objStationFilter") Struct.new("Station", :name, :description, :code) retval = [] ier.response.each do |st| retval << Struct::Station.new(st['StationDesc_sp'], st['StationDesc'], st['StationCode']) end retval end |
#station(name) ⇒ Object
Get train information for a particular station, by station name. This gives data on trains thru that station Returns array of StationData objects, and each object responds to
obj#servertime =>"2012-01-20T10:03:33.777",
obj#traincode =>"E909",
obj#name / obj#station_name =>"Glenageary",
obj#code / obj#station_code =>"GLGRY",
obj#query_time =>"10:03:33",
obj#train_date =>"20 Jan 2012",
obj#origin => {:name => "Bray", :time => "09:55"
obj#destination => => "Howth", :time => "11:03"
obj#status =>"En Route",
obj#last_location =>"Arrived Killiney",
obj#duein / obj#due_in =>"6",
obj#late =>"0",
obj#late? => 0 / 1
obj#arrival => => "10:09", :expected => "10:09"
obj#departure => => "10:09", :expected => "10:09"
obj#direction => "Northbound",
obj#train_type => "DART",
} Returns empty array if no data.
125 126 127 128 129 130 131 132 |
# File 'lib/ierail.rb', line 125 def station(name) ier = IERailGet.new("getStationDataByNameXML?StationDesc=#{name}", "arrayofobjstationdata", "objstationdata") retval = [] ier.response.each do |sd| retval << StationData.new(sd) end retval end |
#station_times(name, mins) ⇒ Object
Get train information for a particular station, by station name, within the time period in minutes from now. This gives data on trains thru that station. Returns array of StationData objects, and each obj looks like the one for IERail#station Will return an empty array if no information.
139 140 141 142 143 144 145 146 |
# File 'lib/ierail.rb', line 139 def station_times(name, mins) ier = IERailGet.new("getStationDataByNameXML_withNumMins?StationDesc=#{name}&NumMins=#{mins}", "arrayofobjstationdata", "objstationdata") retval = [] ier.response.each do |sd| retval << StationData.new(sd) end retval end |
#stations ⇒ Object
Get ALL the stations! Returns array of Station objects, and each object responds to {
obj#name =>"Belfast Central",
obj#location =>["-5.91744", "54.6123"]
obj#code =>"BFSTC",
obj#id =>"228"
}
Returns empty array if no data, but that would be odd.
72 73 74 75 76 77 78 79 |
# File 'lib/ierail.rb', line 72 def stations ier = IERailGet.new("getAllStationsXML?", "arrayofobjstation", "objstation") retval = [] ier.response.each do |s| retval << Station.new(s) end retval end |
#trains ⇒ Object
Get ALL the trains! That are on the go at the moment. Returns array of Train objects, and each object responds to
{
obj#status =>"R",
obj#location =>["-6.23929, ""53.3509"]
obj#code =>"D303",
obj#date =>"20 Jan 2012",
obj#message =>"D303\\n09:30 - Docklands to M3 Parkway (1 mins late)\\nDeparted Docklands next stop Broombridge",
obj#direction =>"Northbound"
}
Returns empty array if no data
93 94 95 96 97 98 99 100 |
# File 'lib/ierail.rb', line 93 def trains ier = IERailGet.new("getCurrentTrainsXML?", "arrayofobjtrainpositions", "objtrainpositions") retval = [] ier.response.each do |t| retval << Train.new(t) end retval end |