summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorZack Hobson2013-12-27 13:19:57 -0800
committerZack Hobson2013-12-27 13:19:57 -0800
commit8e2e788909c6a634976c80eeb75c79af8f8ab734 (patch)
tree8314e9ad539476d0e327ad20abde667203f4115f /lib
parent89867bd84610d6d5622540a7e26b6c3467d632bf (diff)
downloadhcl-8e2e788909c6a634976c80eeb75c79af8f8ab734.tar.bz2
pass http client into instance commands
Diffstat (limited to 'lib')
-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
6 files changed, 40 insertions, 29 deletions
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