From 46afef6a97f9b0ed002637b67b9c56326ec6051a Mon Sep 17 00:00:00 2001 From: Guy Maliar Date: Wed, 24 Apr 2013 12:04:04 +0300 Subject: [PATCH 1/2] Added Pubnub Adapter --- app/assets/javascripts/sync.coffee.erb | 15 ++++++ lib/generators/sync/templates/sync.yml | 8 ++++ lib/sync.rb | 9 ++++ lib/sync/clients/pubnub.rb | 65 ++++++++++++++++++++++++++ sync.gemspec | 1 + test/fixtures/sync_pubnub.yml | 7 +++ test/sync/message_test.rb | 60 ++++++++++++++++++++++++ test/test_helper.rb | 10 ++++ 8 files changed, 175 insertions(+) create mode 100644 lib/sync/clients/pubnub.rb create mode 100644 test/fixtures/sync_pubnub.yml diff --git a/app/assets/javascripts/sync.coffee.erb b/app/assets/javascripts/sync.coffee.erb index 9e8617d..c2a5436 100644 --- a/app/assets/javascripts/sync.coffee.erb +++ b/app/assets/javascripts/sync.coffee.erb @@ -5,6 +5,8 @@ FAYE_HOST: "<%= Sync.server %>" PUSHER_API_KEY: "<%= Sync.api_key %>" CLIENT_ADAPTER: "<%= Sync.adapter %>" + PUBNUB_PUBLISH_KEY: "<%= Sync.pubnub_publish_key %>" + PUBNUB_SUBSCRIBE_KEY: "<%= Sync.pubnub_subscribe_key %>" init: -> @client = new Sync[@CLIENT_ADAPTER] @@ -64,6 +66,19 @@ class Sync.Pusher subscribe: (channel, callback) -> @channel.bind channel, callback +class Sync.Pubnub + + connect: -> + @pubnub = window.PUBNUB.init( + publish_key: Sync.PUBNUB_PUBLISH_KEY, + subscribe_key: Sync.PUBNUB_SUBSCRIBE_KEY + ) + + subscribe: (channel, callback) -> + @pubnub.subscribe( + channel: channel, + message: callback + ) class Sync.View diff --git a/lib/generators/sync/templates/sync.yml b/lib/generators/sync/templates/sync.yml index 0b1b042..6858436 100644 --- a/lib/generators/sync/templates/sync.yml +++ b/lib/generators/sync/templates/sync.yml @@ -14,6 +14,14 @@ development: # adapter: "Pusher" # async: true +# Pubnub +# development: +# adapter_javascript_url: "http://cdn.pubnub.com/pubnub-3.4.min.js" +# pubnub_subscribe_key: "YOUR_SUBSCRIBE_KEY" +# pubnub_publish_key: "YOUR_PUBLISH_KEY" +# adapter: "PubnubAdapter" +# async: false + test: server: "http://localhost:9292/faye" adapter_javascript_url: "http://localhost:9292/faye/faye.js" diff --git a/lib/sync.rb b/lib/sync.rb index 5695098..4accea2 100644 --- a/lib/sync.rb +++ b/lib/sync.rb @@ -11,6 +11,7 @@ require 'sync/resource' require 'sync/clients/faye' require 'sync/clients/pusher' +require 'sync/clients/pubnub' require 'sync/engine' if defined? Rails module Sync @@ -65,6 +66,14 @@ def api_key config[:api_key] end + def pubnub_publish_key + config[:pubnub_publish_key] + end + + def pubnub_subscribe_key + config[:pubnub_subscribe_key] + end + # Returns the Faye Rack application. # Any options given are passed to the Faye::RackAdapter. def pubsub_app(options = {}) diff --git a/lib/sync/clients/pubnub.rb b/lib/sync/clients/pubnub.rb new file mode 100644 index 0000000..df8d4c3 --- /dev/null +++ b/lib/sync/clients/pubnub.rb @@ -0,0 +1,65 @@ +module Sync + module Clients + class PubnubAdapter + + def setup + require 'logger' + require 'pubnub' + @@callback = lambda { |message| Logger.new(STDOUT).debug(message) } + end + + def batch_publish(*args) + Message.batch_publish(*args) + end + + def build_message(*args) + Message.new(*args) + end + + def self.callback + @@callback + end + + class Message + + attr_accessor :channel, :data + + def self.batch_publish(messages) + messages.each do |message| + message.publish + end + end + + def initialize(channel, data) + @channel = channel + @data = data + end + + def publish + if Sync.async? + publish_asynchronous + else + publish_synchronous + end + end + + def publish_synchronous + publish_pubnub + end + + def publish_asynchronous + publish_pubnub + end + + def publish_pubnub + pubnub = Pubnub.new(:publish_key => Sync.pubnub_publish_key, :subscribe_key => Sync.pubnub_subscribe_key) + pubnub.publish( + :channel => @channel, + :message => @data, + :callback => PubnubAdapter.callback + ) + end + end + end + end +end \ No newline at end of file diff --git a/sync.gemspec b/sync.gemspec index 84e86f1..b7a8f9e 100644 --- a/sync.gemspec +++ b/sync.gemspec @@ -14,6 +14,7 @@ Gem::Specification.new do |s| s.add_development_dependency 'faye' s.add_development_dependency 'thin' s.add_development_dependency 'pusher', '~> 0.11.3' + s.add_development_dependency 'pubnub' s.add_development_dependency 'rake' s.add_development_dependency 'rails', '~> 3.2.13' s.add_development_dependency 'mocha', '~> 0.13.3' diff --git a/test/fixtures/sync_pubnub.yml b/test/fixtures/sync_pubnub.yml new file mode 100644 index 0000000..7ec34b0 --- /dev/null +++ b/test/fixtures/sync_pubnub.yml @@ -0,0 +1,7 @@ +# Pubnub +test: + adapter_javascript_url: "http://cdn.pubnub.com/pubnub-3.4.min.js" + pubnub_subscribe_key: "sub-c-7839e1d8-acb2-11e2-9e4e-12313f022c90" + pubnub_publish_key: "pub-c-607d5887-9fc9-49d8-9e65-c1e0f8de77dc" + adapter: "PubnubAdapter" + async: false diff --git a/test/sync/message_test.rb b/test/sync/message_test.rb index f8ac7fd..8c3cde8 100644 --- a/test/sync/message_test.rb +++ b/test/sync/message_test.rb @@ -143,3 +143,63 @@ end end +describe "Pubnub::Message" do + include TestHelperPubnub + + before do + @message = Sync.client.build_message("/my-channel", html: "

Some Data

") + end + + + describe '#to_json' do + it "Converts message to json for Faye publish" do + assert @message.to_json + end + end + + describe "asynchronous publishing" do + include EM::MiniTest::Spec + + before do + Sync.stubs(:async?).returns true + end + + describe "batched message publishing" do + before do + @messages = 10.times.collect{|i| Sync.client.build_message("/ch#{i}", {html: ""})} + end + + it 'should publish array of messages with single post to faye' do + assert Sync.client.batch_publish(@messages) + end + end + + describe '#publish' do + it 'Publishes a message to Pubnub' do + assert @message.publish + end + end + end + + describe "synchronous publishing" do + before do + Sync.stubs(:async?).returns false + end + + describe '#publish' do + it 'Publishes a message to Pubnub' do + assert @message.publish + end + end + + describe "batched message publishing" do + before do + @messages = 10.times.collect{|i| Sync.client.build_message("/ch#{i}", {html: ""})} + end + + it 'should publish array of messages with single post to faye' do + assert Sync.client.batch_publish(@messages) + end + end + end +end \ No newline at end of file diff --git a/test/test_helper.rb b/test/test_helper.rb index 3ee027a..4f3c481 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -38,4 +38,14 @@ def setup end end +module TestHelperPubnub + + def setup + Sync.load_config( + File.expand_path("../fixtures/sync_pubnub.yml", __FILE__), + "test" + ) + end +end + From 9a2a8bed337db129d92bbb3a2fa6d75c9717283f Mon Sep 17 00:00:00 2001 From: Guy Maliar Date: Wed, 24 Apr 2013 12:06:54 +0300 Subject: [PATCH 2/2] Update sync_pubnub.yml --- test/fixtures/sync_pubnub.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/fixtures/sync_pubnub.yml b/test/fixtures/sync_pubnub.yml index 7ec34b0..7c33ecf 100644 --- a/test/fixtures/sync_pubnub.yml +++ b/test/fixtures/sync_pubnub.yml @@ -1,7 +1,7 @@ # Pubnub test: adapter_javascript_url: "http://cdn.pubnub.com/pubnub-3.4.min.js" - pubnub_subscribe_key: "sub-c-7839e1d8-acb2-11e2-9e4e-12313f022c90" - pubnub_publish_key: "pub-c-607d5887-9fc9-49d8-9e65-c1e0f8de77dc" + pubnub_subscribe_key: "" + pubnub_publish_key: "" adapter: "PubnubAdapter" async: false