Permalink
Browse files

JSON Serializer

  • Loading branch information...
cavalle committed Nov 10, 2011
1 parent 991673d commit 2d119f5d9fb44fb502bc3fcc6c833a3bdf70b448
View
@@ -19,6 +19,9 @@ gem 'redis', :require => false
gem 'em-redis', :require => false
gem 'mongo', :require => false
+# Middleware
+gem 'json', :require => false
+
gem 'SystemTimer', :require => false, :platforms => :mri_18
# Specify your gem's dependencies in eventwire.gemspec
View
@@ -17,6 +17,4 @@ Gem::Specification.new do |s|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ['lib']
-
- s.add_dependency 'json'
end
View
@@ -59,7 +59,8 @@ def subscribe(event_name, handler_id, &handler)
def middleware
@middleware ||= [ [Eventwire::Middleware::ErrorHandler, {:error_handler => Eventwire.error_handler, :logger => Eventwire.logger}],
[Eventwire::Middleware::Logger, {:logger => Eventwire.logger}],
- Eventwire::Middleware::DataObjects ]
+ Eventwire::Middleware::JSONSerializer,
+ Eventwire::Middleware::DataObjects ]
end
def decorate(driver)
@@ -4,7 +4,7 @@
class Eventwire::Drivers::AMQP
def publish(event_name, event_data = nil)
Bunny.run do |mq|
- mq.exchange(event_name.to_s, :type => :fanout).publish(event_data.to_json)
+ mq.exchange(event_name.to_s, :type => :fanout).publish(event_data)
end
end
@@ -32,10 +32,6 @@ def purge
end
end
end
-
- def parse_json(json)
- json != 'null' && JSON.parse(json)
- end
def subscriptions
@subscriptions ||= []
@@ -47,7 +43,7 @@ def bind_subscription(event_name, handler_id, handler)
queue = ch.queue(handler_id.to_s)
queue.bind(fanout).subscribe do |json_data|
- handler.call parse_json(json_data)
+ handler.call json_data
end
end
end
@@ -4,7 +4,7 @@ class Eventwire::Drivers::Bunny
def publish(event_name, event_data = nil)
Bunny.run do |mq|
- mq.exchange(event_name.to_s, :type => :fanout).publish(event_data.to_json)
+ mq.exchange(event_name.to_s, :type => :fanout).publish(event_data)
end
end
@@ -26,7 +26,7 @@ def start
event_name = msg[:delivery_details][:exchange]
event_data = msg[:payload]
(subscriptions[event_name.to_s] || []).each do |handler|
- handler.call parse_json(event_data)
+ handler.call event_data
end
end
end
@@ -45,10 +45,6 @@ def purge
end
end
- def parse_json(json)
- json != 'null' && JSON.parse(json)
- end
-
def subscriptions
@subscriptions ||= {}
end
@@ -11,7 +11,7 @@ def publish(event_name, event_data = nil)
collection = db.collection('event_handlers')
collection.find(:event_name => event_name).each do |handler|
queue = db.collection(handler['handler'])
- queue.save({:event_data => event_data.to_json})
+ queue.save({:event_data => event_data})
end
end
@@ -32,7 +32,7 @@ def start
break unless @started
queue = db.collection(queue_name)
if event_data = queue.find_and_modify({:remove => true})
- handler.call parse_json(event_data['event_data'])
+ handler.call event_data['event_data']
end
end
end
@@ -49,9 +49,5 @@ def db
def purge
Mongo::Connection.new.drop_database(DB_NAME)
end
-
- def parse_json(json)
- json != 'null' && JSON.parse(json)
- end
end
@@ -10,7 +10,7 @@ def publish(event_name, event_data = nil)
redis = ::Redis.new
handlers = redis.smembers("event_handlers:#{event_name}")
handlers.each do |handler|
- redis.rpush handler, event_data.to_json
+ redis.rpush handler, event_data
end
end
@@ -23,7 +23,7 @@ def start
EM.run do
@handlers.each do |queue, handler|
subscribe_to_queue queue do |json_event|
- handler.call parse_json(json_event)
+ handler.call json_event
end
end
end
@@ -41,10 +41,6 @@ def stop
EM.stop if EM.reactor_running?
end
- def parse_json(json)
- json != 'null' && JSON.parse(json)
- end
-
def purge
redis = ::Redis.new
redis.flushdb
@@ -2,8 +2,9 @@ module Eventwire
module Middleware
autoload :Base, 'eventwire/middleware/base'
- autoload :Logger, 'eventwire/middleware/logger'
- autoload :ErrorHandler, 'eventwire/middleware/error_handler'
- autoload :DataObjects, 'eventwire/middleware/data_objects'
+ autoload :Logger, 'eventwire/middleware/logger'
+ autoload :ErrorHandler, 'eventwire/middleware/error_handler'
+ autoload :DataObjects, 'eventwire/middleware/data_objects'
+ autoload :JSONSerializer, 'eventwire/middleware/json_serializer'
end
end
@@ -0,0 +1,23 @@
+require 'json'
+
+module Eventwire
+ module Middleware
+ class JSONSerializer < Base
+ def subscribe(event_name, handler_id, &handler)
+ @app.subscribe event_name, handler_id do |data|
+ handler.call parse_json(data)
+ end
+ end
+
+ def publish(event_name, event_data)
+ @app.publish event_name, event_data.to_json
+ end
+
+ private
+
+ def parse_json(json)
+ json != 'null' ? JSON.parse(json) : nil
+ end
+ end
+ end
+end
@@ -60,10 +60,10 @@ def start_worker
subject.subscribe(:this_event, :this_subscriber) { |data| event_data = data }
start_worker
- subject.publish :this_event, 'key1' => 'value1', 'key2' => 2
+ subject.publish :this_event, 'event_data'
eventually {
- event_data.should == { 'key1' => 'value1', 'key2' => 2 }
+ event_data.should == 'event_data'
}
end
@@ -0,0 +1,42 @@
+# encoding: UTF-8
+require 'spec_helper'
+
+describe Eventwire::Middleware::JSONSerializer do
+ let(:app) { mock }
+ subject { Eventwire::Middleware::JSONSerializer.new(app) }
+
+ describe 'subscribe' do
+ it 'should call app’s subscribe' do
+ app.should_receive(:subscribe).with(:event_name, :handler_id)
+
+ subject.subscribe(:event_name, :handler_id)
+ end
+
+ it 'should make the handler deserialize event data' do
+ app.stub :subscribe do |_, _, handler|
+ handler.call('{"task_name": "Cleaning"}')
+ end
+
+ subject.subscribe :event_name, :handler_id do |data|
+ data['task_name'].should == 'Cleaning'
+ end
+ end
+
+ it 'should make the handler deserialize null event data' do
+ app.stub :subscribe do |_, _, handler|
+ handler.call('null')
+ end
+
+ subject.subscribe :event_name, :handler_id do |data|
+ data.should be_nil
+ end
+ end
+ end
+
+ describe 'publish' do
+ it 'should call app’s publish serializing data' do
+ app.should_receive(:publish).with(:event_name, '{"task_name":"Cleaning"}')
+ subject.publish(:event_name, {:task_name => 'Cleaning'})
+ end
+ end
+end

0 comments on commit 2d119f5

Please sign in to comment.