lib/ratis/route.rb in ratis-3.2.1 vs lib/ratis/route.rb in ratis-3.3.0

- old
+ new

@@ -1,31 +1,37 @@ module Ratis - class Route - attr_accessor :short_name, :directions - def self.all - response = Request.get 'Allroutes' - return [] unless response.success? - - routes = response.to_hash[:allroutes_response][:routes].split(/\n/) - atis_routes = routes.map do |r| - r.strip! - next if r.blank? - r = r.split(/,/) - Route.new r[0].strip, r[1..-1].map(&:strip) - end - atis_routes.compact - end - def initialize(short_name, directions) self.short_name = short_name self.directions = directions end def timetable(conditions) Timetable.where conditions.merge :route_short_name => short_name end end + class Routes + def self.all + response = Request.get 'Allroutes2' + + return [] unless response.success? + + # {:hexcolor=>"0", :route=>"0", :operator=>"AP", :description=>"0 - route", :publicroute=>"0", :hasrealtime=>"Y", :direction=>"*", :operatorname=>"VEOLIA-PHOENIX", :color=>"#000000"} + routes = {} + response.to_hash[:allroutes2_response][:routes][:item].each do |item| + if routes.has_key? item[:route] + routes[item[:route]] << item + else + routes[item[:route]] = [item] + end + end + + routes.map do |short_name, _routes| + directions = _routes.map{|rte| rte[:direction] }.reject{|dir| dir == '*' } + Route.new(short_name, directions) + end.compact.sort_by{|rte| rte.short_name } + end + end end