waffle 0.1.3 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +1 -0
- data/README.md +5 -3
- data/lib/waffle/base.rb +4 -4
- data/lib/waffle/config.rb +31 -0
- data/lib/waffle/encoders/json.rb +21 -0
- data/lib/waffle/encoders/marshal.rb +19 -0
- data/lib/waffle/event.rb +5 -5
- data/lib/waffle/{strategies → transports}/rabbitmq.rb +8 -6
- data/lib/waffle/version.rb +1 -1
- data/lib/waffle.rb +7 -5
- data/test/test_config.rb +11 -0
- data/test/test_rabbitmq.rb +11 -14
- data/waffle.gemspec +0 -2
- metadata +12 -11
- data/lib/waffle/configuration.rb +0 -25
- data/lib/waffle/utils.rb +0 -19
- data/test/test_utils.rb +0 -16
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -8,18 +8,20 @@ An abstract flow publisher and subscriber.
|
|
8
8
|
|
9
9
|
Insert in your Rails Gemfile:
|
10
10
|
|
11
|
-
gem 'waffle', '~> 0.1
|
11
|
+
gem 'waffle', '~> 0.2.1'
|
12
12
|
|
13
13
|
and create config file:
|
14
14
|
|
15
15
|
# production.waffle.yml
|
16
|
-
|
16
|
+
transport: rabbitmq
|
17
|
+
encoder: marshal
|
17
18
|
url: amqp://anyhost.com:5678
|
18
19
|
|
19
20
|
or if RabbitMQ on local machine
|
20
21
|
|
21
22
|
# production.waffle.yml
|
22
|
-
|
23
|
+
transport: rabbitmq
|
24
|
+
encoder: marshal
|
23
25
|
|
24
26
|
## Usage
|
25
27
|
|
data/lib/waffle/base.rb
CHANGED
@@ -1,20 +1,20 @@
|
|
1
1
|
module Waffle
|
2
2
|
class Base
|
3
3
|
|
4
|
-
def initialize(
|
5
|
-
@
|
4
|
+
def initialize(transport = nil)
|
5
|
+
@transport = transport
|
6
6
|
end
|
7
7
|
|
8
8
|
def publish(flow = 'events', message = '')
|
9
9
|
begin
|
10
|
-
@
|
10
|
+
@transport.publish flow, message
|
11
11
|
rescue
|
12
12
|
nil
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
16
|
def subscribe(flow = '', &block)
|
17
|
-
@
|
17
|
+
@transport.subscribe flow, &block
|
18
18
|
end
|
19
19
|
|
20
20
|
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'singleton'
|
2
|
+
require 'yaml'
|
3
|
+
|
4
|
+
module Waffle
|
5
|
+
class Config
|
6
|
+
include Singleton
|
7
|
+
|
8
|
+
attr_reader :config_hash
|
9
|
+
|
10
|
+
def initialize
|
11
|
+
@config_hash = {'transport' => 'rabbitmq', 'url' => nil, 'encoder' => 'json'}
|
12
|
+
|
13
|
+
if defined? Rails
|
14
|
+
@config_hash.merge! YAML.load_file("#{Rails.root}/config/#{Rails.env}.waffle.yml")
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
class << self
|
19
|
+
|
20
|
+
def method_missing(m, *args, &block)
|
21
|
+
if self.instance.config_hash.has_key?(m.to_s)
|
22
|
+
self.instance.config_hash[m.to_s]
|
23
|
+
else
|
24
|
+
super
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'yajl'
|
2
|
+
|
3
|
+
module Waffle
|
4
|
+
module Encoders
|
5
|
+
class Json
|
6
|
+
|
7
|
+
class << self
|
8
|
+
|
9
|
+
def encode(message = nil)
|
10
|
+
Yajl::Encoder.encode message
|
11
|
+
end
|
12
|
+
|
13
|
+
def decode(message = '')
|
14
|
+
Yajl::Parser.parse message
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/lib/waffle/event.rb
CHANGED
@@ -11,12 +11,12 @@ module Waffle
|
|
11
11
|
end
|
12
12
|
end
|
13
13
|
|
14
|
-
def
|
15
|
-
@
|
14
|
+
def transport
|
15
|
+
@transport ||= Waffle::Base.new eval("Waffle::Transports::#{Waffle::Config.transport.capitalize}").new
|
16
16
|
end
|
17
17
|
|
18
|
-
def
|
19
|
-
@
|
18
|
+
def encoder
|
19
|
+
@encoder ||= eval("Waffle::Encoders::#{Waffle::Config.encoder.capitalize}")
|
20
20
|
end
|
21
21
|
|
22
22
|
def occured(event_name = 'event', event_data = nil)
|
@@ -26,7 +26,7 @@ module Waffle
|
|
26
26
|
|
27
27
|
event_data.merge!({'occured_at' => Time.now})
|
28
28
|
|
29
|
-
transport.publish event_name,
|
29
|
+
transport.publish event_name, encoder.encode(event_data)
|
30
30
|
end
|
31
31
|
|
32
32
|
end
|
@@ -1,16 +1,18 @@
|
|
1
1
|
require 'bunny'
|
2
2
|
|
3
3
|
module Waffle
|
4
|
-
module
|
4
|
+
module Transports
|
5
5
|
class Rabbitmq
|
6
6
|
|
7
|
-
def initialize
|
8
|
-
|
9
|
-
|
10
|
-
@bunny = Bunny.new configuration.url
|
7
|
+
def initialize
|
8
|
+
@bunny = Bunny.new Waffle::Config.url
|
11
9
|
@bunny.start
|
12
10
|
end
|
13
11
|
|
12
|
+
def encoder
|
13
|
+
@encoder ||= eval("Waffle::Encoders::#{Waffle::Config.encoder.capitalize}")
|
14
|
+
end
|
15
|
+
|
14
16
|
def publish(flow = 'events', message = '')
|
15
17
|
@exchange = @bunny.exchange flow
|
16
18
|
@exchange.publish message
|
@@ -22,7 +24,7 @@ module Waffle
|
|
22
24
|
@queue.bind @exchange
|
23
25
|
|
24
26
|
@queue.subscribe do |message|
|
25
|
-
yield
|
27
|
+
yield encoder.decode(message[:payload])
|
26
28
|
end
|
27
29
|
end
|
28
30
|
|
data/lib/waffle/version.rb
CHANGED
data/lib/waffle.rb
CHANGED
@@ -1,13 +1,15 @@
|
|
1
1
|
require 'waffle/version'
|
2
|
-
require 'waffle/utils'
|
3
2
|
require 'waffle/base'
|
4
|
-
require 'waffle/
|
3
|
+
require 'waffle/config'
|
5
4
|
require 'waffle/event'
|
6
5
|
|
7
6
|
module Waffle
|
8
|
-
module
|
9
|
-
|
10
|
-
|
7
|
+
module Transports
|
8
|
+
autoload :Rabbitmq, 'waffle/transports/rabbitmq'
|
9
|
+
end
|
11
10
|
|
11
|
+
module Encoders
|
12
|
+
autoload :Json, 'waffle/encoders/json'
|
13
|
+
autoload :Marshal, 'waffle/encoders/marshal'
|
12
14
|
end
|
13
15
|
end
|
data/test/test_config.rb
ADDED
data/test/test_rabbitmq.rb
CHANGED
@@ -1,23 +1,20 @@
|
|
1
1
|
require 'test/unit'
|
2
|
-
require '
|
3
|
-
require '
|
2
|
+
require 'mocha'
|
3
|
+
require 'bunny'
|
4
|
+
require 'waffle/transports/rabbitmq'
|
4
5
|
|
5
6
|
class RabbitmqTest < Test::Unit::TestCase
|
6
7
|
|
7
|
-
def
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
assert_raise ArgumentError do
|
13
|
-
Waffle::Strategies::Rabbitmq.new 'crap'
|
14
|
-
end
|
8
|
+
def test_publish
|
9
|
+
Bunny.setup nil
|
10
|
+
Bunny::Exchange.any_instance.stubs(:publish).returns(nil)
|
11
|
+
assert_equal nil, Waffle::Transports::Rabbitmq.new.publish('events', 'message')
|
15
12
|
end
|
16
13
|
|
17
|
-
def
|
18
|
-
|
19
|
-
|
20
|
-
|
14
|
+
def test_subscribe
|
15
|
+
Bunny.setup nil
|
16
|
+
Bunny::Queue.any_instance.stubs(:subscribe).returns(nil)
|
17
|
+
assert_equal nil, Waffle::Transports::Rabbitmq.new.subscribe{ |m| puts m }
|
21
18
|
end
|
22
19
|
|
23
20
|
end
|
data/waffle.gemspec
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: waffle
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1
|
4
|
+
version: 0.2.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-03-
|
12
|
+
date: 2012-03-22 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bunny
|
16
|
-
requirement: &
|
16
|
+
requirement: &74030980 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *74030980
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: yajl-ruby
|
27
|
-
requirement: &
|
27
|
+
requirement: &74030450 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,7 +32,7 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *74030450
|
36
36
|
description: Client part of Patty statistics server
|
37
37
|
email: alexander.lomakin@gmail.com
|
38
38
|
executables: []
|
@@ -45,15 +45,16 @@ files:
|
|
45
45
|
- Rakefile
|
46
46
|
- lib/waffle.rb
|
47
47
|
- lib/waffle/base.rb
|
48
|
-
- lib/waffle/
|
48
|
+
- lib/waffle/config.rb
|
49
|
+
- lib/waffle/encoders/json.rb
|
50
|
+
- lib/waffle/encoders/marshal.rb
|
49
51
|
- lib/waffle/event.rb
|
50
|
-
- lib/waffle/
|
51
|
-
- lib/waffle/utils.rb
|
52
|
+
- lib/waffle/transports/rabbitmq.rb
|
52
53
|
- lib/waffle/version.rb
|
54
|
+
- test/test_config.rb
|
53
55
|
- test/test_rabbitmq.rb
|
54
|
-
- test/test_utils.rb
|
55
56
|
- waffle.gemspec
|
56
|
-
homepage:
|
57
|
+
homepage: http://github.com/peanut/waffle
|
57
58
|
licenses: []
|
58
59
|
post_install_message:
|
59
60
|
rdoc_options: []
|
data/lib/waffle/configuration.rb
DELETED
@@ -1,25 +0,0 @@
|
|
1
|
-
require 'yaml'
|
2
|
-
|
3
|
-
module Waffle
|
4
|
-
class Configuration
|
5
|
-
|
6
|
-
def initialize(custom_params = {})
|
7
|
-
@configuration = {'strategy' => 'rabbitmq', 'url' => nil}
|
8
|
-
|
9
|
-
if defined? Rails
|
10
|
-
@configuration.merge! YAML.load_file("#{Rails.root}/config/#{Rails.env}.waffle.yml")
|
11
|
-
end
|
12
|
-
|
13
|
-
@configuration.merge! custom_params
|
14
|
-
end
|
15
|
-
|
16
|
-
def method_missing(m, *args, &block)
|
17
|
-
if @configuration.has_key? m.to_s
|
18
|
-
@configuration[m.to_s]
|
19
|
-
else
|
20
|
-
super
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
end
|
25
|
-
end
|
data/lib/waffle/utils.rb
DELETED
data/test/test_utils.rb
DELETED
@@ -1,16 +0,0 @@
|
|
1
|
-
require 'test/unit'
|
2
|
-
require 'waffle/utils'
|
3
|
-
|
4
|
-
class UtilsTest < Test::Unit::TestCase
|
5
|
-
|
6
|
-
def test_encode
|
7
|
-
encoded_msg = Waffle::Utils.encode({'a' => 1, 'b' => 2})
|
8
|
-
assert_equal('{"a":1,"b":2}', encoded_msg)
|
9
|
-
end
|
10
|
-
|
11
|
-
def test_decode
|
12
|
-
decoded_msg = Waffle::Utils.decode('{"a":1,"b":2}')
|
13
|
-
assert_equal({'a' => 1, 'b' => 2}, decoded_msg)
|
14
|
-
end
|
15
|
-
|
16
|
-
end
|