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:



325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/bahn.rb', line 325

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.



348
349
350
# File 'lib/bahn.rb', line 348

def service
  @service
end

#stationObject (readonly)

The Station where this stop occurs.



346
347
348
# File 'lib/bahn.rb', line 346

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).



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

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.



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

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.



401
402
403
404
405
406
407
408
409
410
# File 'lib/bahn.rb', line 401

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).



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

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.



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

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.



414
415
416
417
418
419
420
421
422
423
# File 'lib/bahn.rb', line 414

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).



361
362
363
# File 'lib/bahn.rb', line 361

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.



430
431
432
# File 'lib/bahn.rb', line 430

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

#inspectObject

:nodoc:



434
435
436
# File 'lib/bahn.rb', line 434

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

#nameObject

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



351
352
353
# File 'lib/bahn.rb', line 351

def name
	@station.name
end

#originObject

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



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

def origin
	@service.origin
end

#platformObject

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



366
367
368
369
# File 'lib/bahn.rb', line 366

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…



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

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