diff options
| author | Zack Hobson | 2013-12-22 22:00:45 -0800 | 
|---|---|---|
| committer | Zack Hobson | 2013-12-22 22:00:45 -0800 | 
| commit | 50cbd47da7a54a46809a818a43e821431de258ce (patch) | |
| tree | 9667675dda14c7281fc5d60535c9ec89ddb07807 | |
| parent | feac869bf567af57402eb206f1bf5dd62fa0e93f (diff) | |
| download | hcl-50cbd47da7a54a46809a818a43e821431de258ce.tar.bz2 | |
extract net code into hcl::net
| -rw-r--r-- | lib/hcl.rb | 1 | ||||
| -rw-r--r-- | lib/hcl/app.rb | 4 | ||||
| -rw-r--r-- | lib/hcl/commands.rb | 2 | ||||
| -rw-r--r-- | lib/hcl/day_entry.rb | 6 | ||||
| -rw-r--r-- | lib/hcl/harvest_middleware.rb | 2 | ||||
| -rw-r--r-- | lib/hcl/net.rb | 55 | ||||
| -rw-r--r-- | lib/hcl/task.rb | 4 | ||||
| -rw-r--r-- | lib/hcl/timesheet_resource.rb | 57 | ||||
| -rw-r--r-- | test/day_entry_test.rb | 4 | ||||
| -rw-r--r-- | test/timesheet_resource_test.rb | 16 | 
10 files changed, 75 insertions, 76 deletions
| @@ -1,6 +1,7 @@  module HCl    autoload :VERSION, 'hcl/version'    autoload :App, 'hcl/app' +  autoload :Net, 'hcl/net'    autoload :Commands, 'hcl/commands'    autoload :TimesheetResource, 'hcl/timesheet_resource'    autoload :Utility, 'hcl/utility' diff --git a/lib/hcl/app.rb b/lib/hcl/app.rb index 671575c..1f641d5 100644 --- a/lib/hcl/app.rb +++ b/lib/hcl/app.rb @@ -140,7 +140,7 @@ EOM          if has_security_command?            load_password config          end -        TimesheetResource.configure config +        Net.configure config        else          request_config        end @@ -153,7 +153,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) -      TimesheetResource.configure config +      Net.configure config        write_config config      end diff --git a/lib/hcl/commands.rb b/lib/hcl/commands.rb index e9fd847..cf8ddc5 100644 --- a/lib/hcl/commands.rb +++ b/lib/hcl/commands.rb @@ -7,7 +7,7 @@ module HCl      # Display a sanitized view of your auth credentials.      def config -      TimesheetResource.config_hash.merge(password:'***'). +      Net.config_hash.merge(password:'***').          map {|k,v| "#{k}: #{v}" }.join("\n")      end diff --git a/lib/hcl/day_entry.rb b/lib/hcl/day_entry.rb index 6441f1d..645bb3d 100644 --- a/lib/hcl/day_entry.rb +++ b/lib/hcl/day_entry.rb @@ -6,7 +6,7 @@ module HCl      # defaults to today.      def self.all date = nil        url = date.nil? ? 'daily' : "daily/#{date.strftime '%j/%Y'}" -      doc = get url +      doc = Net.get url        Task.cache_tasks_hash doc        doc[:day_entries].map {|e| new e}      end @@ -37,7 +37,7 @@ module HCl        # If I don't include hours it gets reset.        # This doens't appear to be the case for task and project.        (self.notes << "\n#{new_notes}").lstrip! -      DayEntry.post "daily/update/#{id}", notes:notes, hours:hours +      Net.post "daily/update/#{id}", notes:notes, hours:hours      end      def self.with_timer date=nil @@ -63,7 +63,7 @@ module HCl      end      def toggle -      DayEntry.get("daily/timer/#{id}") +      Net.get("daily/timer/#{id}")        self      end diff --git a/lib/hcl/harvest_middleware.rb b/lib/hcl/harvest_middleware.rb index 47e790d..4d742ed 100644 --- a/lib/hcl/harvest_middleware.rb +++ b/lib/hcl/harvest_middleware.rb @@ -18,7 +18,7 @@ class HCl::HarvestMiddleware < FaradayMiddleware::ResponseMiddleware        case env[:status]        when 200..299          begin  -          env[:body] = unescape(MultiJson.load(env[:body].chomp, symbolize_keys:true)) +          env[:body] = unescape(MultiJson.load(env[:body], symbolize_keys:true))          rescue MultiJson::LoadError            env[:body]          end diff --git a/lib/hcl/net.rb b/lib/hcl/net.rb new file mode 100644 index 0000000..2339f1d --- /dev/null +++ b/lib/hcl/net.rb @@ -0,0 +1,55 @@ +require 'faraday_middleware' + +module HCl +  module Net +    # configuration accessors +    CONFIG_VARS = [ :login, :password, :subdomain, :ssl ].freeze +    CONFIG_VARS.each do |config_var| +      class_eval <<-EOC +        def self.#{config_var}= arg +          @@#{config_var} = arg +        end +        def self.#{config_var} +          @@#{config_var} +        end +      EOC +    end + +    def self.configure opts = nil +      if opts +        self.login = opts['login'] +        self.password = opts['password'] +        self.subdomain = opts['subdomain'] +        self.ssl = opts['ssl'] +      end +    end + +    def self.config_hash +      CONFIG_VARS.inject({}) {|c,k| c.update(k => send(k)) } +    end + +    def self.faraday +      @faraday ||= Faraday.new( +        "http#{ssl && 's'}://#{subdomain}.harvestapp.com" +      ) do |f| +        f.headers['Accept'] = 'application/json' +        f.request :json +        f.request :basic_auth, login, password +        f.use HCl::HarvestMiddleware, content_type: /\bjson\b/ +        f.adapter Faraday.default_adapter +      end +    end + +    def self.get action +      faraday.get(action).body +    end + +    def self.post action, data +      faraday.post(action, data).body +    end + +    def self.delete action +      faraday.delete(action).body +    end +  end +end diff --git a/lib/hcl/task.rb b/lib/hcl/task.rb index 7678655..e1d5b58 100644 --- a/lib/hcl/task.rb +++ b/lib/hcl/task.rb @@ -50,7 +50,7 @@ module HCl      def add opts        notes = opts[:note]        starting_time = opts[:starting_time] || 0 -      DayEntry.new Task.post("daily/add", { +      DayEntry.new Net.post("daily/add", {          notes: notes,          hours: starting_time,          project_id: project.id, @@ -64,7 +64,7 @@ module HCl        if day.running?          day        else -        DayEntry.new Task.get("daily/timer/#{day.id}") +        DayEntry.new Net.get("daily/timer/#{day.id}")        end      end    end diff --git a/lib/hcl/timesheet_resource.rb b/lib/hcl/timesheet_resource.rb index 518ebf8..28e5378 100644 --- a/lib/hcl/timesheet_resource.rb +++ b/lib/hcl/timesheet_resource.rb @@ -1,66 +1,9 @@ -require 'net/http' -require 'net/https' -require 'cgi' -require 'faraday_middleware' -  module HCl    class TimesheetResource - -    def self.configure opts = nil -      if opts -        self.login = opts['login'] -        self.password = opts['password'] -        self.subdomain = opts['subdomain'] -        self.ssl = opts['ssl'] -      end -    end - -    # configuration accessors -    CONFIG_VARS = [ :login, :password, :subdomain, :ssl ].freeze -    CONFIG_VARS.each do |config_var| -      class_eval <<-EOC -        def self.#{config_var}= arg -          @@#{config_var} = arg -        end -        def self.#{config_var} -          @@#{config_var} -        end -      EOC -    end - -    # @return [Hash] -    def self.config_hash -      CONFIG_VARS.inject({}) {|c,k| c.update(k => TimesheetResource.send(k)) } -    end - -    def self.faraday -      @faraday ||= Faraday.new( -        "http#{ssl && 's'}://#{subdomain}.harvestapp.com" -      ) do |f| -        f.headers['Accept'] = 'application/json' -        f.request :json -        f.request :basic_auth, login, password -        f.use HCl::HarvestMiddleware, content_type: /\bjson\b/ -        f.adapter Faraday.default_adapter -      end -    end -      def initialize params        @data = params      end -    def self.get action -      faraday.get(action).body -    end - -    def self.post action, data -      faraday.post(action, data).body -    end - -    def self.delete action -      faraday.delete(action).body -    end -      def id        @data[:id]      end diff --git a/test/day_entry_test.rb b/test/day_entry_test.rb index ec2abe2..3e8be24 100644 --- a/test/day_entry_test.rb +++ b/test/day_entry_test.rb @@ -21,14 +21,14 @@ class DayEntryTest < HCl::TestCase    def test_append_note      entry = HCl::DayEntry.new(:id => '1', :notes => 'yourmom.', :hours => '1.0') -    HCl::DayEntry.stubs(:post) +    HCl::Net.stubs(:post)      entry.append_note('hi world')      assert_equal "yourmom.\nhi world", entry.notes    end    def test_append_note_to_empty      entry = HCl::DayEntry.new(:id => '1', :notes => nil, :hours => '1.0') -    HCl::DayEntry.stubs(:post) +    HCl::Net.stubs(:post)      entry.append_note('hi world')      assert_equal 'hi world', entry.notes    end diff --git a/test/timesheet_resource_test.rb b/test/timesheet_resource_test.rb index 1f1b1de..7397c0e 100644 --- a/test/timesheet_resource_test.rb +++ b/test/timesheet_resource_test.rb @@ -4,7 +4,7 @@ class TimesheetResourceTest < HCl::TestCase    def setup      FakeWeb.allow_net_connect = false -    HCl::TimesheetResource.configure \ +    HCl::Net.configure \        'login' => 'bob',        'password' => 'secret',        'subdomain' => 'bobclock', @@ -12,30 +12,30 @@ class TimesheetResourceTest < HCl::TestCase    end    def test_configure -    assert_equal 'bob', HCl::TimesheetResource.login -    assert_equal 'secret', HCl::TimesheetResource.password -    assert_equal 'bobclock', HCl::TimesheetResource.subdomain -    assert_equal true, HCl::TimesheetResource.ssl +    assert_equal 'bob', HCl::Net.login +    assert_equal 'secret', HCl::Net.password +    assert_equal 'bobclock', HCl::Net.subdomain +    assert_equal true, HCl::Net.ssl    end    def test_http_get      FakeWeb.register_uri(:get, "https://bob:secret@bobclock.harvestapp.com/foo",                           :body => 'gotten!'.inspect) -    body = HCl::TimesheetResource.get 'foo' +    body = HCl::Net.get 'foo'      assert_equal 'gotten!', body    end    def test_http_post      FakeWeb.register_uri(:post, "https://bob:secret@bobclock.harvestapp.com/foo",                           :body => 'posted!'.inspect) -    body = HCl::TimesheetResource.post 'foo', {pizza:'taco'} +    body = HCl::Net.post 'foo', {pizza:'taco'}      assert_equal 'posted!', body    end    def test_http_delete      FakeWeb.register_uri(:delete, "https://bob:secret@bobclock.harvestapp.com/foo",                           :body => 'wiped!'.inspect) -    body = HCl::TimesheetResource.delete 'foo' +    body = HCl::Net.delete 'foo'      assert_equal 'wiped!', body    end  end | 
