Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial Pubnub build #28

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions app/assets/javascripts/sync.coffee.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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

Expand Down
8 changes: 8 additions & 0 deletions lib/generators/sync/templates/sync.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
9 changes: 9 additions & 0 deletions lib/sync.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = {})
Expand Down
65 changes: 65 additions & 0 deletions lib/sync/clients/pubnub.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions sync.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/sync_pubnub.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Pubnub
test:
adapter_javascript_url: "http://cdn.pubnub.com/pubnub-3.4.min.js"
pubnub_subscribe_key: ""
pubnub_publish_key: ""
adapter: "PubnubAdapter"
async: false
60 changes: 60 additions & 0 deletions test/sync/message_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,63 @@
end
end

describe "Pubnub::Message" do
include TestHelperPubnub

before do
@message = Sync.client.build_message("/my-channel", html: "<p>Some Data</p>")
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
10 changes: 10 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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