diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c76db3..4883d9c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/lib/slack-ruby-bot/mvc/controller/base.rb b/lib/slack-ruby-bot/mvc/controller/base.rb index 1d0f5ed..9e28b3a 100644 --- a/lib/slack-ruby-bot/mvc/controller/base.rb +++ b/lib/slack-ruby-bot/mvc/controller/base.rb @@ -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) # 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 @@ -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! @@ -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 diff --git a/spec/slack-ruby-bot/mvc/controller/controller_to_command_spec.rb b/spec/slack-ruby-bot/mvc/controller/controller_to_command_spec.rb index 7827b18..1f13728 100644 --- a/spec/slack-ruby-bot/mvc/controller/controller_to_command_spec.rb +++ b/spec/slack-ruby-bot/mvc/controller/controller_to_command_spec.rb @@ -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