From 3973b851e9b14d6201431d39ed90d6ad9293752f Mon Sep 17 00:00:00 2001 From: Greg Gates Date: Wed, 15 Oct 2014 10:42:16 -0400 Subject: [PATCH] Reuse auth tokens when possible --- CHANGELOG.md | 2 ++ lib/google_cells/fetcher.rb | 17 ++++++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bacea8d..9863965 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,5 @@ ++ Don't get a new access token on each request if we already have one. + Version 0.4.0 ========================= diff --git a/lib/google_cells/fetcher.rb b/lib/google_cells/fetcher.rb index bb570f8..18ff36b 100644 --- a/lib/google_cells/fetcher.rb +++ b/lib/google_cells/fetcher.rb @@ -15,7 +15,9 @@ def request(method, url, params={}) url << '?' unless url[-1] == "?" url << params[:url_params].to_a.map{|k,v| "#{k}=#{v}"}.join('&') end - GoogleCells.client.authorization.fetch_access_token! + + authorize + GoogleCells.client.execute!( :http_method => method, :uri => url, @@ -23,5 +25,18 @@ def request(method, url, params={}) :body => params[:body] ) end + + def authorize + if !auth.access_token || auth.expired? + GoogleCells.client.authorization.fetch_access_token! + end + end + + private + + def auth + GoogleCells.client.authorization + end + end end