Class: Bahn::Stop

Inherits:
Object
  • Object
show all
Defined in:
lib/bahn.rb

Overview

 Represents a stop made at a Station by a Service.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Stop

 :nodoc:



333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# File 'lib/bahn.rb', line 333

def initialize(opts) # :nodoc:
	# for the following fields, use :none to mean none supplied (as opposed to not fetched yet):
	# @arrival_time, @departure_time, @platform, @arrival_time_from_origin, @departure_time_from_origin

	@station = opts[:station] # required
	@service = opts[:service] # required
	@arrival_time = opts[:arrival_time]
	@departure_time = opts[:departure_time] # arrival_time or departure_time is required
	@platform = opts[:platform]
	@arrival_time_from_origin = opts[:arrival_time_from_origin]
	@departure_time_from_origin = opts[:departure_time_from_origin]

	@inferred_time_to_destination = opts[:inferred_time_to_destination]
	
	# these can be calculated from arrival_time_from_origin and departure_time_from_origin
	# (but require service.origin, and therefore service.stops, to have been looked up)
	@arrival_time_to_destination = opts[:arrival_time_to_destination]
	@departure_time_to_destination = opts[:departure_time_to_destination]
end

Instance Attribute Details

#serviceObject (readonly)

The Service making this stop.



356
357
358
# File 'lib/bahn.rb', line 356

def service
  @service
end

#stationObject (readonly)

The Station where this stop occurs.



354
355
356
# File 'lib/bahn.rb', line 354

def station
  @station
end

Instance Method Details

#arrival_timeObject

Returns the ClockTime at which the service arrives at this station, or nil if not specified (e.g. because this is the origin station for the service, or is for boarding only).



381
382
383
384
# File 'lib/bahn.rb', line 381

def arrival_time
	get_full_details if @arrival_time.nil?
	@arrival_time == :none ? nil : @arrival_time
end

#arrival_time_from_originObject

Returns the time between departing the origin station and arriving at this station, as a number of seconds, or nil if arrival time is not specified.



395
396
397
398
# File 'lib/bahn.rb', line 395

def arrival_time_from_origin
	get_full_details if @arrival_time_from_origin.nil?
	@arrival_time_from_origin == :none ? nil : @arrival_time_from_origin
end

#arrival_time_to_destinationObject

Returns the time between arriving at this station and arriving at the destination station, as a number of seconds, or nil if arrival time is not specified.



409
410
411
412
413
414
415
416
417
418
# File 'lib/bahn.rb', line 409

def arrival_time_to_destination
	if @arrival_time_to_destination.nil?
		if arrival_time_from_origin == :none
			@arrival_time_to_destination == :none
		else
			@arrival_time_to_destination = @service.destination.arrival_time_from_origin - arrival_time_from_origin
		end
	end
	@arrival_time_to_destination
end

#departure_timeObject

Returns the ClockTime at which the service leaves at this station, or nil if not specified (e.g. because this is the destination station for the service, or is for alighting only).



388
389
390
391
# File 'lib/bahn.rb', line 388

def departure_time
	get_full_details if @departure_time.nil?
	@departure_time == :none ? nil : @departure_time
end

#departure_time_from_originObject

Returns the time between departing the origin station and departing from this station, as a number of seconds, or nil if departure time is not specified.



402
403
404
405
# File 'lib/bahn.rb', line 402

def departure_time_from_origin
	get_full_details if @departure_time_from_origin.nil?
	@departure_time_from_origin == :none ? nil : @departure_time_from_origin
end

#departure_time_to_destinationObject

Returns the time between departing from this station and arriving at the destination station, as a number of seconds, or nil if departure time is not specified.



422
423
424
425
426
427
428
429
430
431
# File 'lib/bahn.rb', line 422

def departure_time_to_destination
	if @departure_time_to_destination.nil?
		if departure_time_from_origin == :none
			@departure_time_to_destination == :none
		else
			@departure_time_to_destination = @service.destination.arrival_time_from_origin - departure_time_from_origin
		end
	end
	@departure_time_to_destination
end

#destinationObject

The Stop object for the destination station (provided as a shortcut for stop.service.destination).



369
370
371
# File 'lib/bahn.rb', line 369

def destination
	@service.destination
end

#inferred_time_to_destinationObject

Returns calculated time to destination while minimising additional hits to the Deutsche Bahn website:

  • if the service timetable has already been fetched, use that;

  • if nothing relevant has been fetched yet, fetch the service timetable and use that;

  • if only the station timetable has been fetched, use that. This may result in an inaccurate count - see BUGS in README.txt.



438
439
440
# File 'lib/bahn.rb', line 438

def inferred_time_to_destination
	@inferred_time_to_destination ||= departure_time_to_destination || arrival_time_to_destination
end

#inspectObject

:nodoc:



442
443
444
# File 'lib/bahn.rb', line 442

def inspect # :nodoc:
	"#<#{self.class} @time=#{(@departure_time.nil? || @departure_time == :none ? @arrival_time : @departure_time).inspect} @station=#{@station.name.inspect} @destination=#{service.destination.station.name.inspect}>"
end

#long_hash_elementObject

alternative implementation, because standard Ruby hashes are probably not collision-resistant enough



454
455
456
# File 'lib/bahn.rb', line 454

def long_hash_element # :nodoc:
	"[#{@station.id},#{departure_time},#{arrival_time}]"
end

#nameObject

The name of the station for this stop (provided as a shortcut for stop.station.name).



359
360
361
# File 'lib/bahn.rb', line 359

def name
	@station.name
end

#originObject

The Stop object for the departure station (provided as a shortcut for stop.service.origin).



364
365
366
# File 'lib/bahn.rb', line 364

def origin
	@service.origin
end

#platformObject

Returns the platform number or name for this stop, or nil if not specified.



374
375
376
377
# File 'lib/bahn.rb', line 374

def platform
	get_full_details if @platform.nil?
	@platform == :none ? nil : @platform
end

#subhashObject

Code identifying this stop, which can form part of the hash for the Service. Not quite suitable as a hash for this stop in its own right, as different trains at the same station at the same time will have the same hash…



449
450
451
# File 'lib/bahn.rb', line 449

def subhash # :nodoc:
	[@station.id, departure_time, arrival_time].hash
end