Skip to content

Commit

Permalink
Fix RegexpError when parsing command
Browse files Browse the repository at this point in the history
  • Loading branch information
kuboshizuma committed Nov 25, 2015
1 parent bb1d494 commit c0166da
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/slack-ruby-bot/commands/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ def self.operator(*values, &block)

def self.command(*values, &block)
values.each do |value|
match Regexp.new("^(?<bot>[\\w[:punct:]@<>]*)[\\s]+(?<command>#{value})$", Regexp::IGNORECASE), &block
match Regexp.new("^(?<bot>[\\w[:punct:]@<>]*)[\\s]+(?<command>#{value})[\\s]+(?<expression>.*)$", Regexp::IGNORECASE), &block
escaped = Regexp.escape(value)
match Regexp.new("^(?<bot>[\\w[:punct:]@<>]*)[\\s]+(?<command>#{escaped})$", Regexp::IGNORECASE), &block
match Regexp.new("^(?<bot>[\\w[:punct:]@<>]*)[\\s]+(?<command>#{escaped})[\\s]+(?<expression>.*)$", Regexp::IGNORECASE), &block
end
end

Expand Down
17 changes: 17 additions & 0 deletions spec/slack-ruby-bot/commands/commands_regexp_escape_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit c0166da

Please sign in to comment.