message_router 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Brad Gessler
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,72 @@
1
+ = Message Router
2
+
3
+ Message router is a Sinatra-like DSL for processing simple messages, like SMS messages or Tweets.
4
+
5
+ == Example Code
6
+
7
+ This is a contrived example for a Twitter message router, but it gives you an idea of how it works.
8
+
9
+ class TwitterRouter < MessageRouter
10
+ context :all_caps_with_numbers do
11
+ # All caps without numbers... but in a proc
12
+ context Proc.new{|r| r.message[:body] =~ /^[A-Z\s]+$/ } do
13
+ match /.+/ do
14
+ "STOP SHOUTING WITHOUT NUMBERS!"
15
+ end
16
+ end
17
+
18
+ match /.+/ do
19
+ "STOP SHOUTING WITH NUMBERS!"
20
+ end
21
+ end
22
+
23
+ match /hi dude/ do
24
+ "pleased to meet you"
25
+ end
26
+
27
+ match /hi (\w+)/ do |name|
28
+ "how do you do #{name}"
29
+ end
30
+
31
+ match /hola (\w+)/, :from => 'bradgessler' do |name|
32
+ "hello #{name} in spanish"
33
+ end
34
+
35
+ private
36
+ def all_caps_with_numbers
37
+ message[:body] =~ /^[A-Z0-9\s]+$/
38
+ end
39
+ end
40
+
41
+ And now some irb action.
42
+
43
+ >> TwitterRouter.new({:body => 'hi dude'}).dispatch
44
+ => "pleased to meet you"
45
+ >> TwitterRouter.new({:body => 'hi brad'}).dispatch
46
+ => "how do you do brad"
47
+ >> TwitterRouter.new({:body => 'HI BRAD 90'}).dispatch
48
+ => "STOP SHOUTING WITH NUMBERS!"
49
+ >> TwitterRouter.new({:body => 'HI BRAD'}).dispatch
50
+ => "STOP SHOUTING WITHOUT NUMBERS!"
51
+
52
+ == License
53
+
54
+ Copyright (c) 2010, Brad Gessler
55
+
56
+ Permission is hereby granted, free of charge, to any person obtaining a copy
57
+ of this software and associated documentation files (the "Software"), to deal
58
+ in the Software without restriction, including without limitation the rights
59
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
60
+ copies of the Software, and to permit persons to whom the Software is
61
+ furnished to do so, subject to the following conditions:
62
+
63
+ The above copyright notice and this permission notice shall be included in
64
+ all copies or substantial portions of the Software.
65
+
66
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
67
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
68
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
69
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
70
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
71
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
72
+ THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,45 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "message_router"
8
+ gem.summary = %Q{Route small messages, like SMS and Tweets}
9
+ gem.description = %Q{Message Router is a Sinatra like DSL that deals with routing small, non-web messages, like Tweets or SMS messages.}
10
+ gem.email = "brad@bradgessler.com"
11
+ gem.homepage = "http://github.com/bradgessler/message_router"
12
+ gem.authors = ["Brad Gessler"]
13
+ gem.add_development_dependency "rspec", ">= 1.2.9"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
+ end
20
+
21
+ require 'spec/rake/spectask'
22
+ Spec::Rake::SpecTask.new(:spec) do |spec|
23
+ spec.libs << 'lib' << 'spec'
24
+ spec.spec_files = FileList['spec/**/*_spec.rb']
25
+ end
26
+
27
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
28
+ spec.libs << 'lib' << 'spec'
29
+ spec.pattern = 'spec/**/*_spec.rb'
30
+ spec.rcov = true
31
+ end
32
+
33
+ task :spec => :check_dependencies
34
+
35
+ task :default => :spec
36
+
37
+ require 'rake/rdoctask'
38
+ Rake::RDocTask.new do |rdoc|
39
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
40
+
41
+ rdoc.rdoc_dir = 'rdoc'
42
+ rdoc.title = "message_router #{version}"
43
+ rdoc.rdoc_files.include('README*')
44
+ rdoc.rdoc_files.include('lib/**/*.rb')
45
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.0
@@ -0,0 +1,23 @@
1
+ class MessageRouter
2
+ class Context
3
+ def initialize(context_proc, &block)
4
+ @block = block
5
+ @context_proc = normalize_context_proc(context_proc)
6
+ end
7
+
8
+ def call(router)
9
+ if args = context_proc.call(router)
10
+ klass = Class.new(router.class)
11
+ klass.instance_exec(*args, &block)
12
+ klass.new(router.message).dispatch
13
+ end
14
+ end
15
+
16
+ private
17
+ attr_reader :block, :context_proc
18
+
19
+ def normalize_context_proc(proc)
20
+ proc.is_a?(Proc) ? proc : Proc.new{|instance| instance.send(proc) }
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,49 @@
1
+ class MessageRouter
2
+ class Matcher
3
+
4
+ def initialize(*args, &block)
5
+ @params, @block = normalize_params(*args), block
6
+ end
7
+
8
+ # Check if all the keys and expression values match the message
9
+ def match(message)
10
+ params.inject({}) do |memo, (key,expression)|
11
+ break unless message.include?(key) # Get out of here if the key isn't even around
12
+
13
+ case expression
14
+ when Regexp # Grap the regexp captures
15
+ if match = message[key].match(expression)
16
+ memo[key] = match.captures
17
+ end
18
+ else # Capture the values, similar to how a regexp would be captured
19
+ memo[key] = Array(message[key]) if message[key] == expression
20
+ end
21
+
22
+ memo[key] ? memo : break
23
+ end
24
+ end
25
+
26
+ def call(router)
27
+ if match = match(router.message)
28
+ router.instance_exec(*match[self.class.default_param], &block)
29
+ end
30
+ end
31
+
32
+ def self.default_param
33
+ :body
34
+ end
35
+
36
+ private
37
+ attr_reader :params, :block
38
+
39
+ def normalize_params(*args)
40
+ if args.size == 2
41
+ args.last.merge(self.class.default_param => args.first)
42
+ elsif args.size == 1 and args.first.is_a?(Hash)
43
+ args.first
44
+ elsif args.size == 1
45
+ { self.class.default_param => args.first }
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,39 @@
1
+ $LOAD_PATH.unshift File.join(File.dirname(__FILE__))
2
+
3
+ class MessageRouter
4
+
5
+ autoload :Context, 'message_router/context'
6
+ autoload :Matcher, 'message_router/matcher'
7
+
8
+ class << self
9
+ def match(*args, &block)
10
+ route Matcher.new(*args, &block)
11
+ end
12
+
13
+ def context(proc, &block)
14
+ route Context.new(proc, &block)
15
+ end
16
+
17
+ def route(proc)
18
+ routes.push proc
19
+ end
20
+
21
+ def routes
22
+ @routes ||= []
23
+ end
24
+ end
25
+
26
+ attr_accessor :message
27
+
28
+ def initialize(message)
29
+ @message = message
30
+ end
31
+
32
+ # Iterate through all of the matchers, find the first one, and call the block on it.
33
+ def dispatch
34
+ self.class.routes.each do |route|
35
+ # Break out of the loop if a match is found
36
+ match = route.call(self) and return match
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,59 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{message_router}
8
+ s.version = "0.0.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Brad Gessler"]
12
+ s.date = %q{2010-09-06}
13
+ s.description = %q{Message Router is a Sinatra like DSL that deals with routing small, non-web messages, like Tweets or SMS messages.}
14
+ s.email = %q{brad@bradgessler.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/message_router.rb",
27
+ "lib/message_router/context.rb",
28
+ "lib/message_router/matcher.rb",
29
+ "message_router.gemspec",
30
+ "spec/message_dispatcher_spec.rb",
31
+ "spec/routers.rb",
32
+ "spec/spec.opts",
33
+ "spec/spec_helper.rb"
34
+ ]
35
+ s.homepage = %q{http://github.com/bradgessler/message_router}
36
+ s.rdoc_options = ["--charset=UTF-8"]
37
+ s.require_paths = ["lib"]
38
+ s.rubygems_version = %q{1.3.7}
39
+ s.summary = %q{Route small messages, like SMS and Tweets}
40
+ s.test_files = [
41
+ "spec/message_dispatcher_spec.rb",
42
+ "spec/routers.rb",
43
+ "spec/spec_helper.rb"
44
+ ]
45
+
46
+ if s.respond_to? :specification_version then
47
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
48
+ s.specification_version = 3
49
+
50
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
51
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
52
+ else
53
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
54
+ end
55
+ else
56
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
57
+ end
58
+ end
59
+
@@ -0,0 +1,96 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe MessageRouter::Matcher do
4
+ context "regexp expressions" do
5
+ it "should return captures" do
6
+ match = MessageRouter::Matcher.new({:body => /(\w+) (\w+) (\w+)/}).match({:body => 'hi there dude'})
7
+ match[:body].should eql(%w(hi there dude))
8
+ end
9
+ end
10
+
11
+ context "string expressions" do
12
+ it "should return capture" do
13
+ match = MessageRouter::Matcher.new({:body => 'testing'}).match({:body => 'testing'})
14
+ match[:body].should eql(['testing'])
15
+ end
16
+ end
17
+
18
+ it "should return captures if all matchers have captures" do
19
+ match = MessageRouter::Matcher.new({:body => 'testing', :carrier => 'att'}).match({:body => 'testing', :carrier => 'att'})
20
+ match.should_not be_nil
21
+ end
22
+
23
+ it "should not return captures if some matchers have captures" do
24
+ match = MessageRouter::Matcher.new({:body => 'testing', :carrier => 'att'}).match({:body => 'testing', :carrier => 'verizon'})
25
+ match.should be_nil
26
+ end
27
+
28
+ it "should not return captures of params are missing" do
29
+ match = MessageRouter::Matcher.new({:body => 'testing', :carrier => 'att'}).match({:body => 'testing'})
30
+ end
31
+
32
+ it "should default matcher param to :body" do
33
+ MessageRouter::Matcher.default_param.should eql(:body)
34
+ end
35
+ end
36
+
37
+ describe MessageRouter do
38
+
39
+ class TwitterRouter < MessageRouter
40
+ context :all_caps_with_numbers do
41
+ # All caps without numbers... but in a proc
42
+ context Proc.new{|r| r.message[:body] =~ /^[A-Z\s]+$/ } do
43
+ match /.+/ do
44
+ "STOP SHOUTING WITHOUT NUMBERS!"
45
+ end
46
+ end
47
+
48
+ match /.+/ do
49
+ "STOP SHOUTING WITH NUMBERS!"
50
+ end
51
+ end
52
+
53
+ match /hi dude/ do
54
+ "pleased to meet you"
55
+ end
56
+
57
+ match /hi (\w+)/ do |name|
58
+ "how do you do #{name}"
59
+ end
60
+
61
+ match /hola (\w+)/, :from => 'bradgessler' do |name|
62
+ "hello #{name} in spanish"
63
+ end
64
+
65
+ private
66
+ def all_caps_with_numbers
67
+ message[:body] =~ /^[A-Z0-9\s]+$/
68
+ end
69
+ end
70
+
71
+ context "default matcher" do
72
+ it "should capture regexps" do
73
+ TwitterRouter.new({:body => 'hi dude'}).dispatch.should eql('pleased to meet you')
74
+ end
75
+
76
+ it "should pass regexp captures through blocks" do
77
+ TwitterRouter.new({:body => 'hi brad'}).dispatch.should eql("how do you do brad")
78
+ end
79
+ end
80
+
81
+ context "hash matcher" do
82
+ it "should capture with default matcher" do
83
+ TwitterRouter.new({:from => 'bradgessler', :body => 'hola jeannette'}).dispatch.should eql("hello jeannette in spanish")
84
+ end
85
+ end
86
+
87
+ context "context" do
88
+ it "should handle contexts and non-proc conditions" do
89
+ TwitterRouter.new({:body => 'HI BRAD 90'}).dispatch.should eql("STOP SHOUTING WITH NUMBERS!")
90
+ end
91
+
92
+ it "should handle nested contexts and proc conditions" do
93
+ TwitterRouter.new({:body => 'HI BRAD'}).dispatch.should eql("STOP SHOUTING WITHOUT NUMBERS!")
94
+ end
95
+ end
96
+ end
data/spec/routers.rb ADDED
@@ -0,0 +1,40 @@
1
+ class SMS < MessageRouter
2
+
3
+ context :group_session do |session|
4
+ match /(\w)?\s(.*)/ do |keyword, value|
5
+ "#{stats(keyword, value)} || #{session}"
6
+ end
7
+
8
+ match /leave/ do
9
+ 'leaaave'
10
+ end
11
+ end
12
+
13
+ context :lists do
14
+ end
15
+
16
+ match /para(ms)/ do |m|
17
+ message
18
+ end
19
+
20
+ match /ping/ do
21
+ "PONG #{Time.now.to_s}"
22
+ end
23
+
24
+ match /(\w+)\s?(.*)/ do |keyword, text|
25
+ stats(keyword, text)
26
+ end
27
+
28
+ private
29
+ def group_session
30
+ true
31
+ end
32
+
33
+ def stats(keyword, text)
34
+ "Global:#{keyword}"
35
+ end
36
+
37
+ def super_session
38
+ true
39
+ end
40
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,10 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'message_router'
4
+ require 'rubygems'
5
+ require 'spec'
6
+ require 'spec/autorun'
7
+
8
+ Spec::Runner.configure do |config|
9
+
10
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: message_router
3
+ version: !ruby/object:Gem::Version
4
+ hash: 31
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 0
10
+ version: 0.0.0
11
+ platform: ruby
12
+ authors:
13
+ - Brad Gessler
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-09-06 00:00:00 -07:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 13
30
+ segments:
31
+ - 1
32
+ - 2
33
+ - 9
34
+ version: 1.2.9
35
+ type: :development
36
+ version_requirements: *id001
37
+ description: Message Router is a Sinatra like DSL that deals with routing small, non-web messages, like Tweets or SMS messages.
38
+ email: brad@bradgessler.com
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files:
44
+ - LICENSE
45
+ - README.rdoc
46
+ files:
47
+ - .document
48
+ - .gitignore
49
+ - LICENSE
50
+ - README.rdoc
51
+ - Rakefile
52
+ - VERSION
53
+ - lib/message_router.rb
54
+ - lib/message_router/context.rb
55
+ - lib/message_router/matcher.rb
56
+ - message_router.gemspec
57
+ - spec/message_dispatcher_spec.rb
58
+ - spec/routers.rb
59
+ - spec/spec.opts
60
+ - spec/spec_helper.rb
61
+ has_rdoc: true
62
+ homepage: http://github.com/bradgessler/message_router
63
+ licenses: []
64
+
65
+ post_install_message:
66
+ rdoc_options:
67
+ - --charset=UTF-8
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ hash: 3
76
+ segments:
77
+ - 0
78
+ version: "0"
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ hash: 3
85
+ segments:
86
+ - 0
87
+ version: "0"
88
+ requirements: []
89
+
90
+ rubyforge_project:
91
+ rubygems_version: 1.3.7
92
+ signing_key:
93
+ specification_version: 3
94
+ summary: Route small messages, like SMS and Tweets
95
+ test_files:
96
+ - spec/message_dispatcher_spec.rb
97
+ - spec/routers.rb
98
+ - spec/spec_helper.rb