module QldTrafficWatch
Constants
- PLUGIN
- STATE_KEY
Public Instance Methods
Source
# File lib/qld_traffic_watch.rb, line 50 def configure(network:, channel:, action:, types: QldConfig::DEFAULT_WATCH_TYPES, store: RabbotDb) state = load_state(network: network, channel: channel, store: store) if action == :on state["enabled"] = true state["types"] = types save_state(network: network, channel: channel, state: state, store: store) "QLD traffic watch ON for #{channel} โ #{types.join(', ')}" else state["enabled"] = false save_state(network: network, channel: channel, state: state, store: store) "QLD traffic watch OFF for #{channel}" end end
Source
# File lib/qld_traffic_watch.rb, line 14 def load_state(network:, channel:, store: RabbotDb) store.get_plugin_json( plugin: PLUGIN, key: STATE_KEY, network: network, channel: channel, default: { "enabled" => false, "types" => QldConfig::DEFAULT_WATCH_TYPES, "seen" => [] } ) end
Source
# File lib/qld_traffic_watch.rb, line 34 def parse_watch_args(parts) tokens = Array(parts).map(&:to_s) return { error: "Usage: !qld traffic watch on|off [crash flooding hazard roadworks]" } if tokens.empty? case tokens[0].downcase when "on" types = tokens[1..].map(&:to_s).reject(&:empty?) types = QldConfig::DEFAULT_WATCH_TYPES if types.empty? { action: :on, types: types } when "off" { action: :off } else { error: "Usage: !qld traffic watch on|off [crash flooding hazard roadworks]" } end end
Source
# File lib/qld_traffic_watch.rb, line 64 def poll_channels(bot, fetch: nil, store: RabbotDb, api_key: nil) messages = [] events = QldTraffic.fetch_events(fetch: fetch, api_key: api_key, past_hour: true) bot.config.channels.each do |channel_name| network = bot.config.server state = load_state(network: network, channel: channel_name, store: store) next unless state["enabled"] types = Array(state["types"]) types = QldConfig::DEFAULT_WATCH_TYPES if types.empty? filtered = QldTraffic.filter_events(events, types: types) seen = Array(state["seen"]) filtered.each do |row| next if row[:id].empty? || seen.include?(row[:id]) seen << row[:id] messages << { channel: channel_name, text: [ IrcFormat.report_header(tag: "QLD", title: "Traffic alert", style: QldTraffic::STYLE_TITLE), QldTraffic.format_alert_line(row), IrcFormat.report_footer("QLDTraffic ยท past hour") ].join("\n") } end state["seen"] = seen.last(100) save_state(network: network, channel: channel_name, state: state, store: store) end messages end
Source
# File lib/qld_traffic_watch.rb, line 24 def save_state(network:, channel:, state:, store: RabbotDb) store.set_plugin_json( plugin: PLUGIN, key: STATE_KEY, network: network, channel: channel, value: state ) end