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

- old
+ new

@@ -1,20 +1,50 @@ module Ratis class NextBus - attr_accessor :runs, :status, :sign, :routetype, :times, :direction, :stop + attr_accessor :stop, :services, :success - def initialize(service, _runs = [], _stop = {}) - @runs = _runs - @stop = _stop + def initialize(response) + @success = response.success? + @stop = response.body[:nextbus_response][:atstop] + _services = @stop.delete(:service) - @status = service[:status] - @sign = service[:sign] - @routetype = service[:routetype] - @times = service[:times] - @direction = service[:direction] + unless _services.is_a?(Array) + _services = [_services] + end + + @services = _services.map do |service| + OpenStruct.new(:status => service[:status], + :sign => service[:sign], + :routetype => service[:routetype], + :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 + ) + end + end def self.where(conditions) stop_id = conditions.delete(:stop_id) app_id = conditions.delete(:app_id) || 'ratis-gem' @@ -26,23 +56,22 @@ datetime = Time.now end raise ArgumentError.new('You must provide a stop ID') unless stop_id - Ratis.all_conditions_used? conditions + Ratis.all_conditions_used?(conditions) response = Request.get 'Nextbus', {'Stopid' => stop_id, 'Appid' => app_id, 'Date' => datetime.strftime("%m/%d/%Y"), 'Time' => datetime.strftime("%I%M"), 'Type' => type } - return [] unless response.success? - stop = response.body[:nextbus_response][:atstop] - service = stop.delete(:service) - runs = service.delete(:tripinfo) + NextBus.new(response) + end - NextBus.new service, runs, stop + def success? + @success end # Gets description of first stop # @return [String] Description of first stop or nil.