Class: EasyTransilien::Trip

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/easy_transilien/trip.rb

Overview

EasyTransilien::Trip This class give you the differents Train from one point to an other point, on the same Transilien::Route, in the Time boudaries you gave.

The key method id Trip.find(from, to, options) You can let options and options params empty: in that case [at] will be right now, and [last] will be in 1 hour later than [at]

A trip is the EasyTransilien representation from Transilien::VehicleJourney

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#access_timeObject

Returns the value of attribute access_time.



12
13
14
# File 'lib/easy_transilien/trip.rb', line 12

def access_time
  @access_time
end

#arrival_timeObject

Returns the value of attribute arrival_time.



13
14
15
# File 'lib/easy_transilien/trip.rb', line 13

def arrival_time
  @arrival_time
end

#atObject

boundaries given via #find options



17
18
19
# File 'lib/easy_transilien/trip.rb', line 17

def at
  @at
end

#from_stationObject

Returns the value of attribute from_station.



12
13
14
# File 'lib/easy_transilien/trip.rb', line 12

def from_station
  @from_station
end

#from_stopObject

Returns the value of attribute from_stop.



12
13
14
# File 'lib/easy_transilien/trip.rb', line 12

def from_stop
  @from_stop
end

#lastObject

boundaries given via #find options



17
18
19
# File 'lib/easy_transilien/trip.rb', line 17

def last
  @last
end

#missionObject

Returns the value of attribute mission.



14
15
16
# File 'lib/easy_transilien/trip.rb', line 14

def mission
  @mission
end

#ms_journeyObject

Returns the value of attribute ms_journey.



15
16
17
# File 'lib/easy_transilien/trip.rb', line 15

def ms_journey
  @ms_journey
end

#start_timeObject

Returns the value of attribute start_time.



13
14
15
# File 'lib/easy_transilien/trip.rb', line 13

def start_time
  @start_time
end

#stopsObject

Returns the value of attribute stops.



18
19
20
# File 'lib/easy_transilien/trip.rb', line 18

def stops
  @stops
end

#to_stationObject

Returns the value of attribute to_station.



12
13
14
# File 'lib/easy_transilien/trip.rb', line 12

def to_station
  @to_station
end

#to_stopObject

Returns the value of attribute to_stop.



12
13
14
# File 'lib/easy_transilien/trip.rb', line 12

def to_stop
  @to_stop
end

Class Method Details

.find(from, to, options = {}) ⇒ Object

Find Options:

  • at (default Time.new)

  • last (default at + default_duration(3600))

  • whole_day: if true, override at and last with respectively 00:01 and 00:00

  • return: if true, invert from and to



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/easy_transilien/trip.rb', line 28

def find(from, to , options= {})
  at = last = nil
  default_duration = 3600
  if options[:whole_day]
    now = Time.new
    at = Time.new(now.year, now.month, now.day, 0, 0, 1)
    last = at + 86400
  elsif options[:at] && options[:at].is_a?(Time)
    at = options[:at]
    last = options[:last] if options[:last] && options[:last].is_a?(Time)
    last ||= at + default_duration
  end
  if options[:return]
    from_was = from
    from = to
    to = from_was
  end
  at ||= Time.new
  raise "at params MUST be a valid Time instance. Given: #{at.inspect}" unless at.is_a?(Time)
  last ||= at + default_duration
  raise "last params MUST be a valid Time instance. Given: #{last.inspect}" unless last.is_a?(Time)

  # auto sort the mess… Yes, I'm very kind to these people
  if at > last
    aat = at
    at = last
    last = aat
  end

  from_station = EasyTransilien::Station.find(from).first
  to_station   = EasyTransilien::Station.find(to).first

  raise "Can't find a Station from #{from.inspect}" unless from_station
  raise "Can't find a Station from #{to.inspect}"   unless to_station

  routes   = Transilien::Route.find(stop_area_external_code: {and: [from_station.external_code, to_station.external_code] }, check_order: 1)
  journeys = Transilien::VehicleJourney.find(route_external_code: routes.map(&:external_code), date: Transilien.date(at), start_time: Transilien.time(at), end_time: Transilien.time(last))

  trips = []

  journeys.each do |journey|
    item = new
    item.at            = at
    item.last          = last
    item.from_station  = from_station
    item.to_station    = to_station
    item.stops         = journey.stops.map do |ms_stop|
      s = EasyTransilien::Stop.new
      s.ms_stop = ms_stop
      s
    end.sort
    item.from_stop     = item.stops.select { |ts| ts.station_external_code == from_station.external_code }.first
    item.to_stop       = item.stops.select { |ts| ts.station_external_code == to_station.external_code   }.first
    next if item.from_stop.nil? || item.to_stop.nil? # drop item if this journey doesn't deserve our from_stop or to_stop
    item.access_time   = journey.access_time
    item.ms_journey    = journey
    item.mission       = journey.name
    item.start_time    = item.from_stop.ms_stop.stop_time
    item.arrival_time  = item.to_stop.ms_stop.stop_time

    trips << item

  end
  trips.sort
end

Instance Method Details

#<=>(another) ⇒ Object



120
121
122
# File 'lib/easy_transilien/trip.rb', line 120

def <=>(another)
  self.from_stop.time <=> another.from_stop.time
end

#external_codeObject



96
97
98
# File 'lib/easy_transilien/trip.rb', line 96

def external_code
  journey.external_code
end

#from_station_nameObject



100
101
102
# File 'lib/easy_transilien/trip.rb', line 100

def from_station_name
  from_station.name
end

#inspectObject



116
117
118
# File 'lib/easy_transilien/trip.rb', line 116

def inspect
  "<EasyTransilien::Trip:#{object_id} @access_time=#{access_time} @mission=#{mission} @from_stop=#{from_stop}, @to_stop=#{to_stop}>"
end

#line_codeObject



108
109
110
# File 'lib/easy_transilien/trip.rb', line 108

def line_code
  ms_journey.route.line.code
end

#to_sObject



112
113
114
# File 'lib/easy_transilien/trip.rb', line 112

def to_s
  "[#{mission}] #{from_stop} -> #{to_stop}"
end

#to_station_nameObject



104
105
106
# File 'lib/easy_transilien/trip.rb', line 104

def to_station_name
  to_station.name
end