Commit 9e42123
Changed files (3)
lib
del
examples
lib/del/examples/routes.rb
@@ -2,6 +2,12 @@ Del.configure do |config|
puts "Registering custom routes."
config.router.register(/.*/) do |message|
+ Del.logger.info("Backwards!")
message.reply(message.text.reverse)
end
+
+ config.router.register(/^cowsay (.*)/) do |message, match_data|
+ Del.logger.info("COWSAY!")
+ message.reply("/code #{`cowsay #{match_data[1]}`}")
+ end
end
lib/del/default_router.rb
@@ -10,8 +10,8 @@ module Del
def route(message)
@routes.each do |route|
- if route[:pattern].match(message.text)
- route[:command].call(message)
+ if matches = route[:pattern].match(message.text)
+ route[:command].call(message, matches)
end
end
end
spec/default_router_spec.rb
@@ -5,18 +5,28 @@ RSpec.describe Del::DefaultRouter do
let(:recorder) { [] }
before :each do
subject.register(/^Hello World!$/) do |message|
- recorder.push(message.text)
+ recorder.push(text: message.text)
+ end
+
+ subject.register(/^cowsay (.*)$/) do |message, match_data|
+ recorder.push(text: message.text, match_data: match_data)
end
end
it 'routes to the registered route' do
subject.route(double(text: 'Hello World!'))
- expect(recorder).to include('Hello World!')
+ expect(recorder).to match_array([text: 'Hello World!'])
end
it 'does not route a route that does not match' do
subject.route(double(text: "What's good?"))
expect(recorder).to be_empty
end
+
+ it 'passes captures to the block' do
+ subject.route(double(text: "cowsay HELLO"))
+ matches = /^cowsay (.*)$/.match("cowsay HELLO")
+ expect(recorder).to match_array([text: 'cowsay HELLO', match_data: matches])
+ end
end
end