Class: Bahn::Service
- Inherits:
-
Object
- Object
- Bahn::Service
- Defined in:
- lib/bahn.rb
Overview
Represents a scheduled train (or other transportation) service. This is not specific to a particular day’s train, but encompasses all trains taking this route at this time of day. When services are subject to daily or seasonal variations, each such variation is considered a Service of its own, even if they share the same service name.
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
The designated name for this service, if any - e.g.
Instance Method Summary collapse
-
#destination ⇒ Object
Returns the Stop object for the destination station of this service.
-
#features ⇒ Object
Returns an array of strings indicating the features of this train, as listed as ‘Comments:’ on the Deutsche Bahn website; e.g.
-
#hash ⇒ Object
Returns a hash code which will match for any two Service objects that stop at the same list of stations at the same times.
-
#initialize(opts) ⇒ Service
constructor
:nodoc:.
-
#inspect ⇒ Object
:nodoc:.
-
#origin ⇒ Object
Returns the Stop object for the departure station of this service.
-
#stops ⇒ Object
Returns an array of Stop objects for all the stops that this service makes.
Constructor Details
#initialize(opts) ⇒ Service
:nodoc:
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/bahn.rb', line 202 def initialize(opts) # :nodoc: @path = opts[:path] @name = opts[:name] if opts[:origin_info] @origin = Stop.new(opts[:origin_info].merge( :service => self, :arrival_time => :none, :arrival_time_from_origin => :none, :arrival_time_to_destination => :none, :departure_time_from_origin => 0 )) end if opts[:destination_info] @destination = Stop.new(opts[:destination_info].merge( :service => self, :arrival_time_to_destination => 0, :departure_time => :none, :departure_time_from_origin => :none, :departure_time_to_destination => :none )) end end |
Instance Attribute Details
#name ⇒ Object (readonly)
The designated name for this service, if any - e.g. “ICE 692”.
226 227 228 |
# File 'lib/bahn.rb', line 226 def name @name end |
Instance Method Details
#destination ⇒ Object
Returns the Stop object for the destination station of this service.
285 286 287 |
# File 'lib/bahn.rb', line 285 def destination @destination ||= stops.last end |
#features ⇒ Object
Returns an array of strings indicating the features of this train, as listed as ‘Comments:’ on the Deutsche Bahn website; e.g. “Subject to compulsory reservation”, “Sleeping-car”. There doesn’t seem to be a consistent format for these.
303 304 305 306 307 308 309 310 311 312 313 |
# File 'lib/bahn.rb', line 303 def features if @features.nil? tr = (traininfo_response_doc / 'table.remarks tr').find {|tr| (tr % 'th').inner_text == 'Comments:'} if tr @features = (tr / 'td strong').collect {|elem| elem.inner_text}.uniq else @features = [] end end @features end |
#hash ⇒ Object
Returns a hash code which will match for any two Service objects that stop at the same list of stations at the same times. (This is the nearest thing we have to a unique ID, as Deutsche Bahn do not expose unique IDs for services)
296 297 298 |
# File 'lib/bahn.rb', line 296 def hash stops.collect{|stop| stop.subhash}.hash end |
#inspect ⇒ Object
:nodoc:
289 290 291 |
# File 'lib/bahn.rb', line 289 def inspect # :nodoc: "#<#{self.class} @name=#{@name.inspect} @origin=#{@origin.inspect} @destination=#{@destination.inspect}>" end |
#origin ⇒ Object
Returns the Stop object for the departure station of this service.
280 281 282 |
# File 'lib/bahn.rb', line 280 def origin @origin ||= stops.first end |
#stops ⇒ Object
Returns an array of Stop objects for all the stops that this service makes.
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
# File 'lib/bahn.rb', line 229 def stops if !@stops stop_docs = traininfo_response_doc / "table.result tr[td.station]" last_time = nil days_passed = 0 origin_departure_time = nil @stops = stop_docs.collect {|stop_doc| station_link = stop_doc % 'td.station a' arrival_time = ClockTime.parse((stop_doc % 'td.arrival').inner_text) departure_time = ClockTime.parse((stop_doc % 'td.departure').inner_text) origin_departure_time ||= departure_time if arrival_time.nil? arrival_time_from_origin = :none else days_passed += 1 if (!last_time.nil? && arrival_time < last_time) arrival_time_from_origin = days_passed * 24*60*60 + (arrival_time - origin_departure_time) last_time = arrival_time end if departure_time.nil? departure_time_from_origin = :none else days_passed += 1 if (!last_time.nil? && departure_time < last_time) departure_time_from_origin = days_passed * 24*60*60 + (departure_time - origin_departure_time) last_time = departure_time end platform = (stop_doc % 'td.platform').inner_text.strip Stop.new( :station => Station.new( :id => station_link['href'].scan(/&input=[^&]*%23(\d+)&/).first.first, :name => station_link.inner_text ), :service => self, :arrival_time => arrival_time || :none, :departure_time => departure_time || :none, :platform => (platform == '' ? :none : platform), :arrival_time_from_origin => arrival_time_from_origin, :departure_time_from_origin => departure_time_from_origin ) } end @stops end |