Cognition
This is a gem that parses a message, and compares it to various matchers. When it finds the first match, it executes an associated block of code or method, returning the output of whatever was run.
Installation
Add this line to your application's Gemfile:
gem 'cognition'
And then execute:
$ bundle
Or install it yourself as:
$ gem install cognition
Usage
Process your message:
result = Cognition.process('command I need to process')
You can also include metadata with your message, like user info, or whatever:
result = Cognition.process('another command', {user_id: 15, name: 'Bob'})
Internally, Cognition will turn your values into a Cognition::Message so
the metadata will be passed along with the message, and arbitrary metadata
is available in the #metadata Hash:
msg = Cognition::Message('another command', {user_id: 15, name: 'Bob'})
msg. # Returns { user_id: 15, name: 'Bob' }
Creating a Plugin
Creating plugins is easy. Subclass Cognition::Plugins::Base and setup your
matches and logic that should be run:
class Hello < Cognition::Plugins::Base
# Simple string based matcher. Must match *EXACTLY*
match 'hello', 'hello: Returns Hello World', :hello
# Advanced Regexp based matcher. Capture groups are made available
# via MatchData in the matches method
match /hello\s*(?<name>.*)/, 'hello <name>', :hello_person
def hello(*)
'Hello World'
end
def hello_person(msg)
name = msg.matches[:name]
"Hello #{name}"
end
end
Contributing
- Fork it ( https://github.com/anoldguy/cognition/fork )
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create a new Pull Request