Skip to content

Commit

Permalink
Handle commands with spaces when using Controller (#144)
Browse files Browse the repository at this point in the history
* specs for converting commands with spaces to method names

* handle translation of commands with spaces to proper controller method names

* add text describing changes
  • Loading branch information
chuckremes authored and dblock committed Jun 8, 2017
1 parent 23bce78 commit 612890b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
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)

# 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

0 comments on commit 612890b

Please sign in to comment.