Sha256: 9bc4c715612f07ee7974a72da509b440e24faccd4b3522baa5e916d27459b18d

Contents?: true

Size: 1.83 KB

Versions: 13

Compression:

Stored size: 1.83 KB

Contents

module Ratis

  class Timetable
    attr_accessor :route_short_name, :direction, :service_type, :operator, :effective, :timepoints, :trips

    def self.where(conditions)
      short_name   = conditions.delete :route_short_name
      direction    = conditions.delete :direction
      date         = conditions.delete :date
      service_type = conditions.delete :service_type

      raise ArgumentError.new('You must provide a route_short_name') unless short_name
      raise ArgumentError.new('You must provide a direction') unless direction
      raise ArgumentError.new('You must provide either date or service_type') if date.blank? && service_type.blank?

      Ratis.all_conditions_used? conditions

      request_params = { 'Route' => short_name, 'Direction' => direction }
      request_params.merge! date ? { 'Date' => date } : { 'Servicetype' => service_type }

      response = Request.get 'Timetable', request_params

      return nil unless response.success?

      headway = response.to_hash[:timetable_response][:headway]

      timetable = Timetable.new
      timetable.route_short_name = headway[:route]
      timetable.direction        = headway[:direction]
      timetable.service_type     = headway[:servicetype]
      timetable.operator         = headway[:operator]
      timetable.effective        = headway[:effective]

      timepoints_array = []

      headway[:timepoints][:stop].each_with_index do |tp, i|
        timepoints_array << Timetable::Stop.new(i, tp[:atisstopid], tp[:stopid], tp[:description], tp[:area])
      end rescue []

      timetable.timepoints = timepoints_array

      trips_array = []

      headway[:times][:trip].each_with_index do |t,i|
        trips_array << Timetable::Trip.new(i, t[:time], t[:comment], t[:sign])
      end rescue []

      timetable.trips            =  trips_array

      timetable
    end



  end

end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
ratis-3.4.3 lib/ratis/timetable.rb
ratis-3.4.2 lib/ratis/timetable.rb
ratis-3.4.1 lib/ratis/timetable.rb
ratis-3.4.0 lib/ratis/timetable.rb
ratis-3.3.7 lib/ratis/timetable.rb
ratis-3.3.6 lib/ratis/timetable.rb
ratis-3.3.5 lib/ratis/timetable.rb
ratis-3.3.4 lib/ratis/timetable.rb
ratis-3.3.3 lib/ratis/timetable.rb
ratis-3.3.2 lib/ratis/timetable.rb
ratis-3.3.1 lib/ratis/timetable.rb
ratis-3.3.0 lib/ratis/timetable.rb
ratis-3.2.1 lib/ratis/timetable.rb