Skip to content

Commit

Permalink
Added connection string support to Connection
Browse files Browse the repository at this point in the history
  • Loading branch information
Brad Gessler committed Feb 7, 2012
1 parent ecd85bf commit 3690c6e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
15 changes: 12 additions & 3 deletions lib/em-jack/connection.rb
@@ -1,5 +1,6 @@
require 'eventmachine'
require 'yaml'
require 'uri'

module EMJack
class Connection
Expand All @@ -21,9 +22,17 @@ def self.handlers
end

def initialize(opts = {})
@host = opts[:host] || 'localhost'
@port = opts[:port] || 11300
@tube = opts[:tube]
case opts
when Hash
@host = opts[:host] || 'localhost'
@port = opts[:port] || 11300
@tube = opts[:tube]
when String
uri = URI.parse(opts)
@host = uri.host || 'localhost'
@port = uri.port || 11300
@tube = uri.path.gsub(/^\//, '') # Kill the leading /
end

reset_tube_state

Expand Down
20 changes: 20 additions & 0 deletions spec/em-jack/connection_spec.rb
Expand Up @@ -16,6 +16,26 @@
EM.stub!(:connect).and_return(connection_mock)
end

describe "uri connection string" do
let(:conn) do
EMJack::Connection.new('beanstalk://leet:1337/leetube')
end

it "should parse host" do
conn.host.should eql('leet')
end

it "should parse tube" do
connection_mock.should_receive(:send).once.with(:use, "leetube")
connection_mock.should_receive(:send).once.with(:watch, "leetube")
conn.connected
end

it "should parse port" do
conn.port.should eql(1337)
end
end

describe 'defaults' do
it 'host of "localhost"' do
conn.host.should == 'localhost'
Expand Down

0 comments on commit 3690c6e

Please sign in to comment.