Class: IERail

Inherits:
Object
  • Object
show all
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

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.



204
205
206
207
208
209
210
211
212
213
# File 'lib/ierail.rb', line 204

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")
   
    ier.response.select { |sd| sd['Direction'] == direction }.map { |sd| StationData.new(sd) }
  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.



168
169
170
171
172
173
174
175
176
177
# File 'lib/ierail.rb', line 168

def find_station(partial)
  ier = IERailGet.new("getStationsFilterXML?StationText=#{partial}", "ArrayOfObjStationFilter", "objStationFilter")
  Struct.new("Station", :name, :description, :code)
  
  ier.response.map do |st|
     Struct::Station.new(st['StationDesc_sp'],
                         st['StationDesc'],
                         st['StationCode'])
  end
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.



120
121
122
123
124
# File 'lib/ierail.rb', line 120

def station(name)
  ier = IERailGet.new("getStationDataByNameXML?StationDesc=#{name}", "arrayofobjstationdata", "objstationdata")
  
  ier.response.map { |sd| StationData.new(sd) }
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.



131
132
133
134
135
# File 'lib/ierail.rb', line 131

def station_times(name, mins)
  ier = IERailGet.new("getStationDataByNameXML_withNumMins?StationDesc=#{name}&NumMins=#{mins}", "arrayofobjstationdata", "objstationdata")
  
  ier.response.map { |sd| StationData.new(sd) }
end

#stationsObject

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.



73
74
75
76
77
# File 'lib/ierail.rb', line 73

def stations
  ier = IERailGet.new("getAllStationsXML?", "arrayofobjstation", "objstation")
  
  ier.response.map { |s| Station.new(s) }
end

#train_movements(code, date = Time.now) ⇒ Object

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 => => "E909", :date => "20 Jan 2012", :origin => "Glenageary"
obj#arrival => => "10:09", :expected => "10:09", :actual => "10.09"
obj#departure => => "10:09", :expected => "10:09", :actual => "10.09"
obj#station => "Glenageary"

}



150
151
152
153
154
155
156
157
# File 'lib/ierail.rb', line 150

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 |tm|
    retval << TrainMovement.new(tm)
  end
  retval
end

#trainsObject

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



91
92
93
94
95
# File 'lib/ierail.rb', line 91

def trains
  ier = IERailGet.new("getCurrentTrainsXML?", "arrayofobjtrainpositions", "objtrainpositions")
  
  ier.response.map { |t| Train.new(t) }
end