Skip to content
Browse files

Initial commit

Notifications can be sent via JSON and rehydrated
on the other side.
  • Loading branch information...
1 parent b7a5789 commit 96cb6c0ca40fa12a4ee53562844fb4311fe75e7a tomhuda committed Jul 10, 2012
Showing with 71 additions and 2 deletions.
  1. +2 −0 .rspec
  2. +19 −2 lib/remote_notifications.rb
  3. +3 −0 remote_notifications.gemspec
  4. +45 −0 spec/remote_notifier_spec.rb
  5. +2 −0 spec/spec_helper.rb
View
2 .rspec
@@ -0,0 +1,2 @@
+-cfs
+-rspec_helper
View
21 lib/remote_notifications.rb
@@ -1,5 +1,22 @@
+require "active_support"
+require "active_support/json"
+require "active_support/notifications"
require "remote_notifications/version"
-module RemoteNotifications
- # Your code goes here...
+module ActiveSupport
+ module Notifications
+ class Event
+ def self.from_json(json)
+ object = ActiveSupport::JSON.decode(json)
+
+ name, start, finish, id, payload =
+ object.values_at("name", "time", "end", "transaction_id", "payload")
+
+ start = DateTime.parse(start)
+ finish = DateTime.parse(finish)
+
+ new(name, start, finish, id, payload)
+ end
+ end
+ end
end
View
3 remote_notifications.gemspec
@@ -14,4 +14,7 @@ Gem::Specification.new do |gem|
gem.name = "remote_notifications"
gem.require_paths = ["lib"]
gem.version = RemoteNotifications::VERSION
+
+ gem.add_dependency "activesupport", ">= 3.0.15", "< 4.0"
+ gem.add_development_dependency "rspec"
end
View
45 spec/remote_notifier_spec.rb
@@ -0,0 +1,45 @@
+describe "notifications can be serialized" do
+ def serialize(*args)
+ event = ActiveSupport::Notifications::Event.new(*args)
+ event.to_json
+ end
+
+ before do
+ @events = []
+ @notifier = ActiveSupport::Notifications::Fanout.new
+ ActiveSupport::Notifications.notifier = @notifier
+ @subscription = @notifier.subscribe do |*args|
+ @events << serialize(*args)
+ end
+
+ ActiveSupport::Notifications.instrument :ohai, { :payload => true }
+
+ @events.size.should == 1
+ @json = @events[0]
+ @object = ActiveSupport::JSON.decode(@events[0])
+ end
+
+ it "receives the notification" do
+ object = ActiveSupport::JSON.decode(@events[0])
+
+ object["name"].should == "ohai"
+ object["payload"].should == { "payload" => true }
+
+ object.should have_key("duration")
+ object.should have_key("end")
+ object.should have_key("time")
+ object.should have_key("transaction_id")
+ end
+
+ it "can build an event object from the JSON" do
+ event = ActiveSupport::Notifications::Event.from_json(@events[0])
+
+ event.name.should == "ohai"
+ event.payload.should == { "payload" => true }
+
+ event.duration.should be_kind_of(Numeric)
+ event.end.should be_kind_of(DateTime)
+ event.time.should be_kind_of(DateTime)
+ event.transaction_id.should be_kind_of(String)
+ end
+end
View
2 spec/spec_helper.rb
@@ -0,0 +1,2 @@
+require "bundler/setup"
+require "remote_notifications"

0 comments on commit 96cb6c0

Please sign in to comment.
Something went wrong with that request. Please try again.