module QldTraffic
Constants
- STYLE_TITLE
Public Instance Methods
Source
# File lib/qld_traffic.rb, line 14 def fetch_events(fetch: nil, api_key: nil, past_hour: false) key = QldConfig.traffic_api_key(key: api_key) base = past_hour ? QldConfig.traffic_past_hour_url : QldConfig.traffic_events_url url = "#{base}?apikey=#{key}" body = if fetch fetch.call(url) else RabbotHttp.fetch(url, user_agent: "Rabbot/1.0 (qld traffic)") end parse_geojson(JSON.parse(body)) rescue StandardError [] end
Source
# File lib/qld_traffic.rb, line 55 def filter_events(events, query: nil, types: nil) rows = Array(events) if types && !types.empty? allowed = types.map { |t| normalize_type(t) } rows = rows.select do |row| type = normalize_type(row[:event_type]) subtype = normalize_type(row[:event_subtype]) allowed.any? { |token| type.include?(token) || subtype.include?(token) || token.include?(type) } end end return rows if query.to_s.strip.empty? needle = query.to_s.strip.downcase rows.select do |row| haystack = [row[:locality], row[:road], row[:description], row[:event_type], row[:event_subtype]] .join(" ") .downcase haystack.include?(needle) end end
Source
# File lib/qld_traffic.rb, line 109 def format_alert_line(row) parts = [row[:event_type]] parts << row[:event_subtype] unless row[:event_subtype].empty? loc = [row[:road], row[:locality]].reject(&:empty?).join(" ยท ") line = "#{parts.join(' โ ')}" line += " โ #{loc}" unless loc.empty? line += " โ #{row[:description][0, 100]}" unless row[:description].to_s.empty? line end
Source
# File lib/qld_traffic.rb, line 80 def format_events(events, title: "QLD traffic") if events.empty? return IrcReply.card( tag: "QLD", title: title, body_lines: ["No matching road events found."], heading_style: STYLE_TITLE ) end lines = events.first(QldConfig::MAX_EVENTS).map.with_index(1) do |row, index| parts = [row[:event_type]] parts << row[:event_subtype] unless row[:event_subtype].empty? loc = [row[:road], row[:locality]].reject(&:empty?).join(" ยท ") text = "#{index}. #{parts.join(' โ ')}" text += " โ #{loc}" unless loc.empty? desc = row[:description] text += " โ #{desc[0, 80]}" unless desc.empty? text end IrcReply.card( tag: "QLD", title: title, body_lines: lines, footer_parts: ["#{events.length} events", "QLDTraffic ยท TMR"], heading_style: STYLE_TITLE ) end
Source
# File lib/qld_traffic.rb, line 76 def normalize_type(text) text.to_s.strip.downcase.gsub(/[^a-z]/, "") end
Source
# File lib/qld_traffic.rb, line 38 def parse_event(props) return nil unless props.is_a?(Hash) summary = props["road_summary"].is_a?(Hash) ? props["road_summary"] : {} impact = props["impact"].is_a?(Hash) ? props["impact"] : {} { id: props["id"].to_s, event_type: props["event_type"].to_s.strip, event_subtype: props["event_subtype"].to_s.strip, description: props["description"].to_s.squeeze(" ").strip, locality: summary["locality"].to_s.strip, road: summary["road_name"].to_s.strip, impact: impact["impact_type"].to_s.strip, status: props["status"].to_s.strip } end
Source
# File lib/qld_traffic.rb, line 28 def parse_geojson(payload) return [] unless payload.is_a?(Hash) Array(payload["features"]).filter_map do |feature| next unless feature.is_a?(Hash) parse_event(feature["properties"]) end end
Source
# File lib/qld_traffic.rb, line 119 def reachable?(fetch: nil, api_key: nil) events = fetch_events(fetch: fetch, api_key: api_key) !events.nil? rescue StandardError false end