Sha256: aa5e40ccf452a86c89d991a89a115d8933d861c21a4043b32c3659691234e8c0

Contents?: true

Size: 1.43 KB

Versions: 5

Compression:

Stored size: 1.43 KB

Contents

#
# Actions are run in the context of a Scamp::Action.
# This allows us to make room, user etc. methods
# available on a per-message basis
#

# {:room_id=>401839, :created_at=>"2011/09/10 00:23:19 +0000", :body=>"something", :id=>408089344, :user_id=>774016, :type=>"TextMessage"}

class Scamp
  class Action
    
    attr_accessor :matches, :bot
    
    def initialize(bot, action, message)
      @bot = bot
      @action = action
      @message = message
    end
    
    def matches=(match)
      @matches = match[1..-1]
      match.names.each do |name|
        name_s = name.to_sym
        self.class.send :define_method, name_s do
          match[name_s]
        end
      end if match.respond_to?(:names) # 1.8 doesn't support named captures
    end
    
    def room_id
      @message[:room_id]
    end
    
    def room
      bot.room_name_for @message[:room_id]
    end
    
    def user
      bot.username_for(@message[:user_id])
    end
    
    def user_id
      @message[:user_id]
    end
    
    def message
      @message[:body]
    end
    
    def run
      self.instance_eval &@action
    end
    
    def command_list
      bot.command_list
    end
    
    def say(msg, room_id_or_name = room_id)
      bot.say(msg, room_id_or_name)
    end

    def paste(msg, room_id_or_name = room_id)
      bot.paste(msg, room_id_or_name)
    end

    def play(sound, room_id_or_name = room_id)
      bot.play(sound, room_id_or_name)
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
scamp-1.2.0 lib/scamp/action.rb
scamp-1.1.0 lib/scamp/action.rb
scamp-1.0.1 lib/scamp/action.rb
scamp-1.0.0 lib/scamp/action.rb
scamp-0.2.1 lib/scamp/action.rb