message_router 0.0.0 → 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use --create --install 1.8.7@message_router
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in message_router.gemspec
4
+ gemspec
5
+
6
+ group :test, :development do
7
+ gem 'rspec'
8
+ gem 'rb-fsevent'
9
+ gem 'growl'
10
+ gem 'guard'
11
+ gem 'guard-rspec'
12
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,43 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ message_router (0.0.1)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ configuration (1.2.0)
10
+ diff-lcs (1.1.2)
11
+ growl (1.0.3)
12
+ guard (0.3.0)
13
+ open_gem (~> 1.4.2)
14
+ thor (~> 0.14.6)
15
+ guard-rspec (0.1.9)
16
+ guard (>= 0.2.2)
17
+ launchy (0.3.7)
18
+ configuration (>= 0.0.5)
19
+ rake (>= 0.8.1)
20
+ open_gem (1.4.2)
21
+ launchy (~> 0.3.5)
22
+ rake (0.8.7)
23
+ rb-fsevent (0.3.9)
24
+ rspec (2.5.0)
25
+ rspec-core (~> 2.5.0)
26
+ rspec-expectations (~> 2.5.0)
27
+ rspec-mocks (~> 2.5.0)
28
+ rspec-core (2.5.1)
29
+ rspec-expectations (2.5.0)
30
+ diff-lcs (~> 1.1.2)
31
+ rspec-mocks (2.5.0)
32
+ thor (0.14.6)
33
+
34
+ PLATFORMS
35
+ ruby
36
+
37
+ DEPENDENCIES
38
+ growl
39
+ guard
40
+ guard-rspec
41
+ message_router!
42
+ rb-fsevent
43
+ rspec
data/Guardfile ADDED
@@ -0,0 +1,8 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard 'rspec', :version => 2, :notification => true do
5
+ watch(%r{^spec/.+_spec\.rb})
6
+ watch(%r{^lib/(.+)\.rb}) { |m| "spec/message_router_spec.rb" }
7
+ watch(/^spec\/spec_helper.rb/) { "spec" }
8
+ end
data/README.rdoc CHANGED
@@ -2,6 +2,10 @@
2
2
 
3
3
  Message router is a Sinatra-like DSL for processing simple messages, like SMS messages or Tweets.
4
4
 
5
+ == Installation
6
+
7
+ sudo gem install message_router
8
+
5
9
  == Example Code
6
10
 
7
11
  This is a contrived example for a Twitter message router, but it gives you an idea of how it works.
data/Rakefile CHANGED
@@ -1,45 +1,27 @@
1
- require 'rubygems'
2
- require 'rake'
1
+ require 'bundler/gem_tasks'
2
+ # require 'spec/rake/spectask'
3
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
4
+ # Spec::Rake::SpecTask.new(:spec) do |spec|
5
+ # spec.libs << 'lib' << 'spec'
6
+ # spec.spec_files = FileList['spec/**/*_spec.rb']
7
+ # end
20
8
 
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
9
+ # Spec::Rake::SpecTask.new(:rcov) do |spec|
10
+ # spec.libs << 'lib' << 'spec'
11
+ # spec.pattern = 'spec/**/*_spec.rb'
12
+ # spec.rcov = true
13
+ # end
26
14
 
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
15
+ # task :spec => :check_dependencies
32
16
 
33
- task :spec => :check_dependencies
17
+ # task :default => :spec
34
18
 
35
- task :default => :spec
19
+ # require 'rake/rdoctask'
20
+ # Rake::RDocTask.new do |rdoc|
21
+ # version = File.exist?('VERSION') ? File.read('VERSION') : ""
36
22
 
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
23
+ # rdoc.rdoc_dir = 'rdoc'
24
+ # rdoc.title = "message_router #{version}"
25
+ # rdoc.rdoc_files.include('README*')
26
+ # rdoc.rdoc_files.include('lib/**/*.rb')
27
+ # end
@@ -9,7 +9,7 @@ class MessageRouter
9
9
  if args = context_proc.call(router)
10
10
  klass = Class.new(router.class)
11
11
  klass.instance_exec(*args, &block)
12
- klass.new(router.message).dispatch
12
+ klass.dispatch(router.message)
13
13
  end
14
14
  end
15
15
 
@@ -0,0 +1,18 @@
1
+ class MessageRouter
2
+ # Mount routers inside of routers.
3
+ class Mount
4
+ def initialize(mounted_router_klass)
5
+ @mounted_router_klass = mounted_router_klass
6
+ end
7
+
8
+ def call(router)
9
+ mounted_router = mounted_router_klass.new(router.message)
10
+ response = mounted_router.dispatch
11
+ # If the mounted router was halted, halt this router and pass through the response
12
+ mounted_router.halted? ? router.halt(response) : response
13
+ end
14
+
15
+ private
16
+ attr_reader :mounted_router_klass
17
+ end
18
+ end
@@ -0,0 +1,3 @@
1
+ class MessageRouter
2
+ VERSION = "0.0.1"
3
+ end
@@ -1,39 +1,62 @@
1
- $LOAD_PATH.unshift File.join(File.dirname(__FILE__))
1
+ require "message_router/version"
2
2
 
3
3
  class MessageRouter
4
-
5
- autoload :Context, 'message_router/context'
6
- autoload :Matcher, 'message_router/matcher'
7
-
4
+
5
+ autoload :Context, 'message_router/context'
6
+ autoload :Matcher, 'message_router/matcher'
7
+ autoload :Mount, 'message_router/mount'
8
+
8
9
  class << self
9
10
  def match(*args, &block)
10
11
  route Matcher.new(*args, &block)
11
12
  end
12
-
13
+
13
14
  def context(proc, &block)
14
15
  route Context.new(proc, &block)
15
16
  end
16
-
17
- def route(proc)
18
- routes.push proc
17
+
18
+ def mount(mounted_router_klass)
19
+ route Mount.new(mounted_router_klass)
19
20
  end
20
-
21
+
21
22
  def routes
22
23
  @routes ||= []
23
24
  end
25
+
26
+ def route(proc)
27
+ routes.push proc
28
+ end
29
+
30
+ def dispatch(message)
31
+ new(message).dispatch
32
+ end
24
33
  end
25
-
26
- attr_accessor :message
27
-
34
+
35
+ attr_accessor :message, :halted_value
36
+
28
37
  def initialize(message)
29
38
  @message = message
30
39
  end
31
-
40
+
41
+ def halt(val=nil)
42
+ @halted = true
43
+ @halted_value = val
44
+ end
45
+
46
+ def halted?
47
+ !!@halted
48
+ end
49
+
32
50
  # Iterate through all of the matchers, find the first one, and call the block on it.
33
51
  def dispatch
34
52
  self.class.routes.each do |route|
35
53
  # Break out of the loop if a match is found
36
- match = route.call(self) and return match
54
+ if match = route.call(self)
55
+ return match
56
+ elsif halted?
57
+ return halted_value
58
+ end
37
59
  end
60
+ return nil # If nothing is matched, we get here and we should return a nil
38
61
  end
39
62
  end
@@ -1,59 +1,20 @@
1
- # Generated by jeweler
2
- # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
1
  # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "message_router/version"
5
4
 
6
5
  Gem::Specification.new do |s|
7
- s.name = %q{message_router}
8
- s.version = "0.0.0"
6
+ s.name = "message_router"
7
+ s.version = MessageRouter::VERSION
8
+ s.authors = ["Brad Gessler"]
9
+ s.email = ["brad@bradgessler.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{Route messages}
12
+ s.description = %q{a DSL for routing SMS, Twitter, and other short message formats.}
9
13
 
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
14
+ s.rubyforge_project = "message_router"
59
15
 
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+ end
@@ -35,62 +35,108 @@ describe MessageRouter::Matcher do
35
35
  end
36
36
 
37
37
  describe MessageRouter do
38
-
38
+ class CrazyTimesRouter < MessageRouter
39
+ match /^crazy$/ do
40
+ "factory blow out sales are awesome"
41
+ end
42
+ end
43
+
44
+ # Test case router
39
45
  class TwitterRouter < MessageRouter
40
- context :all_caps_with_numbers do
46
+ context :all_caps_with_numbers do |funny_word, high_def_resolution|
47
+ match /FUNNYWORD/i do
48
+ "#{funny_word}-#{high_def_resolution}"
49
+ end
50
+
41
51
  # All caps without numbers... but in a proc
42
52
  context Proc.new{|r| r.message[:body] =~ /^[A-Z\s]+$/ } do
43
53
  match /.+/ do
44
54
  "STOP SHOUTING WITHOUT NUMBERS!"
45
55
  end
46
56
  end
47
-
57
+
48
58
  match /.+/ do
49
59
  "STOP SHOUTING WITH NUMBERS!"
50
60
  end
51
61
  end
52
-
62
+
63
+ mount CrazyTimesRouter
64
+
53
65
  match /hi dude/ do
54
66
  "pleased to meet you"
55
67
  end
68
+
69
+ match /hi halt (\w+)/ do |word|
70
+ halt word
71
+ end
72
+
73
+ match /hi halt/ do
74
+ halt
75
+ end
56
76
 
57
77
  match /hi (\w+)/ do |name|
58
78
  "how do you do #{name}"
59
79
  end
60
80
 
61
- match /hola (\w+)/, :from => 'bradgessler' do |name|
62
- "hello #{name} in spanish"
81
+ match /hola (\w+) (\w+)/, :from => 'bradgessler' do |first_name, last_name|
82
+ "hello #{first_name} #{last_name} in spanish"
63
83
  end
64
84
 
65
85
  private
66
86
  def all_caps_with_numbers
67
- message[:body] =~ /^[A-Z0-9\s]+$/
87
+ if message[:body] =~ /^[A-Z0-9\s]+$/
88
+ ["Zeldzamar", 1080]
89
+ end
90
+ end
91
+ end
92
+
93
+ it "should return nil if there are no matches" do
94
+ TwitterRouter.dispatch(:body => "bums").should be_nil
95
+ end
96
+
97
+ context "mounted router" do
98
+ it "should process message" do
99
+ TwitterRouter.dispatch(:body => "crazy").should eql("factory blow out sales are awesome")
100
+ end
101
+ end
102
+
103
+ context "should halt" do
104
+ it "without value" do
105
+ TwitterRouter.dispatch(:body => "hi halt").should be_nil
106
+ end
107
+
108
+ it "with value" do
109
+ TwitterRouter.dispatch(:body => "hi halt narf").should eql("narf")
68
110
  end
69
111
  end
70
112
 
71
113
  context "default matcher" do
72
114
  it "should capture regexps" do
73
- TwitterRouter.new({:body => 'hi dude'}).dispatch.should eql('pleased to meet you')
115
+ TwitterRouter.dispatch(:body => 'hi dude').should eql('pleased to meet you')
74
116
  end
75
117
 
76
118
  it "should pass regexp captures through blocks" do
77
- TwitterRouter.new({:body => 'hi brad'}).dispatch.should eql("how do you do brad")
119
+ TwitterRouter.dispatch(:body => 'hi brad').should eql("how do you do brad")
78
120
  end
79
121
  end
80
122
 
81
123
  context "hash matcher" do
82
124
  it "should capture with default matcher" do
83
- TwitterRouter.new({:from => 'bradgessler', :body => 'hola jeannette'}).dispatch.should eql("hello jeannette in spanish")
125
+ TwitterRouter.dispatch(:from => 'bradgessler', :body => 'hola jeannette gessler').should eql("hello jeannette gessler in spanish")
84
126
  end
85
127
  end
86
128
 
87
129
  context "context" do
88
130
  it "should handle contexts and non-proc conditions" do
89
- TwitterRouter.new({:body => 'HI BRAD 90'}).dispatch.should eql("STOP SHOUTING WITH NUMBERS!")
131
+ TwitterRouter.dispatch(:body => 'HI BRAD 90').should eql("STOP SHOUTING WITH NUMBERS!")
90
132
  end
91
133
 
92
134
  it "should handle nested contexts and proc conditions" do
93
- TwitterRouter.new({:body => 'HI BRAD'}).dispatch.should eql("STOP SHOUTING WITHOUT NUMBERS!")
135
+ TwitterRouter.dispatch(:body => 'HI BRAD').should eql("STOP SHOUTING WITHOUT NUMBERS!")
136
+ end
137
+
138
+ it "should pass arguments into contexts" do
139
+ TwitterRouter.dispatch(:body => 'FUNNYWORD').should eql("Zeldzamar-1080")
94
140
  end
95
141
  end
96
142
  end
data/spec/spec_helper.rb CHANGED
@@ -1,10 +1,9 @@
1
- $LOAD_PATH.unshift(File.dirname(__FILE__))
2
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
- require 'message_router'
4
1
  require 'rubygems'
5
- require 'spec'
6
- require 'spec/autorun'
2
+ require 'bundler'
3
+ Bundler.setup(:test)
4
+
5
+ require 'message_router'
6
+ require 'rspec'
7
7
 
8
- Spec::Runner.configure do |config|
9
-
8
+ RSpec.configure do |config|
10
9
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: message_router
3
3
  version: !ruby/object:Gem::Version
4
- hash: 31
5
- prerelease: false
4
+ hash: 29
5
+ prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 0
10
- version: 0.0.0
9
+ - 1
10
+ version: 0.0.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Brad Gessler
@@ -15,56 +15,46 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-09-06 00:00:00 -07:00
18
+ date: 2011-07-29 00:00:00 -07:00
19
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
20
+ dependencies: []
21
+
22
+ description: a DSL for routing SMS, Twitter, and other short message formats.
23
+ email:
24
+ - brad@bradgessler.com
39
25
  executables: []
40
26
 
41
27
  extensions: []
42
28
 
43
- extra_rdoc_files:
44
- - LICENSE
45
- - README.rdoc
29
+ extra_rdoc_files: []
30
+
46
31
  files:
47
32
  - .document
48
33
  - .gitignore
34
+ - .rvmrc
35
+ - Gemfile
36
+ - Gemfile.lock
37
+ - Guardfile
49
38
  - LICENSE
50
39
  - README.rdoc
51
40
  - Rakefile
52
- - VERSION
53
41
  - lib/message_router.rb
54
42
  - lib/message_router/context.rb
55
43
  - lib/message_router/matcher.rb
44
+ - lib/message_router/mount.rb
45
+ - lib/message_router/version.rb
56
46
  - message_router.gemspec
57
- - spec/message_dispatcher_spec.rb
47
+ - spec/message_router_spec.rb
58
48
  - spec/routers.rb
59
49
  - spec/spec.opts
60
50
  - spec/spec_helper.rb
61
51
  has_rdoc: true
62
- homepage: http://github.com/bradgessler/message_router
52
+ homepage: ""
63
53
  licenses: []
64
54
 
65
55
  post_install_message:
66
- rdoc_options:
67
- - --charset=UTF-8
56
+ rdoc_options: []
57
+
68
58
  require_paths:
69
59
  - lib
70
60
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -87,12 +77,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
87
77
  version: "0"
88
78
  requirements: []
89
79
 
90
- rubyforge_project:
91
- rubygems_version: 1.3.7
80
+ rubyforge_project: message_router
81
+ rubygems_version: 1.6.2
92
82
  signing_key:
93
83
  specification_version: 3
94
- summary: Route small messages, like SMS and Tweets
84
+ summary: Route messages
95
85
  test_files:
96
- - spec/message_dispatcher_spec.rb
86
+ - spec/message_router_spec.rb
97
87
  - spec/routers.rb
88
+ - spec/spec.opts
98
89
  - spec/spec_helper.rb
data/VERSION DELETED
@@ -1 +0,0 @@
1
- 0.0.0