Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: polleverywhere/message_router
base: d6e4f68c6c
...
head fork: polleverywhere/message_router
compare: ccb6145cc0
  • 3 commits
  • 2 files changed
  • 0 commit comments
  • 1 contributor
Showing with 125 additions and 8 deletions.
  1. +33 −2 lib/message_router/router.rb
  2. +92 −6 spec/message_router_spec.rb
View
35 lib/message_router/router.rb
@@ -18,7 +18,7 @@ def initialize
# Returns true if a rule matched
def call(message)
@rules.detect do |should_i, do_this|
- if should_i
+ if should_i.call(message)
do_this.call message
return true
end
@@ -29,7 +29,38 @@ def call(message)
private
def match should_i, do_this
- @rules << [should_i, do_this]
+ case should_i
+ when Regexp, String
+ match({:body => should_i}, do_this)
+ when TrueClass, FalseClass
+ match(Proc.new { should_i }, do_this)
+ when Symbol
+ match(Proc.new do |message|
+ if self.method(should_i).arity == 0
+ # Method won't accept arguments
+ self.send should_i
+ else
+ # Method will accept arguments. Try sending the message.
+ self.send should_i, message
+ end
+ end, do_this)
+ when Hash
+ match(Proc.new do |message|
+ should_i.all? do |key, val|
+ case val
+ when String
+ message[key] == val
+ when Regexp
+ message[key] =~ val
+ else
+ raise "Unexpected value '#{val.inspect}'. Should be String or Regexp."
+ end
+ end
+ end, do_this)
+ else
+ # Assume it already responds to #call.
+ @rules << [should_i, do_this]
+ end
end
end
end
View
98 spec/message_router_spec.rb
@@ -2,6 +2,7 @@
# TODO: Maybe move this into a sub-directory
describe MessageRouter::Router do
+
describe ".build" do
it "returns a Router" do
r = MessageRouter::Router.build {}
@@ -9,13 +10,98 @@
end
describe 'defining matchers' do
- it 'accepts a boolean'
- it 'accepts a proc which is passed the message'
- it 'accepts a regex to match against the message body'
- it 'accepts a string to match against the message body'
+ before do
+ # For use in confirming whether or not a proc was called.
+ $thing_to_match = $did_it_run = nil
+ end
+
+ let :message do
+ {
+ :body => 'hello world',
+ :from => '15554443333',
+ :to => '12345'
+ }
+ end
+
+ # This needs to be a method (and not memoized by #let) so that
+ # $thing_to_match can change within a test.
+ def router
+ MessageRouter::Router.build do
+ match($thing_to_match, lambda { $did_it_run = true } )
+
+ # Using these methods also proves that the message is optionally
+ # accessable by helper methods.
+ def always_true(message)
+ message[:body] == 'hello world'
+ end
+ def always_false
+ false
+ end
+ end
+ end
+
+ let :the_test do
+ Proc.new do |opts|
+ $thing_to_match = opts[:true]
+ router.call(message)
+ $did_it_run.should == true
+ $did_it_run = nil # reset for next time
+
+ $thing_to_match = opts[:false]
+ router.call(message)
+ $did_it_run.should == nil
+ $did_it_run = nil # reset for next time
+ end
+ end
+
+ it 'accepts a boolean' do
+ the_test.call :true => true, :false => false
+ end
+
+ it 'accepts a proc which is passed the message' do
+ the_test.call(
+ :true => Proc.new {|msg| msg[:to] == '12345'},
+ :false => Proc.new {|msg| msg[:to] == '54321'}
+ )
+ end
+
+ it 'accepts a regex to match against the message body' do
+ the_test.call :true => /hello/, :false => /bye bye/
+ end
+
+ it 'accepts a string to match against the message body' do
+ the_test.call :true => 'hello world', :false => 'hello'
+ end
+
+ it 'accepts a symbol which is a method name' do
+ the_test.call :true => :always_true, :false => :always_false
+ end
+
describe 'matching a hash' do
- it 'accepts a string to match against the hash key'
- it 'accepts a regex to match against the hash key'
+ it 'accepts a string to match against the hash key' do
+ the_test.call(
+ :true => {
+ :from => '15554443333',
+ :to => '12345'
+ },
+ :false => {
+ :from => 'something-else',
+ :to => '12345'
+ }
+ )
+ end
+ it 'accepts a regex to match against the hash key' do
+ the_test.call(
+ :true => {
+ :from => /\A1555\d{7}\Z/,
+ :to => /\A\d{5}\Z/
+ },
+ :false => {
+ :from => /\A1555\d{7}\Z/,
+ :to => /\A\d{6}\Z/
+ }
+ )
+ end
end
end
end

No commit comments for this range

Something went wrong with that request. Please try again.