From c0166da47a545859da7f098cb3377a412f7bf99a Mon Sep 17 00:00:00 2001 From: kuboshizuma Date: Wed, 25 Nov 2015 11:05:30 +0900 Subject: [PATCH] Fix RegexpError when parsing command --- lib/slack-ruby-bot/commands/base.rb | 5 +++-- .../commands/commands_regexp_escape_spec.rb | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 spec/slack-ruby-bot/commands/commands_regexp_escape_spec.rb diff --git a/lib/slack-ruby-bot/commands/base.rb b/lib/slack-ruby-bot/commands/base.rb index 47af56b..a08b4b7 100644 --- a/lib/slack-ruby-bot/commands/base.rb +++ b/lib/slack-ruby-bot/commands/base.rb @@ -47,8 +47,9 @@ def self.operator(*values, &block) def self.command(*values, &block) values.each do |value| - match Regexp.new("^(?[\\w[:punct:]@<>]*)[\\s]+(?#{value})$", Regexp::IGNORECASE), &block - match Regexp.new("^(?[\\w[:punct:]@<>]*)[\\s]+(?#{value})[\\s]+(?.*)$", Regexp::IGNORECASE), &block + escaped = Regexp.escape(value) + match Regexp.new("^(?[\\w[:punct:]@<>]*)[\\s]+(?#{escaped})$", Regexp::IGNORECASE), &block + match Regexp.new("^(?[\\w[:punct:]@<>]*)[\\s]+(?#{escaped})[\\s]+(?.*)$", Regexp::IGNORECASE), &block end end diff --git a/spec/slack-ruby-bot/commands/commands_regexp_escape_spec.rb b/spec/slack-ruby-bot/commands/commands_regexp_escape_spec.rb new file mode 100644 index 0000000..10f216f --- /dev/null +++ b/spec/slack-ruby-bot/commands/commands_regexp_escape_spec.rb @@ -0,0 +1,17 @@ +require 'spec_helper' + +describe SlackRubyBot::Commands do + let! :command do + Class.new(SlackRubyBot::Commands::Base) do + command '(' do |client, data, match| + send_message client, data.channel, "#{match[:command]}: #{match[:expression]}" + end + end + end + def app + SlackRubyBot::App.new + end + it 'does not return error' do + expect(message: "#{SlackRubyBot.config.user} ( /").to respond_with_slack_message('(: /') + end +end