summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--README.markdown12
-rw-r--r--lib/hcl/app.rb4
-rw-r--r--lib/hcl/commands.rb14
-rw-r--r--lib/hcl/day_entry.rb12
-rw-r--r--lib/hcl/net.rb16
-rw-r--r--lib/hcl/task.rb10
-rw-r--r--lib/hcl/timesheet_resource.rb13
-rw-r--r--test/command_test.rb4
-rw-r--r--test/day_entry_test.rb10
-rw-r--r--test/task_test.rb6
-rw-r--r--test/test_helper.rb3
11 files changed, 59 insertions, 45 deletions
diff --git a/README.markdown b/README.markdown
index 4b05856..d6fe847 100644
--- a/README.markdown
+++ b/README.markdown
@@ -144,17 +144,19 @@ powerful Harvest API client built into HCl, since not all of its
features are exposed via the command line. The current {HCl::App}
instance is available as `hcl`.
-It's also possible to issue HCl commands directly (as Ruby methods), or
-to query specific JSON end points and have the results pretty-printed:
+It's also possible to issue HCl commands directly (except `alias`, see
+below), or to query specific JSON end points and have the results
+pretty-printed:
hcl console
hcl> show "yesterday"
# => prints yesterday's timesheet, note the quotes!
- hcl> Net.get('daily')
+ hcl> hcl.http.get('daily')
# => displays a pretty-printed version of the JSON output
-Note that unlike the commands themselves, the HCl internals may change without
-notice.
+Note that the the HCl internals may change without notice.
+Also, commands (like `alias`) that are also reserved words in Ruby
+can't be issued directly (use `send :alias` instead).
### Date Formats
diff --git a/lib/hcl/app.rb b/lib/hcl/app.rb
index 115ab65..2799f12 100644
--- a/lib/hcl/app.rb
+++ b/lib/hcl/app.rb
@@ -20,6 +20,10 @@ module HCl
self
end
+ def http
+ HCl::Net
+ end
+
# Run the given command and arguments.
def self.command *args
new.process_args(*args).run
diff --git a/lib/hcl/commands.rb b/lib/hcl/commands.rb
index 834ba2c..17c86b1 100644
--- a/lib/hcl/commands.rb
+++ b/lib/hcl/commands.rb
@@ -8,7 +8,7 @@ module HCl
# Display a sanitized view of your auth credentials.
def config
- Net.config_hash.merge(password:'***').map {|k,v| "#{k}: #{v}" }.join("\n")
+ http.config_hash.merge(password:'***').map {|k,v| "#{k}: #{v}" }.join("\n")
end
def console
@@ -46,7 +46,7 @@ module HCl
def cancel
entry = DayEntry.with_timer || DayEntry.last
if entry
- if entry.cancel
+ if entry.cancel http
"Deleted entry #{entry}."
else
fail "Failed to delete #{entry}!"
@@ -92,7 +92,7 @@ module HCl
if task.nil?
fail "Unknown task alias, try one of the following: ", aliases.join(', ')
end
- timer = task.start \
+ timer = task.start http,
:starting_time => starting_time,
:note => args.join(' ')
"Started timer for #{timer} (at #{current_time})"
@@ -107,8 +107,8 @@ module HCl
def stop *args
entry = DayEntry.with_timer || DayEntry.with_timer(DateTime.yesterday)
if entry
- entry.append_note(args.join(' ')) if args.any?
- entry.toggle
+ entry.append_note(http, args.join(' ')) if args.any?
+ entry.toggle http
"Stopped #{entry} (at #{current_time})"
else
fail "No running timers found."
@@ -121,7 +121,7 @@ module HCl
if args.empty?
return entry.notes
else
- entry.append_note args.join(' ')
+ entry.append_note http, args.join(' ')
"Added note to #{entry}."
end
else
@@ -152,7 +152,7 @@ module HCl
DayEntry.last
end
if entry
- entry.toggle
+ entry.toggle http
else
fail "No matching timer found."
end
diff --git a/lib/hcl/day_entry.rb b/lib/hcl/day_entry.rb
index 8485139..0a54bfd 100644
--- a/lib/hcl/day_entry.rb
+++ b/lib/hcl/day_entry.rb
@@ -15,9 +15,9 @@ module HCl
@data[:task]
end
- def cancel
+ def cancel http
begin
- Net.delete("daily/delete/#{id}")
+ http.delete("daily/delete/#{id}")
rescue HarvestMiddleware::Failure
return false
end
@@ -29,11 +29,11 @@ module HCl
end
# Append a string to the notes for this task.
- def append_note new_notes
+ def append_note http, new_notes
# 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!
- Net.post "daily/update/#{id}", notes:notes, hours:hours
+ http.post "daily/update/#{id}", notes:notes, hours:hours
end
def self.with_timer date=nil
@@ -58,8 +58,8 @@ module HCl
# TODO cache client/project names and ids
end
- def toggle
- Net.get("daily/timer/#{id}")
+ def toggle http
+ http.get("daily/timer/#{id}")
self
end
diff --git a/lib/hcl/net.rb b/lib/hcl/net.rb
index 01fb737..334571c 100644
--- a/lib/hcl/net.rb
+++ b/lib/hcl/net.rb
@@ -5,8 +5,8 @@ module HCl
module Net
class << self
# configuration accessors
- CONFIG_VARS = [ :login, :password, :subdomain, :ssl ].freeze
- CONFIG_VARS.each { |config_var| attr_reader config_var }
+ 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)) }
@@ -17,27 +17,25 @@ module HCl
@password = opts['password'].freeze
@subdomain = opts['subdomain'].freeze
@ssl = !!opts['ssl']
- end
-
- def http
- @http ||= Faraday.new(
+ @http = Faraday.new(
"http#{ssl ? 's' : '' }://#{subdomain}.harvestapp.com"
) do |f|
f.use :harvest, login, password
f.adapter Faraday.default_adapter
end
+ self
end
def get action
- http.get(action).body
+ @http.get(action).body
end
def post action, data
- http.post(action, data).body
+ @http.post(action, data).body
end
def delete action
- http.delete(action).body
+ @http.delete(action).body
end
end
end
diff --git a/lib/hcl/task.rb b/lib/hcl/task.rb
index e1d5b58..89e3b97 100644
--- a/lib/hcl/task.rb
+++ b/lib/hcl/task.rb
@@ -47,10 +47,10 @@ module HCl
end
end
- def add opts
+ def add http, opts
notes = opts[:note]
starting_time = opts[:starting_time] || 0
- DayEntry.new Net.post("daily/add", {
+ DayEntry.new http.post("daily/add", {
notes: notes,
hours: starting_time,
project_id: project.id,
@@ -59,12 +59,12 @@ module HCl
})
end
- def start opts
- day = add opts
+ def start http, opts
+ day = add http, opts
if day.running?
day
else
- DayEntry.new Net.get("daily/timer/#{day.id}")
+ DayEntry.new http.get("daily/timer/#{day.id}")
end
end
end
diff --git a/lib/hcl/timesheet_resource.rb b/lib/hcl/timesheet_resource.rb
index 54c74f1..10f8586 100644
--- a/lib/hcl/timesheet_resource.rb
+++ b/lib/hcl/timesheet_resource.rb
@@ -16,6 +16,10 @@ 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|
@@ -34,6 +38,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
@@ -41,7 +50,7 @@ module HCl
send(method, name) do |*args|
url = instance_exec *args, &res[:url_cb]
cb = res[:opts][:load_cb]
- Net.get(url).tap{|e| cb.call(e) if cb }[cls.collection_name].map{|e|new(e)}
+ http.get(url).tap{|e| cb.call(e) if cb }[cls.collection_name].map{|e|new(e)}
end
end
@@ -52,7 +61,7 @@ module HCl
send(method, name) do |*args|
url = instance_exec *args, &res[:url_cb]
cb = res[:opts][:load_cb]
- cls.new Net.get(url).tap{|e| cb.call(e) if cb }[cls.underscore_name]
+ cls.new http.get(url).tap{|e| cb.call(e) if cb }[cls.underscore_name]
end
end
diff --git a/test/command_test.rb b/test/command_test.rb
index 56c7d93..a0cdca1 100644
--- a/test/command_test.rb
+++ b/test/command_test.rb
@@ -69,7 +69,7 @@ class CommandTest < HCl::TestCase
project: HCl::Project.new(id:456, name:'App', client:'Bob', code:'b')
)
HCl::Task.expects(:find).with('456','123').returns(task)
- task.expects(:start).with(starting_time:nil, note:'do stuff')
+ task.expects(:start).with(http, starting_time:nil, note:'do stuff')
start *%w[ 456 123 do stuff ]
end
@@ -106,7 +106,7 @@ class CommandTest < HCl::TestCase
def test_note
entry = stub
HCl::DayEntry.expects(:with_timer).returns(entry)
- entry.expects(:append_note).with('hi world')
+ entry.expects(:append_note).with(http, 'hi world')
note 'hi world'
end
diff --git a/test/day_entry_test.rb b/test/day_entry_test.rb
index 5b53463..a685bc0 100644
--- a/test/day_entry_test.rb
+++ b/test/day_entry_test.rb
@@ -25,19 +25,19 @@ class DayEntryTest < HCl::TestCase
def test_toggle
entry = HCl::DayEntry.new(id:123)
register_uri(:get, '/daily/timer/123', {note:'hi'})
- entry.toggle
+ entry.toggle http
end
def test_cancel_success
entry = HCl::DayEntry.new(id:123)
register_uri(:delete, '/daily/delete/123')
- assert entry.cancel
+ assert entry.cancel http
end
def test_cancel_failure
entry = HCl::DayEntry.new(id:123)
HCl::Net.expects(:delete).raises(HCl::HarvestMiddleware::Failure)
- assert !entry.cancel
+ assert !entry.cancel(http)
end
def test_to_s
@@ -49,14 +49,14 @@ class DayEntryTest < HCl::TestCase
def test_append_note
entry = HCl::DayEntry.new(:id => '1', :notes => 'yourmom.', :hours => '1.0')
HCl::Net.stubs(:post)
- entry.append_note('hi world')
+ 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)
- entry.append_note('hi world')
+ entry.append_note(http, 'hi world')
assert_equal 'hi world', entry.notes
end
end
diff --git a/test/task_test.rb b/test/task_test.rb
index 3deb620..b224ed3 100644
--- a/test/task_test.rb
+++ b/test/task_test.rb
@@ -23,7 +23,7 @@ class TaskTest < HCl::TestCase
task = HCl::Task.new(id:1, project:HCl::Project.new({id:2}))
register_uri(:post, '/daily/add', {
note:'good stuff', hours:0.2, project_id:2, task_id:1, spent_at: Date.today})
- entry = task.add(note:'good stuff', starting_time:0.2)
+ entry = task.add(http, note:'good stuff', starting_time:0.2)
assert_equal 'good stuff', entry.note
end
@@ -32,7 +32,7 @@ class TaskTest < HCl::TestCase
register_uri(:post, '/daily/add', {
note:'good stuff', timer_started_at:DateTime.now,
hours:0.2, project_id:2, task_id:1, spent_at: Date.today})
- entry = task.start(note:'good stuff', starting_time:0.2)
+ entry = task.start(http, note:'good stuff', starting_time:0.2)
assert_equal 'good stuff', entry.note
end
@@ -41,7 +41,7 @@ class TaskTest < HCl::TestCase
register_uri(:post, '/daily/add', {id:123, note:'woot'})
register_uri(:get, '/daily/timer/123', {note:'good stuff', hours:0.2,
project_id:2, task_id:1, spent_at: Date.today})
- entry = task.start(note:'good stuff', starting_time:0.2)
+ entry = task.start(http, note:'good stuff', starting_time:0.2)
assert_equal 'good stuff', entry.note
end
end
diff --git a/test/test_helper.rb b/test/test_helper.rb
index 47d3517..33cf499 100644
--- a/test/test_helper.rb
+++ b/test/test_helper.rb
@@ -28,9 +28,10 @@ require 'fakeweb'
Dir[File.dirname(__FILE__) + '/ext/*.rb'].each { |ext| require ext }
class HCl::TestCase < MiniTest::Unit::TestCase
+ attr_reader :http
def setup
FakeWeb.allow_net_connect = false
- HCl::Net.configure \
+ @http = HCl::Net.configure \
'login' => 'bob',
'password' => 'secret',
'subdomain' => 'bobclock',