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 CHANGED
@@ -1,4 +1,5 @@
1
1
  source "http://rubygems.org"
2
2
 
3
3
  gem 'rake'
4
+ gem 'mocha'
4
5
  gem 'waffle', :git => 'git://github.com/peanut/waffle.git'
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.3'
11
+ gem 'waffle', '~> 0.2.1'
12
12
 
13
13
  and create config file:
14
14
 
15
15
  # production.waffle.yml
16
- strategy: rabbitmq
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
- strategy: rabbitmq
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(strategy = nil)
5
- @strategy = strategy
4
+ def initialize(transport = nil)
5
+ @transport = transport
6
6
  end
7
7
 
8
8
  def publish(flow = 'events', message = '')
9
9
  begin
10
- @strategy.publish flow, message
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
- @strategy.subscribe flow, &block
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
@@ -0,0 +1,19 @@
1
+ module Waffle
2
+ module Encoders
3
+ class Marshal
4
+
5
+ class << self
6
+
7
+ def encode(message = nil)
8
+ ::Marshal.dump message
9
+ end
10
+
11
+ def decode(message = '')
12
+ ::Marshal.restore message
13
+ end
14
+
15
+ end
16
+
17
+ end
18
+ end
19
+ end
data/lib/waffle/event.rb CHANGED
@@ -11,12 +11,12 @@ module Waffle
11
11
  end
12
12
  end
13
13
 
14
- def config
15
- @config ||= Waffle::Configuration.new
14
+ def transport
15
+ @transport ||= Waffle::Base.new eval("Waffle::Transports::#{Waffle::Config.transport.capitalize}").new
16
16
  end
17
17
 
18
- def transport
19
- @transport ||= Waffle::Base.new eval("Waffle::Strategies::#{config.strategy.capitalize}").new(config)
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, Waffle::Utils.encode(event_data)
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 Strategies
4
+ module Transports
5
5
  class Rabbitmq
6
6
 
7
- def initialize(configuration = nil)
8
- raise ArgumentError unless configuration && configuration.is_a?(Waffle::Configuration)
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 Waffle::Utils.decode(message[:payload])
27
+ yield encoder.decode(message[:payload])
26
28
  end
27
29
  end
28
30
 
@@ -1,5 +1,5 @@
1
1
  module Waffle
2
2
 
3
- VERSION = '0.1.3'
3
+ VERSION = '0.2.1'
4
4
 
5
5
  end
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/configuration'
3
+ require 'waffle/config'
5
4
  require 'waffle/event'
6
5
 
7
6
  module Waffle
8
- module Strategies
9
-
10
- autoload :Rabbitmq, 'waffle/strategies/rabbitmq'
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
@@ -0,0 +1,11 @@
1
+ require 'test/unit'
2
+ require 'waffle/config'
3
+
4
+ class RabbitmqTest < Test::Unit::TestCase
5
+
6
+ def test_config_options_mapping
7
+ assert_equal Waffle::Config.transport, 'rabbitmq'
8
+ assert_equal Waffle::Config.encoder, 'json'
9
+ end
10
+
11
+ end
@@ -1,23 +1,20 @@
1
1
  require 'test/unit'
2
- require 'waffle/configuration'
3
- require 'waffle/strategies/rabbitmq'
2
+ require 'mocha'
3
+ require 'bunny'
4
+ require 'waffle/transports/rabbitmq'
4
5
 
5
6
  class RabbitmqTest < Test::Unit::TestCase
6
7
 
7
- def test_bad_initialization
8
- assert_raise ArgumentError do
9
- Waffle::Strategies::Rabbitmq.new
10
- end
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 test_good_initialization
18
- assert_nothing_raised do
19
- Waffle::Strategies::Rabbitmq.new Waffle::Configuration.new
20
- end
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
@@ -10,8 +10,6 @@ Gem::Specification.new do |s|
10
10
  s.authors = ['Alexander Lomakin']
11
11
  s.email = 'alexander.lomakin@gmail.com'
12
12
 
13
- s.homepage = ''
14
-
15
13
  s.summary = 'Abstract flow publisher and subscriber'
16
14
  s.description = 'Client part of Patty statistics server'
17
15
 
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.3
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-21 00:00:00.000000000Z
12
+ date: 2012-03-22 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bunny
16
- requirement: &72738890 !ruby/object:Gem::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: *72738890
24
+ version_requirements: *74030980
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: yajl-ruby
27
- requirement: &72738230 !ruby/object:Gem::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: *72738230
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/configuration.rb
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/strategies/rabbitmq.rb
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: []
@@ -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
@@ -1,19 +0,0 @@
1
- require 'yajl'
2
-
3
- module Waffle
4
- class Utils
5
-
6
- class << self
7
-
8
- def encode(message = nil)
9
- Yajl::Encoder.encode message
10
- end
11
-
12
- def decode(message = '')
13
- Yajl::Parser.parse message
14
- end
15
-
16
- end
17
-
18
- end
19
- end
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