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.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/gares/train.rb', line 27

def initialize(*arguments)
  attributes = arguments.first

  fail "Please provide a train number" unless attributes[:num].is_a?(Integer)
  fail "Please provide a departure date" unless attributes[:date].is_a?(Time)

  if attributes[:origdest]
    @origdest = attributes[:orig].nil? ? :orig : :dest
    attributes[@origdest] = Gares::Station.search(attributes[:origdest]).first
    attributes.delete(:origdest)
  end

  super(*arguments)
end

Instance Method Details

#arrivalStation

Returns The arrival station of the train.

Returns:

  • (Station)

    The arrival station of the train



62
63
64
65
66
67
68
# File 'lib/gares/train.rb', line 62

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

#delayed?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/gares/train.rb', line 76

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

#departureStation

Returns The departure station of the train.

Returns:

  • (Station)

    The departure station of the train



48
49
50
51
52
53
54
# File 'lib/gares/train.rb', line 48

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

#numberInteger

Returns The train number.

Returns:

  • (Integer)

    The train number



43
44
45
# File 'lib/gares/train.rb', line 43

def number
  num
end

#origdestObject

Deprecated.


71
72
73
74
# File 'lib/gares/train.rb', line 71

def origdest
  warn("[DEPRECATED] Warning: This method is deprecated, please use `orig` or `dest` instead of `origdest`")
  send(@origdest)
end

#platformObject



80
81
82
# File 'lib/gares/train.rb', line 80

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.



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

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