Skip to content

Commit

Permalink
Merge pull request #17 from theckman/improve_config_building
Browse files Browse the repository at this point in the history
try to improve TunnelBroker::Client
  • Loading branch information
theckman committed Apr 13, 2014
2 parents 4369638 + 332145b commit a8b3c7a
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 59 deletions.
20 changes: 14 additions & 6 deletions lib/tunnelbroker/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,22 @@ def config
end

def build_messenger_config
url = config.url || ENDPOINT
conf = {}
conf.merge!(url: url)
conf.merge!(username: config.username) unless config.username.nil?
conf.merge!(update_key: config.update_key) unless config.update_key.nil?
conf.merge!(tunnelid: config.tunnelid) unless config.tunnelid.nil?
conf.merge!(ip4addr: config.ip4addr) unless config.ip4addr.nil?
TunnelBroker::Configuration::FIELDS.each do |k|
conf.merge!(config_hash_item(k))
end
conf
end

def config_hash_item(key)
c = config.send(key)
if c.nil? && key == :url
{ key => ENDPOINT }
elsif c.nil?
{}
else
{ key => c }
end
end
end
end
3 changes: 2 additions & 1 deletion lib/tunnelbroker/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ module TunnelBroker
# Configuration class for the TunnelBroker client
#
class Configuration
attr_accessor :ip4addr, :username, :update_key, :tunnelid, :url
FIELDS = [:url, :ip4addr, :username, :update_key, :tunnelid]
attr_accessor(*FIELDS)

def initialize
set_default_values
Expand Down
2 changes: 1 addition & 1 deletion lib/tunnelbroker/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@
# The TunnelBroker API client namespace.
#
module TunnelBroker
VERSION ||= '0.0.10'
VERSION ||= '0.0.11'
end
143 changes: 92 additions & 51 deletions spec/unit/tunnelbroker/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def self.url=(url_in)
end

def self.username
'tim'
'theckman'
end
def self.update_key
'abc123'
Expand Down Expand Up @@ -88,7 +88,6 @@ def call_api
before do
@tbc = TunnelBroker::Client.new
allow(@tbc).to receive(:config).and_return(MockConfig)
@base_hash = { url: TunnelBroker::Client::ENDPOINT }
end

context 'when given more than one arg' do
Expand All @@ -99,113 +98,155 @@ def call_api
end
end

context 'when config.url set' do
context 'irrespective of what the config options are' do
it 'should try to build the config with each config item' do
TunnelBroker::Configuration::FIELDS.each do |c|
expect(@tbc).to receive(:config_hash_item).with(c).and_return({})
end
@tbc.send(:build_messenger_config)
end
end

context 'when none of the config options are set' do
before do
allow(MockConfig).to receive(:url).and_return('http://tb.io/')
allow(MockConfig).to receive(:username).and_return(nil)
allow(MockConfig).to receive(:update_key).and_return(nil)
allow(MockConfig).to receive(:tunnelid).and_return(nil)
allow(MockConfig).to receive(:ip4addr).and_return(nil)
end

subject { @tbc.send(:build_messenger_config) }

it { should be_an_instance_of Hash }

it { should eql(url: 'http://tb.io/') }
it { should eql(url: TunnelBroker::Client::ENDPOINT) }
end

context 'when config.url is not set' do
context 'when all of the config options are set' do
before do
allow(MockConfig).to receive(:username).and_return(nil)
allow(MockConfig).to receive(:update_key).and_return(nil)
allow(MockConfig).to receive(:tunnelid).and_return(nil)
allow(MockConfig).to receive(:ip4addr).and_return(nil)
@cfg = {
url: TunnelBroker::Client::ENDPOINT,
username: MockConfig.username,
update_key: MockConfig.update_key,
tunnelid: MockConfig.tunnelid,
ip4addr: MockConfig.ip4addr
}
end

subject { @tbc.send(:build_messenger_config) }

it { should be_an_instance_of Hash }

it { should eql(url: TunnelBroker::Client::ENDPOINT) }
it { should eql @cfg }
end
end

context 'when config.username is set' do
before do
@cfg = @base_hash.merge(username: MockConfig.username)
allow(MockConfig).to receive(:update_key).and_return(nil)
allow(MockConfig).to receive(:tunnelid).and_return(nil)
allow(MockConfig).to receive(:ip4addr).and_return(nil)
describe '.config_hash_item' do
before do
@tbc = TunnelBroker::Client.new
allow(@tbc).to receive(:config).and_return(MockConfig)
end

context 'when given more than one arg' do
it 'should raise ArgumentError' do
expect do
@tbc.send(:config_hash_item)
end.to raise_error ArgumentError
end
end

subject { @tbc.send(:build_messenger_config) }
context 'when given less than one arg' do
it 'should raise ArgumentError' do
expect do
@tbc.send(:config_hash_item)
end.to raise_error ArgumentError
end
end

it { should be_an_instance_of Hash }
context 'when given a key that is not valid' do
before do
@key = :fakeitem
end

it { should eql @cfg }
it 'should not be a valid key' do
expect(TunnelBroker::Configuration::FIELDS.include?(@key))
.to be_falsey
end

it 'should raise NoMethodError' do
expect do
@tbc.send(:config_hash_item, @key)
end.to raise_error NoMethodError
end
end

context 'when config.update_key is set' do
context 'when given a valid key that is unset' do
before do
@cfg = @base_hash.merge(update_key: MockConfig.update_key)
@key = :username
allow(MockConfig).to receive(:username).and_return(nil)
allow(MockConfig).to receive(:tunnelid).and_return(nil)
allow(MockConfig).to receive(:ip4addr).and_return(nil)
end

subject { @tbc.send(:build_messenger_config) }
it 'should be a valid key' do
expect(TunnelBroker::Configuration::FIELDS.include?(@key))
.to be_truthy
end

subject { @tbc.send(:config_hash_item, @key) }

it { should be_an_instance_of Hash }

it { should eql @cfg }
it { should eql({}) }
end

context 'when config.tunnelid is set' do
context 'when given a valid key that is set' do
before do
@cfg = @base_hash.merge(tunnelid: MockConfig.tunnelid)
allow(MockConfig).to receive(:username).and_return(nil)
allow(MockConfig).to receive(:update_key).and_return(nil)
allow(MockConfig).to receive(:ip4addr).and_return(nil)
@key = :username
end

subject { @tbc.send(:build_messenger_config) }
it 'should be a valid key' do
expect(TunnelBroker::Configuration::FIELDS.include?(@key))
.to be_truthy
end

subject { @tbc.send(:config_hash_item, @key) }

it { should be_an_instance_of Hash }

it { should eql @cfg }
it { should eql(username: 'theckman') }
end

context 'when config.ip4addr is set' do
context 'when given :url and it being unset' do
before do
@cfg = @base_hash.merge(ip4addr: MockConfig.ip4addr)
allow(MockConfig).to receive(:username).and_return(nil)
allow(MockConfig).to receive(:update_key).and_return(nil)
allow(MockConfig).to receive(:tunnelid).and_return(nil)
@key = :url
end

subject { @tbc.send(:build_messenger_config) }
it 'should be a valid key' do
expect(TunnelBroker::Configuration::FIELDS.include?(@key))
.to be_truthy
end

subject { @tbc.send(:config_hash_item, @key) }

it { should be_an_instance_of Hash }

it { should eql @cfg }
it { should eql(url: TunnelBroker::Client::ENDPOINT) }
end

context 'when all config options are set' do
context 'when given :url and it being unset' do
before do
@cfg = {
url: TunnelBroker::Client::ENDPOINT,
username: MockConfig.username,
update_key: MockConfig.update_key,
tunnelid: MockConfig.tunnelid,
ip4addr: MockConfig.ip4addr
}
@key = :url
allow(MockConfig).to receive(:url).and_return('http://tb.io/')
end

subject { @tbc.send(:build_messenger_config) }
it 'should be a valid key' do
expect(TunnelBroker::Configuration::FIELDS.include?(@key))
.to be_truthy
end

subject { @tbc.send(:config_hash_item, @key) }

it { should be_an_instance_of Hash }

it { should eql @cfg }
it { should eql(url: 'http://tb.io/') }
end
end

Expand Down

0 comments on commit a8b3c7a

Please sign in to comment.