message_router 0.0.2 → 0.1.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 +1 -1
- data/Gemfile.lock +1 -1
- data/LICENSE +1 -1
- data/README.rdoc +151 -39
- data/lib/message_router.rb +2 -70
- data/lib/message_router/router.rb +320 -0
- data/lib/message_router/version.rb +2 -2
- data/message_router.gemspec +3 -3
- data/spec/message_router_spec.rb +434 -125
- metadata +11 -17
- data/lib/message_router/context.rb +0 -23
- data/lib/message_router/matcher.rb +0 -49
- data/lib/message_router/mount.rb +0 -18
- data/spec/routers.rb +0 -40
metadata
CHANGED
@@ -1,27 +1,28 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: message_router
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 25
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 1
|
9
|
+
- 1
|
10
|
+
version: 0.1.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Brad Gessler
|
14
|
+
- Paul Cortens
|
14
15
|
autorequire:
|
15
16
|
bindir: bin
|
16
17
|
cert_chain: []
|
17
18
|
|
18
|
-
date:
|
19
|
-
default_executable:
|
19
|
+
date: 2012-07-13 00:00:00 Z
|
20
20
|
dependencies: []
|
21
21
|
|
22
22
|
description: a DSL for routing SMS, Twitter, and other short message formats.
|
23
23
|
email:
|
24
24
|
- brad@bradgessler.com
|
25
|
+
- paul@thoughtless.ca
|
25
26
|
executables: []
|
26
27
|
|
27
28
|
extensions: []
|
@@ -38,16 +39,12 @@ files:
|
|
38
39
|
- README.rdoc
|
39
40
|
- Rakefile
|
40
41
|
- lib/message_router.rb
|
41
|
-
- lib/message_router/
|
42
|
-
- lib/message_router/matcher.rb
|
43
|
-
- lib/message_router/mount.rb
|
42
|
+
- lib/message_router/router.rb
|
44
43
|
- lib/message_router/version.rb
|
45
44
|
- message_router.gemspec
|
46
45
|
- spec/message_router_spec.rb
|
47
|
-
- spec/routers.rb
|
48
46
|
- spec/spec.opts
|
49
47
|
- spec/spec_helper.rb
|
50
|
-
has_rdoc: true
|
51
48
|
homepage: ""
|
52
49
|
licenses: []
|
53
50
|
|
@@ -77,12 +74,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
77
74
|
requirements: []
|
78
75
|
|
79
76
|
rubyforge_project: message_router
|
80
|
-
rubygems_version: 1.
|
77
|
+
rubygems_version: 1.8.22
|
81
78
|
signing_key:
|
82
79
|
specification_version: 3
|
83
80
|
summary: Route messages
|
84
|
-
test_files:
|
85
|
-
|
86
|
-
- spec/routers.rb
|
87
|
-
- spec/spec.opts
|
88
|
-
- spec/spec_helper.rb
|
81
|
+
test_files: []
|
82
|
+
|
@@ -1,23 +0,0 @@
|
|
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.dispatch(router.message)
|
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
|
@@ -1,49 +0,0 @@
|
|
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_key], &block)
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
def self.default_key
|
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_key => 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_key => args.first }
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
data/lib/message_router/mount.rb
DELETED
@@ -1,18 +0,0 @@
|
|
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
|
data/spec/routers.rb
DELETED
@@ -1,40 +0,0 @@
|
|
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
|