Class: Waylon::Skill
- Inherits:
-
Object
- Object
- Waylon::Skill
- Includes:
- BaseComponent
- Defined in:
- lib/waylon/skill.rb
Overview
Skills are what Waylon can do based on inputs from Senses
Direct Known Subclasses
Waylon::Skills::Default, Waylon::Skills::Diagnostics, Waylon::Skills::Fun, Waylon::Skills::Groups, Waylon::Skills::Help
Instance Attribute Summary collapse
-
#request ⇒ Object
readonly
Returns the value of attribute request.
-
#route ⇒ Object
readonly
Returns the value of attribute route.
-
#sense ⇒ Object
readonly
Returns the value of attribute sense.
-
#tokens ⇒ Object
readonly
Returns the value of attribute tokens.
Class Method Summary collapse
-
.config_namespace ⇒ String
Config namespace for config keys.
-
.perform(details) ⇒ Object
Resque uses this to execute the Skill.
-
.queue ⇒ Symbol
private
Redis/Resque queue name.
-
.route(condition, action, allowed_groups: :everyone, help: nil, name: nil, mention_only: true) ⇒ Object
Adds skills to the SkillRegistry.
Instance Method Summary collapse
-
#acknowledgement ⇒ String
Provides a random entry from a list of canned acknowledgements.
-
#codify(text) ⇒ String
Provides a simple way to convert some text to a code block in a way the Sense understands.
-
#details ⇒ Hash
The details used to call this Skill.
-
#initialize(sense, route, request, meta) ⇒ Skill
constructor
A new instance of Skill.
-
#mention(user) ⇒ String
Defers to the Sense to determine how to mention a user.
-
#message ⇒ Waylon::Message
Provides a wrapped message for responding to the received Sense.
- #named_tokens ⇒ Object
-
#react(type) ⇒ Boolean, String
Defers to the Sense to react to a message.
-
#reply(text, private: false) ⇒ Object
Defers to the Sense to determine how to reply to a message.
-
#reply_with_blocks(blocks, private: false) ⇒ Object
Defers to the Sense to determine how to reply to a message with rich content.
-
#threaded_reply(text) ⇒ Object
Defers to the Sense to do threaded replies if it can, otherwise it falls back to normal replies.
Methods included from BaseComponent
Constructor Details
#initialize(sense, route, request, meta) ⇒ Skill
Returns a new instance of Skill.
52 53 54 55 56 57 58 |
# File 'lib/waylon/skill.rb', line 52 def initialize(sense, route, request, ) @sense = sense.is_a?(Class) ? sense : Module.const_get(sense) @route = SkillRegistry.find_by_name(route) @request = request @tokens = @route.tokens(.body) || [] @meta = end |
Instance Attribute Details
#request ⇒ Object (readonly)
Returns the value of attribute request.
8 9 10 |
# File 'lib/waylon/skill.rb', line 8 def request @request end |
#route ⇒ Object (readonly)
Returns the value of attribute route.
8 9 10 |
# File 'lib/waylon/skill.rb', line 8 def route @route end |
#sense ⇒ Object (readonly)
Returns the value of attribute sense.
8 9 10 |
# File 'lib/waylon/skill.rb', line 8 def sense @sense end |
#tokens ⇒ Object (readonly)
Returns the value of attribute tokens.
8 9 10 |
# File 'lib/waylon/skill.rb', line 8 def tokens @tokens end |
Class Method Details
.config_namespace ⇒ String
Config namespace for config keys
12 13 14 |
# File 'lib/waylon/skill.rb', line 12 def self.config_namespace "skills.#{component_namespace}" end |
.perform(details) ⇒ Object
Resque uses this to execute the Skill. Just defers to the ‘action` subclass method
18 19 20 21 |
# File 'lib/waylon/skill.rb', line 18 def self.perform(details) skill = new(details["sense"], details["route"], details["request"], details["meta"]) skill.send(skill.route.action.to_sym) end |
.queue ⇒ Symbol
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Redis/Resque queue name
26 27 28 |
# File 'lib/waylon/skill.rb', line 26 def self.queue :skills end |
.route(condition, action, allowed_groups: :everyone, help: nil, name: nil, mention_only: true) ⇒ Object
Adds skills to the SkillRegistry
35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/waylon/skill.rb', line 35 def self.route(condition, action, allowed_groups: :everyone, help: nil, name: nil, mention_only: true) name ||= "#{to_s.split("::").last.downcase}##{action}" real_cond = case condition when Condition condition when Regexp Conditions::Regex.new(condition, action, allowed_groups, help, mention_only) else log("Unknown condition for route for #{name}##{action}", :warn) nil end SkillRegistry.instance.register(name, self, real_cond) if real_cond end |
Instance Method Details
#acknowledgement ⇒ String
Provides a random entry from a list of canned acknowledgements
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/waylon/skill.rb', line 62 def acknowledgement responses = [ "You got it!", "As you wish.", "Certainly!", "Sure thing!", "Absolutely!", "No problem.", "Consider it done.", "Of course!", "I'd be delighted!", "Right away!", "Gladly!", "All right.", "I'm all over it.", "I'm on it!", "Will do!" ] responses.sample end |
#codify(text) ⇒ String
Provides a simple way to convert some text to a code block in a way the Sense understands
86 87 88 |
# File 'lib/waylon/skill.rb', line 86 def codify(text) sense.codify(text) end |
#details ⇒ Hash
The details used to call this Skill
92 93 94 95 96 97 98 99 100 |
# File 'lib/waylon/skill.rb', line 92 def details { sense: @sense, message: @request, tokens: @tokens, meta: @meta, route: route.name } end |
#mention(user) ⇒ String
Defers to the Sense to determine how to mention a user
105 106 107 |
# File 'lib/waylon/skill.rb', line 105 def mention(user) sense.mention(user) end |
#message ⇒ Waylon::Message
Provides a wrapped message for responding to the received Sense
111 112 113 |
# File 'lib/waylon/skill.rb', line 111 def sense.(request) end |
#named_tokens ⇒ Object
115 116 117 |
# File 'lib/waylon/skill.rb', line 115 def named_tokens @named_tokens ||= @route.named_tokens(.body) || {} end |
#react(type) ⇒ Boolean, String
Defers to the Sense to react to a message
122 123 124 125 126 |
# File 'lib/waylon/skill.rb', line 122 def react(type) return false unless sense.supports?(:reactions) sense.react(request, type) end |
#reply(text, private: false) ⇒ Object
Defers to the Sense to determine how to reply to a message
130 131 132 133 134 135 136 137 |
# File 'lib/waylon/skill.rb', line 130 def reply(text, private: false) if private && sense.supports?(:private_messages) sense.private_reply(request, text) else log("Unable to send private message for Sense #{sense.name}, replying instead", :debug) if private sense.reply(request, text) end end |
#reply_with_blocks(blocks, private: false) ⇒ Object
Defers to the Sense to determine how to reply to a message with rich content
141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/waylon/skill.rb', line 141 def reply_with_blocks(blocks, private: false) unless sense.supports?(:blocks) log("Unable to use blocks with Sense #{sense.name}") return false end if private sense.private_reply_with_blocks(request, blocks) else log("Unable to send private message for Sense #{sense.name}, replying instead", :debug) if private sense.reply_with_blocks(request, blocks) end end |
#threaded_reply(text) ⇒ Object
Defers to the Sense to do threaded replies if it can, otherwise it falls back to normal replies
156 157 158 159 160 161 162 163 |
# File 'lib/waylon/skill.rb', line 156 def threaded_reply(text) if sense.supports?(:threads) sense.threaded_reply(request, text) else log("Unable to reply in theads for Sense #{sense.name}, replying instead", :debug) sense.reply(request, text) end end |