module QuakeWatch
Constants
- PLUGIN
- STATE_KEY
Public Instance Methods
Source
# File lib/quake_watch.rb, line 50 def configure(network:, channel:, action:, min_mag: 4.0, store: RabbotDb) state = load_state(network: network, channel: channel, store: store) if action == :on state["enabled"] = true state["min_mag"] = min_mag save_state(network: network, channel: channel, state: state, store: store) "Earthquake watch ON for #{channel} โ posting events M#{min_mag}+" else state["enabled"] = false save_state(network: network, channel: channel, state: state, store: store) "Earthquake watch OFF for #{channel}" end end
Source
# File lib/quake_watch.rb, line 15 def load_state(network:, channel:, store: RabbotDb) store.get_plugin_json( plugin: PLUGIN, key: STATE_KEY, network: network, channel: channel, default: { "enabled" => false, "min_mag" => 4.0, "seen" => [] } ) end
Source
# File lib/quake_watch.rb, line 35 def parse_watch_args(parts) tokens = Array(parts).map(&:to_s) return { error: "Usage: !quake watch on|off [min_mag]" } if tokens.empty? case tokens[0].downcase when "on" min_mag = tokens[1] ? tokens[1].to_f : 4.0 { action: :on, min_mag: min_mag } when "off" { action: :off } else { error: "Usage: !quake watch on|off [min_mag]" } end end
Source
# File lib/quake_watch.rb, line 64 def poll_channels(bot, fetch: nil, store: RabbotDb) messages = [] 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"] events = QuakeFeed.fetch_recent(fetch: fetch) min_mag = state["min_mag"].to_f seen = Array(state["seen"]) events.each do |event| next if event[:magnitude] < min_mag next if seen.include?(event[:id]) seen << event[:id] messages << { channel: channel_name, text: [ IrcFormat.report_header(tag: "QUAKE", title: "Earthquake alert", style: QuakeFeed::STYLE_TITLE), "M#{event[:magnitude]} โ #{event[:place]} โ #{QuakeFeed.format_time(event[:time])}", IrcFormat.report_footer("USGS ยท AU region") ].join("\n") } end state["seen"] = seen.last(50) save_state(network: network, channel: channel_name, state: state, store: store) end messages end
Source
# File lib/quake_watch.rb, line 25 def save_state(network:, channel:, state:, store: RabbotDb) store.set_plugin_json( plugin: PLUGIN, key: STATE_KEY, network: network, channel: channel, value: state ) end