lib/ratis/next_bus.rb in ratis-3.1.3 vs lib/ratis/next_bus.rb in ratis-3.1.4

- old
+ new

@@ -20,30 +20,44 @@ :times => service[:times], :direction => service[:direction], :servicetype => service[:servicetype], :route => service[:route], :operator => service[:operator], - :trips => service[:tripinfo].map do |ti| - OpenStruct.new(:triptime => ti[:triptime], - :block => ti[:block], - :tripid => ti[:tripid], - :exception => ti[:exception], - :skedtripid => ti[:skedtripid], - :realtime => OpenStruct.new(:valid => ti[:realtime][:valid], - :adherence => ti[:realtime][:adherence], - :estimatedtime => ti[:realtime][:estimatedtime], - :estimatedminutes => ti[:realtime][:estimatedminutes], - :polltime => ti[:realtime][:polltime], - :trend => ti[:realtime][:trend], - :speed => ti[:realtime][:speed], - :reliable => ti[:realtime][:reliable], - :stopped => ti[:realtime][:stopped], - :lat => ti[:realtime][:lat], - :long => ti[:realtime][:long] )) - end + :trips => parse_trip_info(service[:tripinfo]) ) end + end + + def parse_trip_info(trips) + # can come back as an Array or single Hash... + if trips.is_a?(Array) + trips.map do |ti| + create_trip(ti) + end + else # Hash + [create_trip(trips)] + end + end + + # TODO: turn into real classes + def create_trip(trip) + OpenStruct.new(:triptime => trip[:triptime], + :block => trip[:block], + :tripid => trip[:tripid], + :exception => trip[:exception], + :skedtripid => trip[:skedtripid], + :realtime => OpenStruct.new(:valid => trip[:realtime][:valid], + :adherence => trip[:realtime][:adherence], + :estimatedtime => trip[:realtime][:estimatedtime], + :estimatedminutes => trip[:realtime][:estimatedminutes], + :polltime => trip[:realtime][:polltime], + :trend => trip[:realtime][:trend], + :speed => trip[:realtime][:speed], + :reliable => trip[:realtime][:reliable], + :stopped => trip[:realtime][:stopped], + :lat => trip[:realtime][:lat], + :long => trip[:realtime][:long] )) end def self.where(conditions) stop_id = conditions.delete(:stop_id) app_id = conditions.delete(:app_id) || 'ratis-gem'