Class: Gares::Train

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

Overview

Instance Method Summary collapse

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

#arrivalTrainStop

Returns The arrival point of the train

Returns:

  • (TrainStop)

    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

Returns:

  • (Boolean)

57
58
59
# File 'lib/gares/train.rb', line 57

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

#departureTrainStop

Returns The departure point of the train

Returns:

  • (TrainStop)

    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

#numberInteger

Returns The train number

Returns:

  • (Integer)

    The train number


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

def number
  num
end

#platformObject


61
62
63
# File 'lib/gares/train.rb', line 61

def platform
  voie
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.


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