Sha256: a5e9f3db22b2d5e3c74917a81aa60a8a65d51c1425e386245cd4d7894cd3b2a0

Contents?: true

Size: 1.6 KB

Versions: 1

Compression:

Stored size: 1.6 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') unless date ^ service_type
      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]

      stop = headway[:timepoints][:stop]
      timetable.timepoints = [ Timetable::Stop.new(stop[:atisstopid], stop[:stopid], stop[:description], stop[:area]) ]

      trip = headway[:times][:trip]
      timetable.trips = [ Timetable::Trip.new(trip[:time], trip[:comment]) ]

      timetable
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ratis-2.5.2.5 lib/ratis/timetable.rb