lib/easy_transilien/trip.rb in easy_transilien-0.0.4 vs lib/easy_transilien/trip.rb in easy_transilien-0.0.5
- old
+ new
@@ -12,38 +12,50 @@
attr_accessor :from_station, :to_station, :from_stop, :to_stop, :access_time
attr_accessor :start_time, :arrival_time
attr_accessor :mission
attr_accessor :ms_journey
- attr_accessor :at
+ attr_accessor :at, :last # boundaries given via #find options
attr_accessor :stops
class << self
# Find
# Options:
# * at (default Time.new)
- # * last (default at + 3600)
+ # * 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
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 + 3600
+ 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 = Station.find(from).first
to_station = 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
@@ -54,9 +66,10 @@
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