lib/googleauth/signet.rb in googleauth-0.4.2 vs lib/googleauth/signet.rb in googleauth-0.5.0
- old
+ new
@@ -40,11 +40,11 @@
class Client
# Updates a_hash updated with the authentication token
def apply!(a_hash, opts = {})
# fetch the access token there is currently not one, or if the client
# has expired
- fetch_access_token!(opts) if access_token.nil? || expired?
+ fetch_access_token!(opts) if access_token.nil? || expires_within?(60)
a_hash[AUTH_METADATA_KEY] = "Bearer #{access_token}"
end
# Returns a clone of a_hash updated with the authentication token
def apply(a_hash, opts = {})
@@ -55,9 +55,28 @@
# Returns a reference to the #apply method, suitable for passing as
# a closure
def updater_proc
lambda(&method(:apply))
+ end
+
+ def on_refresh(&block)
+ @refresh_listeners ||= []
+ @refresh_listeners << block
+ end
+
+ alias_method :orig_fetch_access_token!, :fetch_access_token!
+ def fetch_access_token!(options = {})
+ info = orig_fetch_access_token!(options)
+ notify_refresh_listeners
+ info
+ end
+
+ def notify_refresh_listeners
+ listeners = @refresh_listeners || []
+ listeners.each do |block|
+ block.call(self)
+ end
end
end
end
end