Class: Gares::Train

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

Overview

Represents something a train from www.sncf.com/fr/horaires-info-trafic/train

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(number, date) ⇒ Train

Initialize a new Train object with it's number and departure date

train = Gares::Train.new(6704, Time.parse('2015-04-15'))

Gares::Train objects are lazy loaded, meaning that no HTTP request will be performed when a new object is created. An HTTP request is made (once) Only when you use an accessor that needs the remote data.



16
17
18
19
20
21
22
# File 'lib/gares/train.rb', line 16

def initialize(number, date)
  fail "Please provide a train number" unless number.is_a?(Integer)
  fail "Please provide a departure date" unless date.is_a?(Time)

  @number = number
  @date = date
end

Instance Attribute Details

#dateObject

Returns the value of attribute date



7
8
9
# File 'lib/gares/train.rb', line 7

def date
  @date
end

#numberObject

Returns the value of attribute number



7
8
9
# File 'lib/gares/train.rb', line 7

def number
  @number
end

Instance Method Details

#arrivalTrainStop

Returns The arrival point of the train

Returns:

  • (TrainStop)

    The arrival point of the train



35
36
37
# File 'lib/gares/train.rb', line 35

def arrival
  @arrival ||= TrainStop.new(document.at('tr.itinerary-end'), @date)
end

#delayed?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/gares/train.rb', line 39

def delayed?
  ([departure] + stops + [arrival]).any?(&:delayed?)
end

#departureTrainStop

Returns The departure point of the train

Returns:

  • (TrainStop)

    The departure point of the train



25
26
27
# File 'lib/gares/train.rb', line 25

def departure
  @departure ||= TrainStop.new(document.at('tr.itinerary-start'), @date)
end

#stopsArray<TrainStop>

Returns A list of all stops between departure and arrival stations.

Returns:

  • (Array<TrainStop>)

    A list of all stops between departure and arrival stations.



30
31
32
# File 'lib/gares/train.rb', line 30

def stops
  @stops ||= document.css('tr.itinerary-stop').map { |stop| TrainStop.new(stop, @date) }
end