From a96ce5e947f6559b66661fe09fdaf26d82bf6838 Mon Sep 17 00:00:00 2001 From: Ken Mayer Date: Mon, 28 Oct 2013 13:17:05 -0700 Subject: [PATCH] Defer evaluation of the Heroku auth token - HerokuSan::API was trying to evaluate the auth token at construction time, instead of waiting until it was actually needed, if ever. - Fixes #156 --- lib/heroku_san/api.rb | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/heroku_san/api.rb b/lib/heroku_san/api.rb index a7bb33d..e70ba38 100644 --- a/lib/heroku_san/api.rb +++ b/lib/heroku_san/api.rb @@ -4,8 +4,6 @@ module HerokuSan class API def initialize(options = {}) @options = options - @options[:api_key] ||= auth_token - @heroku_api = Heroku::API.new(@options) end def sh(app, *command) @@ -23,7 +21,7 @@ def sh(app, *command) end def method_missing(name, *args) - @heroku_api.send(name, *args) + heroku_api.send(name, *args) rescue Heroku::API::Errors::ErrorWithResponse => error status = error.response.headers["Status"] msg = JSON.parse(error.response.body)['error'] rescue '???' @@ -34,6 +32,13 @@ def method_missing(name, *args) private + def heroku_api + @heroku_api ||= begin + @options[:api_key] ||= auth_token + Heroku::API.new(@options) + end + end + def auth_token ENV['HEROKU_API_KEY'] || Bundler.with_clean_env { `heroku auth:token`.chomp } rescue Errno::ENOENT