Skip to content

Commit

Permalink
Merge branch 'release/0.0.21'
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdanRada committed Nov 6, 2015
2 parents 55d8d50 + 13554b8 commit 6eaa816
Show file tree
Hide file tree
Showing 9 changed files with 294 additions and 297 deletions.
16 changes: 8 additions & 8 deletions celluloid_pubsub.gemspec
Expand Up @@ -22,21 +22,21 @@ Gem::Specification.new do |s|
s.add_runtime_dependency 'celluloid-websocket-client', '0.0.1'
s.add_runtime_dependency 'activesupport', '~> 4.1', '>= 4.1.0'

s.add_development_dependency 'rspec-rails', '~> 2.0', '>= 2.0'
s.add_development_dependency 'guard', '~> 2.6', '>= 2.6'
s.add_development_dependency 'guard-rspec', '~> 4.2', '>= 4.2'
s.add_development_dependency 'simplecov', '~> 0.9', '>= 0.9'
s.add_development_dependency 'rspec-rails', '~> 3.3', '>= 3.3'
s.add_development_dependency 'guard', '~> 2.13', '>= 2.13'
s.add_development_dependency 'guard-rspec', '~> 4.6', '>= 4.6'
s.add_development_dependency 'simplecov', '~> 0.10', '>= 0.10'
s.add_development_dependency 'simplecov-summary', '~> 0.0.4', '>= 0.0.4'
s.add_development_dependency 'mocha', '~> 1.1', '>= 1.1'
s.add_development_dependency 'coveralls', '~> 0.7', '>= 0.7'
s.add_development_dependency 'rvm-tester', '~> 1.1', '>= 1.1'

s.add_development_dependency 'rubocop', '0.29'
s.add_development_dependency 'phare', '~> 0.6', '>= 0.6'
s.add_development_dependency 'rubocop', '~> 0.33', '>= 0.33'
s.add_development_dependency 'phare', '~> 0.7', '>= 0.7'
s.add_development_dependency 'yard', '~> 0.8', '>= 0.8.7'
s.add_development_dependency 'yard-rspec', '~> 0.1', '>= 0.1'
s.add_development_dependency 'redcarpet', '~> 3.2', '>= 3.2.2'
s.add_development_dependency 'redcarpet', '~> 3.3', '>= 3.3'
s.add_development_dependency 'github-markup', '~> 1.3', '>= 1.3.3'
s.add_development_dependency 'inch', '~> 0.5', '>= 0.5.10'
s.add_development_dependency 'inch', '~> 0.6', '>= 0.6'
s.add_development_dependency 'guard-inch', '~> 0.1', '>= 0.1.0'
end
8 changes: 2 additions & 6 deletions examples/simple_test.rb
Expand Up @@ -19,9 +19,7 @@ class Subscriber
include Celluloid::Logger

def initialize(options = {})
@client = CelluloidPubsub::Client.connect({ actor: Actor.current }.merge(options)) do |ws|
ws.subscribe('test_channel') # this will execute after the connection is opened
end
@client = CelluloidPubsub::Client.connect({ actor: Actor.current, channel: 'test_channel' }.merge(options))
end

def on_message(message)
Expand All @@ -46,9 +44,7 @@ class Publisher
include Celluloid::Logger

def initialize(options = {})
@client = CelluloidPubsub::Client.connect({ actor: Actor.current }.merge(options)) do |ws|
ws.subscribe('test_channel2') # this will execute after the connection is opened
end
@client = CelluloidPubsub::Client.connect({ actor: Actor.current, channel: 'test_channel2' }.merge(options))
@client.publish('test_channel', 'data' => 'my_message') # the message needs to be a Hash
end

Expand Down
14 changes: 8 additions & 6 deletions lib/celluloid_pubsub/client_pubsub.rb
Expand Up @@ -28,7 +28,7 @@ class Client
class PubSubWorker
include Celluloid
include Celluloid::Logger
attr_accessor :actor, :connect_blk, :client, :options, :hostname, :port, :path
attr_accessor :actor, :client, :options, :hostname, :port, :path, :channel

# receives a list of options that are used to connect to the webserver and an actor to which the callbacks are delegated to
# when receiving messages from a channel
Expand All @@ -44,10 +44,10 @@ class PubSubWorker
# @return [void]
#
# @api public
def initialize(options, &connect_blk)
def initialize(options)
parse_options(options)
raise "#{self}: Please provide an actor in the options list!!!" if @actor.blank?
@connect_blk = connect_blk
raise "#{self}: Please provide an channel in the options list!!!" if @channel.blank?
@client = Celluloid::WebSocket::Client.new("ws://#{@hostname}:#{@port}#{@path}", Actor.current)
end

Expand All @@ -66,6 +66,7 @@ def parse_options(options)
raise 'Options is not a hash' unless options.is_a?(Hash)
@options = options.stringify_keys!
@actor = @options.fetch('actor', nil)
@channel = @options.fetch('channel', nil)
@hostname = @options.fetch('hostname', CelluloidPubsub::WebServer::HOST)
@port = @options.fetch('port', CelluloidPubsub::WebServer::PORT)
@path = @options.fetch('path', CelluloidPubsub::WebServer::PATH)
Expand Down Expand Up @@ -147,14 +148,15 @@ def unsubscribe_clients(channel)
def unsubscribe_all
send_action('unsubscribe_all')
end

# callback executes after connection is opened and delegates action to actor
#
# @return [void]
#
# @api public
def on_open
debug("#{self.class} websocket connection opened") if debug_enabled?
@connect_blk.call Actor.current
async.subscribe(@channel)
end

# callback executes when actor receives a message from a subscribed channel
Expand Down Expand Up @@ -245,8 +247,8 @@ def chat(message)
# @return [CelluloidPubsub::Client::PubSubWorker]
#
# @api public
def self.connect(options = {}, &connect_blk)
CelluloidPubsub::Client::PubSubWorker.new(options, &connect_blk)
def self.connect(options = {})
CelluloidPubsub::Client::PubSubWorker.new(options)
end
end
end
2 changes: 1 addition & 1 deletion lib/celluloid_pubsub/version.rb
Expand Up @@ -17,7 +17,7 @@ module VERSION
# minor release version
MINOR = 0
# tiny release version
TINY = 20
TINY = 21
# prelease version ( set this only if it is a prelease)
PRE = nil

Expand Down
33 changes: 17 additions & 16 deletions spec/lib/celluloid_pubsub/client_pubsub_spec.rb
Expand Up @@ -10,7 +10,7 @@
expected = nil
CelluloidPubsub::Client::PubSubWorker.stubs(:new).returns(expected)
res = CelluloidPubsub::Client.connect(options, &blk)
res.should eq expected
expect(res).to eq expected
end
end

Expand All @@ -19,11 +19,12 @@
let(:options) { {} }
let(:socket) { mock }
let(:actor) { mock }
let(:channel) { 'some_channel' }

before(:each) do
Celluloid::WebSocket::Client.stubs(:new).returns(socket)
socket.stubs(:text)
@worker = CelluloidPubsub::Client::PubSubWorker.new({ 'actor' => actor, enable_debug: true }, &blk)
@worker = CelluloidPubsub::Client::PubSubWorker.new({ 'actor' => actor, channel: channel, enable_debug: true }, &blk)
@worker.stubs(:client).returns(socket)
@worker.stubs(:debug)
@worker.stubs(:async).returns(@worker)
Expand All @@ -32,8 +33,8 @@

describe '#initialize' do
it 'creates a object' do
@worker.connect_blk.should_not be_nil
@worker.actor.should eq actor
expect(@worker.channel).to eq channel
expect(@worker.actor).to eq actor
end
end

Expand All @@ -46,25 +47,25 @@

it 'parses options' do
@worker.parse_options(custom_options)
@worker.actor.should eq(actor)
@worker.hostname.should eq(hostname)
@worker.port.should eq(port)
@worker.path.should eq(path)
expect(@worker.actor).to eq(actor)
expect(@worker.hostname).to eq(hostname)
expect(@worker.port).to eq(port)
expect(@worker.path).to eq(path)
end

it 'sets defaults' do
@worker.parse_options({})
@worker.actor.should eq(nil)
@worker.hostname.should eq('0.0.0.0')
@worker.port.should eq(1234)
@worker.path.should eq('/ws')
expect(@worker.actor).to eq(nil)
expect(@worker.hostname).to eq('0.0.0.0')
expect(@worker.port).to eq(1234)
expect(@worker.path).to eq('/ws')
end
end

describe '#debug_enabled?' do
it 'checks if debug is enabled' do
act = @worker.debug_enabled?
act.should eq(true)
expect(act).to eq(true)
end
end

Expand All @@ -89,14 +90,14 @@
message.expects(:present?).returns(true)
message.stubs(:[]).with('client_action').returns('successful_subscription')
actual = @worker.succesfull_subscription?(message)
actual.should eq(true)
expect(actual).to eq(true)
end

it 'checks the message and returns false' do
message.expects(:present?).returns(true)
message.stubs(:[]).with('client_action').returns('something_else')
actual = @worker.succesfull_subscription?(message)
actual.should eq(false)
expect(actual).to eq(false)
end
end

Expand All @@ -113,7 +114,7 @@
let(:channel) { 'some_channel' }
let(:data) { 'some_message' }
it 'chats with the server' do
@worker.connect_blk.expects(:call)
@worker.expects(:subscribe).with(channel)
@worker.on_open
end
end
Expand Down

0 comments on commit 6eaa816

Please sign in to comment.