Class: Gares::Train
- Inherits:
-
Hashie::Dash
- Object
- Hashie::Dash
- Gares::Train
- Defined in:
- lib/gares/train.rb
Overview
Represents a train from www.sncf.com/fr/horaires-info-trafic/train
Instance Method Summary collapse
-
#arrival ⇒ TrainStop
The arrival point of the train.
- #delayed? ⇒ Boolean
-
#departure ⇒ TrainStop
The departure point of the train.
-
#initialize(*arguments) ⇒ Train
constructor
Initialize a new Train object with at least it’s number and departure date.
-
#number ⇒ Integer
The train number.
- #platform ⇒ Object
-
#stops ⇒ Array<TrainStop>
A list of all stops between departure and arrival stations.
Constructor Details
#initialize(*arguments) ⇒ Train
Initialize a new Train object with at least it’s number and departure date
train = Gares::Train.new(num: 6704, date: 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 Only when you use an accessor that needs remote data.
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/gares/train.rb', line 26 def initialize(*arguments) fail "Please provide a train number" unless arguments.first[:num].is_a?(Integer) fail "Please provide a departure date" unless arguments.first[:date].is_a?(Time) if arguments.first[:origdest] arguments.first[:origdest] = Gares::Station.search(arguments.first[:origdest]).first end super(*arguments) end |
Instance Method Details
#arrival ⇒ TrainStop
Returns The arrival point of the train.
53 54 55 |
# File 'lib/gares/train.rb', line 53 def arrival @arrival ||= TrainStop.new(document.at('tr.itinerary-end'), date) end |
#delayed? ⇒ Boolean
57 58 59 |
# File 'lib/gares/train.rb', line 57 def delayed? retard || ([departure] + stops + [arrival]).any?(&:delayed?) end |
#departure ⇒ TrainStop
Returns The departure point of the train.
43 44 45 |
# File 'lib/gares/train.rb', line 43 def departure @departure ||= TrainStop.new(document.at('tr.itinerary-start'), date) end |
#number ⇒ Integer
Returns The train number.
38 39 40 |
# File 'lib/gares/train.rb', line 38 def number num end |
#platform ⇒ Object
61 62 63 |
# File 'lib/gares/train.rb', line 61 def platform voie end |
#stops ⇒ Array<TrainStop>
Returns A list of all stops between departure and arrival stations.
48 49 50 |
# File 'lib/gares/train.rb', line 48 def stops @stops ||= document.css('tr.itinerary-stop').map { |stop| TrainStop.new(stop, date) } end |