superbolt 0.3.6 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/superbolt/message_handler.rb +21 -0
- data/lib/superbolt/router.rb +38 -0
- data/lib/superbolt/tasks.rb +3 -0
- data/lib/superbolt/version.rb +1 -1
- data/lib/superbolt.rb +5 -0
- data/lib/tasks/superbolt.rake +7 -0
- data/spec/router_spec.rb +52 -0
- metadata +8 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7dc90be952028b8e32550e8e97b205b033da9fe6
|
4
|
+
data.tar.gz: ec9a217c4593a381c77fe59538d7736395ef3904
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 46a40bbdcde081318e933098ad181b9560f7e8926f09142f3a84a28880dae140be0459ddee880286d0d360cdd944c70090f3cb6d50c671a067c51babcb3bea38
|
7
|
+
data.tar.gz: fa346cb0ca0ae6b1bfdf92bd664938ebbd8f35b2df8401859d579d574f160c8e861723be5b5716def3ae365b7896b85cf0e64b980602f69f0104e20dfaae67fa
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# Subclass me to get an easy MessageHandler to plug into your router, no boilerplate.
|
2
|
+
# Just focus on the perform method that you need to build.
|
3
|
+
module Superbolt
|
4
|
+
class MessageHandler
|
5
|
+
attr_reader :arguments, :logger
|
6
|
+
|
7
|
+
def initialize(arguments, logger)
|
8
|
+
@arguments = arguments
|
9
|
+
@logger = logger
|
10
|
+
parse_arguments
|
11
|
+
end
|
12
|
+
|
13
|
+
def parse_arguments
|
14
|
+
# override if you want to extract important stuff at initialize
|
15
|
+
end
|
16
|
+
|
17
|
+
def perform
|
18
|
+
raise NotImplementedError
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module Superbolt
|
2
|
+
class Router
|
3
|
+
attr_reader :message, :logger
|
4
|
+
|
5
|
+
def initialize(message, logger)
|
6
|
+
@message = message
|
7
|
+
@logger = logger
|
8
|
+
end
|
9
|
+
|
10
|
+
def event
|
11
|
+
message['event']
|
12
|
+
end
|
13
|
+
|
14
|
+
def arguments
|
15
|
+
message['arguments'].symbolize_keys
|
16
|
+
end
|
17
|
+
|
18
|
+
def handler_class
|
19
|
+
self.class.routes[event] && self.class.routes[event].constantize
|
20
|
+
end
|
21
|
+
|
22
|
+
def perform
|
23
|
+
if handler_class
|
24
|
+
handler_class.new(arguments, logger).perform
|
25
|
+
else
|
26
|
+
logger.warn "No Superbolt route for event: '#{event}'"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.routes
|
31
|
+
@routes ||= {} # set this up
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.routes=(r)
|
35
|
+
@routes = r
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
data/lib/superbolt/version.rb
CHANGED
data/lib/superbolt.rb
CHANGED
@@ -4,6 +4,8 @@ require 'bunny'
|
|
4
4
|
require 'amqp'
|
5
5
|
require 'eventmachine'
|
6
6
|
require 'active_support/core_ext/module/delegation'
|
7
|
+
require 'active_support/core_ext/hash'
|
8
|
+
require 'active_support/inflector'
|
7
9
|
|
8
10
|
require "superbolt/version"
|
9
11
|
require "superbolt/config"
|
@@ -31,4 +33,7 @@ require "superbolt/facade"
|
|
31
33
|
require "superbolt/messenger"
|
32
34
|
require "superbolt/spec_helpers"
|
33
35
|
|
36
|
+
require "superbolt/router"
|
37
|
+
require "superbolt/message_handler"
|
38
|
+
|
34
39
|
$stdout.sync = true
|
@@ -0,0 +1,7 @@
|
|
1
|
+
# assumes an environment task that sets up your app environment
|
2
|
+
desc "worker that reads the queue and sends messages through the router as configured"
|
3
|
+
task :superbolt => :environment do
|
4
|
+
Superbolt::App.new(Superbolt.app_name).run do |message, logger|
|
5
|
+
Superbolt::Router.new(message, logger).perform
|
6
|
+
end
|
7
|
+
end
|
data/spec/router_spec.rb
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Superbolt::Router do
|
4
|
+
let(:router) { Superbolt::Router.new(message, logger) }
|
5
|
+
|
6
|
+
class MessageHandler
|
7
|
+
end
|
8
|
+
|
9
|
+
before do
|
10
|
+
Superbolt::Router.routes = {
|
11
|
+
'handler' => 'MessageHandler'
|
12
|
+
}
|
13
|
+
end
|
14
|
+
|
15
|
+
let(:handler) { double('handler') }
|
16
|
+
let(:logger) { Logger.new('/dev/null') }
|
17
|
+
|
18
|
+
context 'when event maps to a known route' do
|
19
|
+
let(:message) {
|
20
|
+
{
|
21
|
+
'event' => 'handler',
|
22
|
+
'arguments' => {
|
23
|
+
'yes' => 'we can'
|
24
|
+
}
|
25
|
+
}
|
26
|
+
}
|
27
|
+
|
28
|
+
it "performs the event handler" do
|
29
|
+
MessageHandler.should_receive(:new).with({yes: 'we can'}, logger).and_return(handler)
|
30
|
+
handler.should_receive(:perform)
|
31
|
+
|
32
|
+
router.perform
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
context 'when event is unknown' do
|
37
|
+
let(:message) {
|
38
|
+
{
|
39
|
+
'event' => 'no_one_home',
|
40
|
+
'arguments' => {
|
41
|
+
'ah' => 'maybe not then'
|
42
|
+
}
|
43
|
+
}
|
44
|
+
}
|
45
|
+
|
46
|
+
it "logs a warning" do
|
47
|
+
logger.should_receive(:warn).with("No Superbolt route for event: 'no_one_home'")
|
48
|
+
|
49
|
+
router.perform
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: superbolt
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- socialchorus
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-01-
|
11
|
+
date: 2014-01-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -132,9 +132,11 @@ files:
|
|
132
132
|
- lib/superbolt/connection/queue.rb
|
133
133
|
- lib/superbolt/facade.rb
|
134
134
|
- lib/superbolt/incoming_message.rb
|
135
|
+
- lib/superbolt/message_handler.rb
|
135
136
|
- lib/superbolt/messenger.rb
|
136
137
|
- lib/superbolt/processor.rb
|
137
138
|
- lib/superbolt/queue.rb
|
139
|
+
- lib/superbolt/router.rb
|
138
140
|
- lib/superbolt/runner/ack.rb
|
139
141
|
- lib/superbolt/runner/ack_one.rb
|
140
142
|
- lib/superbolt/runner/base.rb
|
@@ -142,12 +144,15 @@ files:
|
|
142
144
|
- lib/superbolt/runner/greedy.rb
|
143
145
|
- lib/superbolt/runner/pop.rb
|
144
146
|
- lib/superbolt/spec_helpers.rb
|
147
|
+
- lib/superbolt/tasks.rb
|
145
148
|
- lib/superbolt/version.rb
|
149
|
+
- lib/tasks/superbolt.rake
|
146
150
|
- spec/app_spec.rb
|
147
151
|
- spec/config_spec.rb
|
148
152
|
- spec/connection_spec.rb
|
149
153
|
- spec/messenger_spec.rb
|
150
154
|
- spec/queue_spec.rb
|
155
|
+
- spec/router_spec.rb
|
151
156
|
- spec/spec_helper.rb
|
152
157
|
- spec/superbolt_spec.rb
|
153
158
|
- spec/support/queue_helpers.rb
|
@@ -182,6 +187,7 @@ test_files:
|
|
182
187
|
- spec/connection_spec.rb
|
183
188
|
- spec/messenger_spec.rb
|
184
189
|
- spec/queue_spec.rb
|
190
|
+
- spec/router_spec.rb
|
185
191
|
- spec/spec_helper.rb
|
186
192
|
- spec/superbolt_spec.rb
|
187
193
|
- spec/support/queue_helpers.rb
|