Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle commands with spaces when using Controller #144

Merged
merged 3 commits into from
Jun 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
### 0.10.3 (date)

* Your contribution here.
* [#144](https://github.com/slack-ruby/slack-ruby-bot/pull/144): Support usage of commands with embedded spaces when using Controller methods - [@chuckremes](https://github.com/chuckremes).

### 0.10.2 (06/03/2017)

Expand Down
17 changes: 15 additions & 2 deletions lib/slack-ruby-bot/mvc/controller/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,11 @@ def register_controller(controller)

methods.each do |meth_name|
next if meth_name[0] == '_'
method_name = convert_method_name(meth_name)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a fairly poor choice of variable name, method_name vs. meth_name, just make it m.


# sprinkle a little syntactic sugar on top of existing `command` infrastructure
command_class.class_eval do
command meth_name.to_s do |client, data, match|
command method_name do |client, data, match|
controller.use_args(client, data, match)
controller.call_command
end
Expand All @@ -76,6 +77,12 @@ def internal_methods(controller)
controller = controller.superclass until controller.abstract?
controller.public_instance_methods(true)
end

private

def convert_method_name(name)
name.tr('_', ' ')
end
end

abstract!
Expand All @@ -102,9 +109,15 @@ def use_args(client, data, match)
# Determine the command issued and call the corresponding instance method
def call_command
verb = match.captures[match.names.index('command')]
verb = verb.downcase if verb
verb = normalize_command_string(verb)
public_send(verb)
end

private

def normalize_command_string(string)
string.downcase.tr(' ', '_')
end
end
end
end
Expand Down
26 changes: 26 additions & 0 deletions spec/slack-ruby-bot/mvc/controller/controller_to_command_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,29 @@ def quxo
expect(instance.__flag).to be_truthy
end
end

describe SlackRubyBot::MVC::Controller::Base, 'command text conversion' do
let(:controller) do
Class.new(SlackRubyBot::MVC::Controller::Base) do
def quxo_foo_bar
client.say(channel: data.channel, text: "#{match[:command].downcase}: #{match[:expression]}")
end
end
end

after(:each) { controller.reset! }

it 'converts a command with spaces into a controller method with underscores separating the tokens' do
model = SlackRubyBot::MVC::Model::Base.new
view = SlackRubyBot::MVC::View::Base.new
controller.new(model, view)
expect(message: " #{SlackRubyBot.config.user} quxo foo bar red").to respond_with_slack_message('quxo foo bar: red')
end

it 'converts a command with upper case letters into a lower case method call' do
model = SlackRubyBot::MVC::Model::Base.new
view = SlackRubyBot::MVC::View::Base.new
controller.new(model, view)
expect(message: " #{SlackRubyBot.config.user} Quxo Foo Bar red").to respond_with_slack_message('quxo foo bar: red')
end
end