module QldTranslinkGtfs
Public Instance Methods
Source
# File lib/qld_translink_gtfs.rb, line 36 def cached_zip_path File.join(QldConfig::GTFS_CACHE_DIR, "SEQ_GTFS.zip") end
Source
# File lib/qld_translink_gtfs.rb, line 23 def ensure_stops!(fetch: nil, time: Time.now) return @stops if @stops && @stops_loaded_at && (time - @stops_loaded_at) < QldConfig::GTFS_CACHE_MAX_AGE_SEC path = cached_zip_path refresh_zip!(path, fetch: fetch, time: time) unless zip_fresh?(path, time: time) @stops = parse_stops_zip(path) @stops_loaded_at = time @stops rescue StandardError @stops = [] @stops end
Source
# File lib/qld_translink_gtfs.rb, line 90 def find_stop(query, fetch: nil) search_stops(query, fetch: fetch, limit: 1).first end
Source
# File lib/qld_translink_gtfs.rb, line 55 def parse_stops_zip(path) return [] unless File.file?(path) rows = [] Zip::File.open(path) do |zip| entry = zip.find_entry("stops.txt") return [] unless entry CSV.parse(entry.get_input_stream.read, headers: true).each do |row| rows << { stop_id: row["stop_id"].to_s.strip, stop_code: row["stop_code"].to_s.strip, name: row["stop_name"].to_s.strip, parent_station: row["parent_station"].to_s.strip } end end rows end
Source
# File lib/qld_translink_gtfs.rb, line 44 def refresh_zip!(path, fetch: nil, time: Time.now) FileUtils.mkdir_p(File.dirname(path)) body = if fetch fetch.call(QldConfig::GTFS_ZIP_URL) else RabbotHttp.fetch(QldConfig::GTFS_ZIP_URL, user_agent: "Rabbot/1.0 (qld translink gtfs)") end File.binwrite(path, body) File.utime(time, time, path) end
Source
# File lib/qld_translink_gtfs.rb, line 13 def reset_stops! @stops = nil @stops_loaded_at = nil end
Source
# File lib/qld_translink_gtfs.rb, line 75 def search_stops(query, fetch: nil, limit: QldConfig::MAX_STOPS) needle = query.to_s.strip.downcase return [] if needle.empty? ranked = stops(fetch: fetch).filter_map do |row| name = row[:name].downcase next unless name.include?(needle) || needle.split.all? { |part| name.include?(part) } score = name.start_with?(needle) ? 0 : 1 score -= 1 if name.include?("station") || name.include?("platform") [score, row] end ranked.sort_by { |score, _| score }.map(&:last).first(limit) end
Source
# File lib/qld_translink_gtfs.rb, line 18 def stops(fetch: nil, time: Time.now) ensure_stops!(fetch: fetch, time: time) @stops end
Source
# File lib/qld_translink_gtfs.rb, line 40 def zip_fresh?(path, time: Time.now) File.file?(path) && (time - File.mtime(path)) < QldConfig::GTFS_CACHE_MAX_AGE_SEC end