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 /lib | |
| parent | 4e6cf745e5d8426f1470c26f705bacbad1514e91 (diff) | |
| download | hcl-non-singleton-net.tar.bz2 | |
completely eliminate the global http handle.non-singleton-net
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/hcl/app.rb | 10 | ||||
| -rw-r--r-- | lib/hcl/net.rb | 55 | 
2 files changed, 30 insertions, 35 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 | 
