diff options
| author | Zack Hobson | 2013-12-27 13:34:14 -0800 | 
|---|---|---|
| committer | Zack Hobson | 2013-12-27 13:34:14 -0800 | 
| commit | 4e6cf745e5d8426f1470c26f705bacbad1514e91 (patch) | |
| tree | 2e6d7b2882e6a6208ed47de848ee641dd80ad227 | |
| parent | 8e2e788909c6a634976c80eeb75c79af8f8ab734 (diff) | |
| download | hcl-4e6cf745e5d8426f1470c26f705bacbad1514e91.tar.bz2 | |
non-global API for net access
| -rw-r--r-- | lib/hcl/commands.rb | 14 | ||||
| -rw-r--r-- | lib/hcl/day_entry.rb | 17 | ||||
| -rw-r--r-- | lib/hcl/timesheet_resource.rb | 13 | ||||
| -rw-r--r-- | test/command_test.rb | 8 | ||||
| -rw-r--r-- | test/day_entry_test.rb | 14 | 
5 files changed, 26 insertions, 40 deletions
| diff --git a/lib/hcl/commands.rb b/lib/hcl/commands.rb index 17c86b1..0811260 100644 --- a/lib/hcl/commands.rb +++ b/lib/hcl/commands.rb @@ -44,7 +44,7 @@ module HCl      end      def cancel -      entry = DayEntry.with_timer || DayEntry.last +      entry = DayEntry.with_timer(http) || DayEntry.last(http)        if entry          if entry.cancel http            "Deleted entry #{entry}." @@ -99,13 +99,13 @@ module HCl      end      def log *args -      fail "There is already a timer running." if DayEntry.with_timer +      fail "There is already a timer running." if DayEntry.with_timer(http)        start *args        stop      end      def stop *args -      entry = DayEntry.with_timer || DayEntry.with_timer(DateTime.yesterday) +      entry = DayEntry.with_timer(http) || DayEntry.with_timer(http, DateTime.yesterday)        if entry          entry.append_note(http, args.join(' ')) if args.any?          entry.toggle http @@ -116,7 +116,7 @@ module HCl      end      def note *args -      entry = DayEntry.with_timer +      entry = DayEntry.with_timer http        if entry          if args.empty?            return entry.notes @@ -133,7 +133,7 @@ module HCl        date = args.empty? ? nil : Chronic.parse(args.join(' '))        total_hours = 0.0        result = '' -      DayEntry.daily(date).each do |day| +      DayEntry.daily(http, date).each do |day|          running = day.running? ? '(running) ' : ''          columns = HighLine::SystemExtensions.terminal_size[0] rescue 80          result << "\t#{day.formatted_hours}\t#{running}#{day.project}: #{day.notes.lines.to_a.last}\n"[0..columns-1] @@ -147,9 +147,9 @@ module HCl        ident = get_ident args        entry = if ident            task_ids = get_task_ids ident, args -          DayEntry.last_by_task *task_ids +          DayEntry.last_by_task http, *task_ids          else -          DayEntry.last +          DayEntry.last(http)          end        if entry          entry.toggle http diff --git a/lib/hcl/day_entry.rb b/lib/hcl/day_entry.rb index 0a54bfd..c1215ed 100644 --- a/lib/hcl/day_entry.rb +++ b/lib/hcl/day_entry.rb @@ -36,28 +36,23 @@ module HCl        http.post "daily/update/#{id}", notes:notes, hours:hours      end -    def self.with_timer date=nil -      daily(date).detect {|t| t.running? } +    def self.with_timer http, date=nil +      daily(http, date).detect {|t| t.running? }      end -    def self.last_by_task project_id, task_id -      today.sort {|a,b| b.updated_at<=>a.updated_at}. +    def self.last_by_task http, project_id, task_id +      today(http).sort {|a,b| b.updated_at<=>a.updated_at}.          detect {|t| t.project_id == project_id && t.task_id == task_id }      end -    def self.last -      today.sort {|a,b| a.updated_at<=>b.updated_at}[-1] +    def self.last http +      today(http).sort {|a,b| a.updated_at<=>b.updated_at}[-1]      end      def running?        !@data[:timer_started_at].nil? && !@data[:timer_started_at].empty?      end -    def initialize *args -      super -      # TODO cache client/project names and ids -    end -      def toggle http        http.get("daily/timer/#{id}")        self diff --git a/lib/hcl/timesheet_resource.rb b/lib/hcl/timesheet_resource.rb index 10f8586..e230f1f 100644 --- a/lib/hcl/timesheet_resource.rb +++ b/lib/hcl/timesheet_resource.rb @@ -16,10 +16,6 @@ module HCl        (@data && @data.key?(method.to_sym)) || super      end -    def http -      self.class.http -    end -      class << self        def _prepare_resource name, *args, &url_cb          ((@resources ||= {})[name] = {}).tap do |res| @@ -38,16 +34,11 @@ module HCl          end        end -      def http -        # XXX how do I get the app instance in here? -        HCl::Net -      end -        def resources name, *args, &url_cb          res = _prepare_resource name, *args, &url_cb          cls = res[:opts][:class_name] ? HCl.const_get(res[:opts][:class_name]) : self          method = cls == self ? :define_singleton_method : :define_method -        send(method, name) do |*args| +        send(method, name) do |http, *args|            url = instance_exec *args, &res[:url_cb]            cb = res[:opts][:load_cb]            http.get(url).tap{|e| cb.call(e) if cb }[cls.collection_name].map{|e|new(e)} @@ -58,7 +49,7 @@ module HCl          res = _prepare_resource name, *args, &url_cb          cls = res[:opts][:class_name] ? HCl.const_get(res[:opts][:class_name]) : self          method = cls == self ? :define_singleton_method : :define_method -        send(method, name) do |*args| +        send(method, name) do |http, *args|            url = instance_exec *args, &res[:url_cb]            cb = res[:opts][:load_cb]            cls.new http.get(url).tap{|e| cb.call(e) if cb }[cls.underscore_name] diff --git a/test/command_test.rb b/test/command_test.rb index a0cdca1..2e2812f 100644 --- a/test/command_test.rb +++ b/test/command_test.rb @@ -91,15 +91,15 @@ class CommandTest < HCl::TestCase    def test_resume_with_task_alias      entry = stub      expects(:get_task_ids).with('mytask',[]).returns(%w[ 456 789 ]) -    HCl::DayEntry.expects(:last_by_task).with('456', '789').returns(entry) -    entry.expects(:toggle) +    HCl::DayEntry.expects(:last_by_task).with(http, '456', '789').returns(entry) +    entry.expects(:toggle).with(http)      resume 'mytask'    end    def test_cancel      entry = stub -    HCl::DayEntry.expects(:with_timer).returns(entry) -    entry.expects(:cancel).returns(true) +    HCl::DayEntry.expects(:with_timer).with(http).returns(entry) +    entry.expects(:cancel).with(http).returns(true)      cancel    end diff --git a/test/day_entry_test.rb b/test/day_entry_test.rb index a685bc0..fd8543a 100644 --- a/test/day_entry_test.rb +++ b/test/day_entry_test.rb @@ -4,22 +4,22 @@ class DayEntryTest < HCl::TestCase    def test_project_info      register_uri(:get, '/daily', {projects:[], day_entries:[{project_id:123}]})      register_uri(:get, '/projects/123', {project:{name:'fun times'}}) -    assert_equal 'fun times', HCl::DayEntry.today.first.project_info.name +    assert_equal 'fun times', HCl::DayEntry.today(http).first.project_info(http).name    end    def test_all_today_empty      register_uri(:get, '/daily', {projects:[],day_entries:[]}) -    assert HCl::DayEntry.today.empty? +    assert HCl::DayEntry.today(http).empty?    end    def test_all_today      register_uri(:get, '/daily', {projects:[], day_entries:[{id:1,note:'hi'}]}) -    assert_equal 'hi', HCl::DayEntry.today.first.note +    assert_equal 'hi', HCl::DayEntry.today(http).first.note    end    def test_all_with_date      register_uri(:get, '/daily/013/2013', {projects:[], day_entries:[{id:1,note:'hi'}]}) -    assert_equal 'hi', HCl::DayEntry.daily(Date.civil(2013,1,13)).first.note +    assert_equal 'hi', HCl::DayEntry.daily(http,Date.civil(2013,1,13)).first.note    end    def test_toggle @@ -36,7 +36,7 @@ class DayEntryTest < HCl::TestCase    def test_cancel_failure      entry = HCl::DayEntry.new(id:123) -    HCl::Net.expects(:delete).raises(HCl::HarvestMiddleware::Failure) +    http.expects(:delete).raises(HCl::HarvestMiddleware::Failure)      assert !entry.cancel(http)    end @@ -48,14 +48,14 @@ class DayEntryTest < HCl::TestCase    def test_append_note      entry = HCl::DayEntry.new(:id => '1', :notes => 'yourmom.', :hours => '1.0') -    HCl::Net.stubs(:post) +    http.stubs(:post)      entry.append_note(http, '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::Net.stubs(:post) +    http.stubs(:post)      entry.append_note(http, 'hi world')      assert_equal 'hi world', entry.notes    end | 
