lib/ierail.rb in ierail-0.3.4 vs lib/ierail.rb in ierail-0.3.5
- old
+ new
@@ -4,10 +4,11 @@
require 'time'
require 'train'
require 'station'
require 'station_data'
+require 'train_movement'
require 'core_ext'
class IERail
URL = "http://api.irishrail.ie/realtime/realtime.asmx"
@@ -69,15 +70,12 @@
# }
# Returns empty array if no data, but that would be odd.
#
def stations
ier = IERailGet.new("getAllStationsXML?", "arrayofobjstation", "objstation")
- retval = []
- ier.response.each do |s|
- retval << Station.new(s)
- end
- retval
+
+ ier.response.map { |s| Station.new(s) }
end
# Get ALL the trains! That are on the go at the moment.
# Returns array of Train objects, and each object responds to
# {
@@ -90,15 +88,12 @@
# }
# Returns empty array if no data
#
def trains
ier = IERailGet.new("getCurrentTrainsXML?", "arrayofobjtrainpositions", "objtrainpositions")
- retval = []
- ier.response.each do |t|
- retval << Train.new(t)
- end
- retval
+
+ ier.response.map { |t| Train.new(t) }
end
# 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
# {
@@ -122,27 +117,43 @@
# }
# Returns empty array if no data.
#
def station(name)
ier = IERailGet.new("getStationDataByNameXML?StationDesc=#{name}", "arrayofobjstationdata", "objstationdata")
- retval = []
- ier.response.each do |sd|
- retval << StationData.new(sd)
- end
- retval
+
+ ier.response.map { |sd| StationData.new(sd) }
end
# 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.
#
def station_times(name, mins)
ier = IERailGet.new("getStationDataByNameXML_withNumMins?StationDesc=#{name}&NumMins=#{mins}", "arrayofobjstationdata", "objstationdata")
+
+ ier.response.map { |sd| StationData.new(sd) }
+ end
+
+ # Get the movements of a particular train, by train code, on a date.
+ # If no date is supplied assume train has run/is running today.
+ #
+ # This gives all the stations that the train has or will be stopped at.
+ # Returns an array of TrainMovement objects, and each object responds to
+ # {
+ # obj#location => {code: "GLGRY", name: "Glenageary", stop_number: 1 (Stop number on route), type: "O" (Origin) \ "S" (Stop) \ "D" (Destination)}
+ # obj#train => {:code => "E909", :date => "20 Jan 2012", :origin => "Glenageary"}
+ # obj#arrival => {:scheduled => "10:09", :expected => "10:09", :actual => "10.09"}
+ # obj#departure => {:scheduled => "10:09", :expected => "10:09", :actual => "10.09"}
+ # obj#station => "Glenageary"
+ # }
+ #
+ def train_movements(code, date=Time.now)
+ ier = IERailGet.new("getTrainMovementsXML?TrainId=#{code}&TrainDate=#{date.strftime("%d/%m/%Y")}", "ArrayOfObjTrainMovements", "ObjTrainMovements")
retval = []
- ier.response.each do |sd|
- retval << StationData.new(sd)
+ ier.response.each do |tm|
+ retval << TrainMovement.new(tm)
end
retval
end
# Find station codes and descriptions using a partial string to match the station name
@@ -155,17 +166,16 @@
# or an empty array if no matches.
#
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'])
+
+ ier.response.map do |st|
+ Struct::Station.new(st['StationDesc_sp'],
+ st['StationDesc'],
+ st['StationCode'])
end
- retval
end
# 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
@@ -195,13 +205,10 @@
# 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
+
+ ier.response.select { |sd| sd['Direction'] == direction }.map { |sd| StationData.new(sd) }
end
end
end