module Lobstersbot::PluggableConnection

Public Class Methods

included(mod) click to toggle source
# File lib/lobstersbot/pluggable_connection.rb, line 18
def self.included(mod)
  mod.extend(ClassMethods)
end
triggers() click to toggle source
# File lib/lobstersbot/pluggable_connection.rb, line 14
def self.triggers
  @@triggers
end

Public Instance Methods

channel_message(sender, channel, message) click to toggle source
# File lib/lobstersbot/pluggable_connection.rb, line 42
def channel_message(sender, channel, message)
  match = nil

  _, _, handler = @@triggers.find do |trigger|
    match = message.match(trigger[1])
    !match.nil?
  end
  return unless match

  handler.call(self, channel, sender[:nick], match)
end
did_start_up() click to toggle source
# File lib/lobstersbot/pluggable_connection.rb, line 32
def did_start_up
  @memory = PStore.new(config_dir('memory.pstore'), true)

  # Sort the triggers by priority.
  @@triggers.sort! {|a, b| b[0] <=> a[0] }

  timer = Concurrent::TimerTask.new(execution_interval: 1) { evaluate(:frequently) }
  timer.execute
end
evaluate(group, *args) click to toggle source
# File lib/lobstersbot/pluggable_connection.rb, line 59
def evaluate(group, *args)
  matching = public_methods.select {|m| m.to_s.start_with?(group.to_s) }

  matching.each do |match|
    slice_name = match.to_s.sub("#{group}_", '').to_sym
    @memory.transaction do
      slice = @memory[slice_name] ||= {}
      self.send(match, slice, *args)
    end
  end
end
join_event(sender, channel) click to toggle source
# File lib/lobstersbot/pluggable_connection.rb, line 54
def join_event(sender, channel)
  response_proc = ->(msg) { privmsg("#{sender[:nick]}: #{msg}", channel) }
  evaluate(:seen, sender[:nick], response_proc)
end
respond(channel, nick, msg) click to toggle source
# File lib/lobstersbot/pluggable_connection.rb, line 28
def respond(channel, nick, msg)
  privmsg("#{nick}: #{msg}", channel)
end