module GovauTfnsw
Constants
- BASE_URL
- STYLE_TITLE
- USER_AGENT
Public Instance Methods
Source
# File lib/govau_tfnsw.rb, line 22 def auth_headers(api_key:) { "Authorization" => "apikey #{api_key}" } end
Source
# File lib/govau_tfnsw.rb, line 116 def delay_seconds(planned, estimated) return 0 if planned.to_s.strip.empty? || estimated.to_s.strip.empty? (Time.parse(estimated.to_s) - Time.parse(planned.to_s)).to_i rescue StandardError 0 end
Source
# File lib/govau_tfnsw.rb, line 49 def departure_url(stop_id:, when_time: Time.now) local = when_time.getlocal("+10:00") date = local.strftime("%Y%m%d") time = local.strftime("%H%M") "#{BASE_URL}/departure_mon?outputFormat=rapidJSON&type_dm=any&name_dm=#{CGI.escape(stop_id.to_s)}&itdDate=#{date}&itdTime=#{time}&TfNSWTR=true" end
Source
# File lib/govau_tfnsw.rb, line 30 def fetch(url, api_key:, fetch: nil) headers = request_headers(api_key: api_key) if fetch fetch.call(url, headers: headers) else RabbotHttp.fetch(url, user_agent: USER_AGENT, headers: headers) end end
Source
# File lib/govau_tfnsw.rb, line 84 def fetch_departures(stop_query, api_key:, fetch: nil, when_time: Time.now) stop = find_stop(stop_query, api_key: api_key, fetch: fetch) return [] unless stop url = departure_url(stop_id: stop[:id], when_time: when_time) payload = fetch_json(url, api_key: api_key, fetch: fetch) parse_departures(payload, stop_name: stop[:name]) rescue StandardError [] end
Source
# File lib/govau_tfnsw.rb, line 39 def fetch_json(url, api_key:, fetch: nil) body = fetch(url, api_key: api_key, fetch: fetch) JSON.parse(body) end
Source
# File lib/govau_tfnsw.rb, line 77 def find_stop(query, api_key:, fetch: nil) stops = search_stops(query, api_key: api_key, fetch: fetch) return nil if stops.empty? stops.find { |s| s[:name].match?(/station/i) } || stops.first end
Source
# File lib/govau_tfnsw.rb, line 141 def format_departures(stop_query, rows) lines = [ IrcFormat.report_header(tag: "GOVAU", title: "TfNSW departures ยท #{stop_query}", style: STYLE_TITLE) ] if rows.empty? lines << "No departures found." else rows.each do |row| time = format_time(row[:estimated_at]) lines << "#{time} โ #{row[:destination]}" end end lines << IrcFormat.report_footer("Transport for NSW") lines.join("\n") end
Source
# File lib/govau_tfnsw.rb, line 128 def format_stops(query, stops) lines = [ IrcFormat.report_header(tag: "GOVAU", title: "TfNSW stops ยท #{query}", style: STYLE_TITLE) ] if stops.empty? lines << "No stops found." else stops.first(5).each { |stop| lines << "#{stop[:name]} (#{stop[:id]})" } end lines << IrcFormat.report_footer("Transport for NSW") lines.join("\n") end
Source
# File lib/govau_tfnsw.rb, line 157 def format_time(value) return "?" if value.to_s.strip.empty? Time.parse(value.to_s).getlocal("+10:00").strftime("%H:%M") rescue StandardError value.to_s end
Source
# File lib/govau_tfnsw.rb, line 124 def on_time?(departure, threshold_sec: 120) departure[:delay_sec].to_i <= threshold_sec end
Source
# File lib/govau_tfnsw.rb, line 95 def parse_departures(payload, stop_name: nil) events = payload.dig("stopEvents") || payload.dig("journeys") || [] Array(events).first(4).filter_map do |row| next unless row.is_a?(Hash) dest = row.dig("transportation", "destination", "name") || row.dig("destination", "name") || row["direction"] planned = row["departureTimePlanned"] || row.dig("departureTime", "planned") estimated = row["departureTimeEstimated"] || row.dig("departureTime", "estimated") delay_sec = delay_seconds(planned, estimated) { stop: stop_name, destination: dest.to_s.strip, planned_at: planned, estimated_at: estimated || planned, delay_sec: delay_sec } end end
Source
# File lib/govau_tfnsw.rb, line 64 def parse_stops(payload) locations = payload.dig("locations") || payload.dig("stopFinder", "points") || [] Array(locations).filter_map do |row| next unless row.is_a?(Hash) id = row["id"] || row.dig("ref", "id") || row["name"] name = row["name"] || row.dig("ref", "name") next if id.to_s.empty? || name.to_s.empty? { id: id.to_s, name: name.to_s } end end
Source
# File lib/govau_tfnsw.rb, line 26 def request_headers(api_key:) auth_headers(api_key: api_key) end
Source
# File lib/govau_tfnsw.rb, line 56 def search_stops(query, api_key:, fetch: nil) url = stop_finder_url(query) payload = fetch_json(url, api_key: api_key, fetch: fetch) parse_stops(payload) rescue StandardError [] end
Source
# File lib/govau_tfnsw.rb, line 44 def stop_finder_url(query) q = CGI.escape(query.to_s.strip) "#{BASE_URL}/stop_finder?outputFormat=rapidJSON&type_sf=any&name_sf=#{q}&TfNSWTR=true" end