diff options
| author | Zack Hobson | 2013-12-27 13:42:42 -0800 |
|---|---|---|
| committer | Zack Hobson | 2013-12-27 13:44:14 -0800 |
| commit | 7317487adc111ab6f226fc42b57dd6a32cb28541 (patch) | |
| tree | 2053983a5ae51970c9fb89888aa16b3f34b412ee | |
| parent | 4e6cf745e5d8426f1470c26f705bacbad1514e91 (diff) | |
| download | hcl-non-singleton-net.tar.bz2 | |
completely eliminate the global http handle.non-singleton-net
| -rw-r--r-- | lib/hcl/app.rb | 10 | ||||
| -rw-r--r-- | lib/hcl/net.rb | 55 | ||||
| -rw-r--r-- | test/net_test.rb | 16 | ||||
| -rw-r--r-- | test/test_helper.rb | 2 |
4 files changed, 39 insertions, 44 deletions
diff --git a/lib/hcl/app.rb b/lib/hcl/app.rb index 2799f12..bf02e5e 100644 --- a/lib/hcl/app.rb +++ b/lib/hcl/app.rb @@ -13,6 +13,8 @@ module HCl SETTINGS_FILE = "#{HCL_DIR}/settings.yml".freeze CONFIG_FILE = "#{HCL_DIR}/config.yml".freeze + attr_reader :http + def initialize FileUtils.mkdir_p(HCL_DIR) read_config @@ -20,10 +22,6 @@ module HCl self end - def http - HCl::Net - end - # Run the given command and arguments. def self.command *args new.process_args(*args).run @@ -144,7 +142,7 @@ EOM if has_security_command? load_password config end - Net.configure config + @http = HCl::Net.new config else request_config end @@ -157,7 +155,7 @@ EOM config['password'] = ask("Password: ") { |q| q.echo = false }.to_s config['subdomain'] = ask("Subdomain: ").to_s config['ssl'] = /^y/.match(ask("Use SSL? (y/n): ").downcase) - Net.configure config + @http = HCl::Net.new config write_config config end diff --git a/lib/hcl/net.rb b/lib/hcl/net.rb index 334571c..dd73528 100644 --- a/lib/hcl/net.rb +++ b/lib/hcl/net.rb @@ -2,41 +2,38 @@ require 'hcl/harvest_middleware' require 'faraday' module HCl - module Net - class << self - # configuration accessors - CONFIG_VARS = [ :login, :password, :subdomain, :ssl ].freeze. - each { |config_var| attr_reader config_var } + class Net + # configuration accessors + CONFIG_VARS = [ :login, :password, :subdomain, :ssl ].freeze. + each { |config_var| attr_reader config_var } - def config_hash - CONFIG_VARS.inject({}) {|c,k| c.update(k => send(k)) } - end + def config_hash + CONFIG_VARS.inject({}) {|c,k| c.update(k => send(k)) } + end - def configure opts - @login = opts['login'].freeze - @password = opts['password'].freeze - @subdomain = opts['subdomain'].freeze - @ssl = !!opts['ssl'] - @http = Faraday.new( - "http#{ssl ? 's' : '' }://#{subdomain}.harvestapp.com" - ) do |f| - f.use :harvest, login, password - f.adapter Faraday.default_adapter - end - self + def initialize opts + @login = opts['login'].freeze + @password = opts['password'].freeze + @subdomain = opts['subdomain'].freeze + @ssl = !!opts['ssl'] + @http = Faraday.new( + "http#{ssl ? 's' : '' }://#{subdomain}.harvestapp.com" + ) do |f| + f.use :harvest, login, password + f.adapter Faraday.default_adapter end + end - def get action - @http.get(action).body - end + def get action + @http.get(action).body + end - def post action, data - @http.post(action, data).body - end + def post action, data + @http.post(action, data).body + end - def delete action - @http.delete(action).body - end + def delete action + @http.delete(action).body end end end diff --git a/test/net_test.rb b/test/net_test.rb index 882a09f..94e5ad4 100644 --- a/test/net_test.rb +++ b/test/net_test.rb @@ -3,10 +3,10 @@ require 'test_helper' class NetTest < HCl::TestCase def test_configure - assert_equal 'bob', HCl::Net.login - assert_equal 'secret', HCl::Net.password - assert_equal 'bobclock', HCl::Net.subdomain - assert_equal true, HCl::Net.ssl + assert_equal 'bob', http.login + assert_equal 'secret', http.password + assert_equal 'bobclock', http.subdomain + assert_equal true, http.ssl end def test_http_deep_unescape @@ -14,7 +14,7 @@ class NetTest < HCl::TestCase status:'gotten & got!', comparisons:['burrito > taco', 'rain < sun'] }) - body = HCl::Net.get 'foo' + body = http.get 'foo' assert_equal 'gotten & got!', body[:status] assert_equal 'burrito > taco', body[:comparisons][0] assert_equal 'rain < sun', body[:comparisons][1] @@ -22,19 +22,19 @@ class NetTest < HCl::TestCase def test_http_get register_uri(:get, "/foo", {message:'gotten!'}) - body = HCl::Net.get 'foo' + body = http.get 'foo' assert_equal 'gotten!', body[:message] end def test_http_post register_uri(:post, "/foo", {message:'posted!'}) - body = HCl::Net.post 'foo', {pizza:'taco'} + body = http.post 'foo', {pizza:'taco'} assert_equal 'posted!', body[:message] end def test_http_delete register_uri(:delete, "/foo", {message:'wiped!'}) - body = HCl::Net.delete 'foo' + body = http.delete 'foo' assert_equal 'wiped!', body[:message] end end diff --git a/test/test_helper.rb b/test/test_helper.rb index 33cf499..dc052e2 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -31,7 +31,7 @@ class HCl::TestCase < MiniTest::Unit::TestCase attr_reader :http def setup FakeWeb.allow_net_connect = false - @http = HCl::Net.configure \ + @http = HCl::Net.new \ 'login' => 'bob', 'password' => 'secret', 'subdomain' => 'bobclock', |
