module SunbusRssRoutes
Constants
- ROUTE_LINK_PATTERN
Public Instance Methods
Source
# File lib/sunbus/rss_routes.rb, line 33 def matching_items(items, query, region:) regional = SunbusRss.filter_by_region(items, region: region) regional.select { |item| title_matches_query?(item[:title], query) } end
Source
# File lib/sunbus/rss_routes.rb, line 18 def parse_route_from_link(link) match = link.to_s.match(ROUTE_LINK_PATTERN) return nil unless match { prefix: match[1].upcase, number: match[2] } end
Source
# File lib/sunbus/rss_routes.rb, line 38 def route_numbers_from_items(items) items.filter_map { |item| parse_route_from_link(item[:link])&.fetch(:number) }.uniq end
Source
# File lib/sunbus/rss_routes.rb, line 61 def stops_for_query(query, feed:, region:, fetch:, limit: SunbusConfig::MAX_STOPS) body = SunbusRss.fetch_feed(SunbusRss::FEED_BUS, fetch: fetch) items = matching_items(SunbusRss.parse_items(body), query, region: region) route_numbers = route_numbers_from_items(items) stops_on_routes(feed, route_numbers, limit: limit) end
Source
# File lib/sunbus/rss_routes.rb, line 42 def stops_on_routes(feed, route_numbers, limit:) return [] if route_numbers.empty? numbers = route_numbers.map(&:to_s).to_set route_ids = feed[:routes].each_value.filter_map do |route| route[:route_id] if numbers.include?(route[:route_short_name].to_s) end return [] if route_ids.empty? trip_ids = feed[:trips].each_value.filter_map do |trip| trip[:trip_id] if route_ids.include?(trip[:route_id]) end stop_ids = trip_ids.flat_map do |trip_id| feed[:stop_times][trip_id].map { |row| row[:stop_id] } end.uniq feed[:stops].select { |stop| stop_ids.include?(stop[:stop_id]) }.first(limit) end
Source
# File lib/sunbus/rss_routes.rb, line 25 def title_matches_query?(title, query) name = SunbusStopSearch.normalize_text(title) tokens = SunbusStopSearch.query_tokens(query) return false if tokens.empty? tokens.all? { |token| name.include?(token) } end