Class: Boty::Action

Inherits:
Object
  • Object
show all
Includes:
Logger
Defined in:
lib/boty/action.rb

Overview

Public: Wrap the idea of something that should happen when a regex matches. Actions are the underlying mecanism for store and execute the blocks passed to ‘Bot#match` and `Bot#respond` invocations.

Maybe one of the more important things to remember here is that an Action is executed within the scope of a ‘Boty::DSL` instance already binded with the `Bot` of the current `Session` (via `#instance_exec`).

Examples:

# some_script.rb
desc "omg! whoo!"
hear(/omg/i) do
  im "whoo!"
end

# Will generate an `Action` like this:
action = Action.new(bot, /omg/i, "omg! whoo!") { im "whoo!" }

# Then this can be executed, by `#execute`.
action.execute "omg! this string triggers the hear block."

# If the regex matches the parameter passed to execute, it'll invoke the
# block passed to `#hear`.

Instance Attribute Summary collapse

Attributes included from Logger

#logger

Instance Method Summary collapse

Methods included from Logger

adapter, adapter=, #log_error

Constructor Details

#initialize(bot, regex, description, &action) ⇒ Action

Public: Initialize an ‘Action` associated with a specific `Bot`.

bot - A Bot that will react to the matches on the regex. regex - A Regexp to match against messages received in the bot session. action - A Proc that will be executed everytime the regex matches.



35
36
37
38
39
40
41
# File 'lib/boty/action.rb', line 35

def initialize(bot, regex, description, &action)
  @dsl    = DSL.new bot
  @regex  = regex
  @action = action

  self.desc = description
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



28
29
30
# File 'lib/boty/action.rb', line 28

def action
  @action
end

#descObject

Returns the value of attribute desc.



28
29
30
# File 'lib/boty/action.rb', line 28

def desc
  @desc
end

#regexObject (readonly)

Returns the value of attribute regex.



28
29
30
# File 'lib/boty/action.rb', line 28

def regex
  @regex
end

Instance Method Details

#execute(message) ⇒ Object

Public: Creates a ‘Regexp::Match` based on the internal `regex` against a Slack::Message. Then pass this match to be used as argument to the action per se.

message - A Slack::Message object to be matched against the ‘regex`.

Returns nothing.



69
70
71
72
73
74
# File 'lib/boty/action.rb', line 69

def execute(message)
  action_call regex.match(message.text)
rescue => e
  logger.error e.message
  raise e
end

#this?(regex, block) ⇒ Boolean

Public: Check if a given Regexp and an optional block were the same used to build this Action.

Examples:

send_im = -> { im "whoo!" }
action = Action.new(bot, /omg/i, "omg! whoo!", &send_im)
action.this? /omg/i, send_im
# => return true

regex - A Regexp to be compared against ‘regex`. block - An optional Proc to be checked against `action`.

Returns True or False.

Returns:

  • (Boolean)


57
58
59
60
# File 'lib/boty/action.rb', line 57

def this?(regex, block)
  same_regex = regex == self.regex
  block ? same_regex && block == action : same_regex
end