summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorZack Hobson2013-11-21 15:21:24 -0800
committerZack Hobson2013-11-21 15:21:24 -0800
commit6d42e52a997514f02beab65f8e465c3614cef041 (patch)
treeeccea95ca82af531d0f72c127785e59adb5fd730 /lib
parent63a48ab55f6cf1e2a2c5a8e309c10442f7416f96 (diff)
downloadhcl-6d42e52a997514f02beab65f8e465c3614cef041.tar.bz2
Fix user setup, reconfigure on auth failure.
Diffstat (limited to 'lib')
-rw-r--r--lib/hcl/app.rb31
-rw-r--r--lib/hcl/timesheet_resource.rb12
2 files changed, 27 insertions, 16 deletions
diff --git a/lib/hcl/app.rb b/lib/hcl/app.rb
index 970bd6d..4ff63d5 100644
--- a/lib/hcl/app.rb
+++ b/lib/hcl/app.rb
@@ -2,14 +2,13 @@ require 'yaml'
require 'fileutils'
require 'trollop'
-require 'highline'
+require 'highline/import'
module HCl
class App
include HCl::Utility
include HCl::Commands
-
HCL_DIR = ENV['HCL_DIR'] || "#{ENV['HOME']}/.hcl"
SETTINGS_FILE = "#{HCL_DIR}/settings.yml"
CONFIG_FILE = "#{HCL_DIR}/config.yml"
@@ -61,6 +60,10 @@ module HCl
rescue SocketError => e
STDERR.puts "Connection failed. (#{e.message})"
exit 1
+ rescue TimesheetResource::AuthFailure => e
+ STDERR.puts "Unable to authenticate: #{e}"
+ request_config
+ run
rescue TimesheetResource::Failure => e
STDERR.puts "API failure: #{e}"
exit 1
@@ -122,9 +125,9 @@ EOM
self
end
- protected
+ private
- def read_config
+ def read_config force=false
if File.exists? CONFIG_FILE
config = YAML::load File.read(CONFIG_FILE)
TimesheetResource.configure config
@@ -133,17 +136,21 @@ EOM
TimesheetResource.configure config
write_config config
else
- config = {}
- puts "Please specify your Harvest credentials.\n"
- config['login'] = ask("Email Address: ").to_s
- config['password'] = ask("Password: ") { |q| q.echo = false }.to_s
- config['subdomain'] = ask("Subdomain: ").to_s
- config['ssl'] = %w(y yes).include?(ask("Use SSL? (y/n): ").downcase)
- TimesheetResource.configure config
- write_config config
+ request_config
end
end
+ def request_config
+ config = {}
+ puts "Please specify your Harvest credentials.\n"
+ config['login'] = ask("Email Address: ").to_s
+ config['password'] = ask("Password: ") { |q| q.echo = false }.to_s
+ config['subdomain'] = ask("Subdomain: ").to_s
+ config['ssl'] = %w(y yes).include?(ask("Use SSL? (y/n): ").downcase)
+ TimesheetResource.configure config
+ write_config config
+ end
+
def write_config config
puts "Writing configuration to #{CONFIG_FILE}."
File.open(CONFIG_FILE, 'w') do |f|
diff --git a/lib/hcl/timesheet_resource.rb b/lib/hcl/timesheet_resource.rb
index 615fb2a..7ab99ec 100644
--- a/lib/hcl/timesheet_resource.rb
+++ b/lib/hcl/timesheet_resource.rb
@@ -16,6 +16,7 @@ end
module HCl
class TimesheetResource
class Failure < StandardError; end
+ class AuthFailure < StandardError; end
def self.configure opts = nil
if opts
@@ -64,12 +65,15 @@ module HCl
request.content_type = 'application/xml'
request['Accept'] = 'application/xml'
response = https.request request, data
- if response.kind_of? Net::HTTPSuccess
+ case response
+ when Net::HTTPSuccess
response.body
- elsif response.kind_of? Net::HTTPFound
- raise Failure, "Redirected in the request. Perhaps your ssl configuration variable is set incorrectly?"
+ when Net::HTTPFound
+ raise Failure, "Redirected! Perhaps your ssl configuration variable is set incorrectly?"
+ when Net::HTTPUnauthorized
+ raise AuthFailure, "Login failed."
else
- raise Failure, "Unexpected response from the upstream API"
+ raise Failure, "Unexpected response from the upstream API."
end
end