Module: NbApiClient::OAuthTokenRefresh
- Defined in:
- lib/nb_api_client/oauth_token_refresh.rb
Overview
Optional mixin implementing refresh_oauth_token (see README's "nation
interface") for nations backed by an ActiveRecord-style model with
token, refresh_token, token_expires_at columns, and #with_lock/
#update!. include NbApiClient::OAuthTokenRefresh instead of writing
your own refresh_oauth_token; write your own if your model differs
(no with_lock, different column names, etc).
Instance Method Summary collapse
Instance Method Details
#refresh_oauth_token ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/nb_api_client/oauth_token_refresh.rb', line 11 def refresh_oauth_token with_lock do # Re-check after acquiring the lock in case another process already # refreshed the token while this one was waiting. return true if Time.now - updated_at < 5 response = HTTParty.post( "https://#{slug}.nationbuilder.com/oauth/token", body: { grant_type: "refresh_token", refresh_token: refresh_token, client_id: NbApiClient.configuration.oauth_client_id, client_secret: NbApiClient.configuration.oauth_client_secret } ) return false unless response.success? update!( token: response["access_token"], refresh_token: response["refresh_token"], token_expires_at: response["expires_in"] ? Time.now + response["expires_in"].to_i : nil ) end rescue => e NbApiClient.configuration.logger&.error("[NbApiClient::OAuthTokenRefresh] Failed to refresh OAuth token for #{slug}: #{e.}") false end |