class GoogleRefresh::Token

Constants

ACCESS_TOKEN_FAILURE_MESSAGE
ACCESS_TYPE
CONTENT_TYPE
DEFAULT_SCOPE
GOOGLE_AUTHORIZE_URL
GOOGLE_TOKEN_URL
GRANT_TYPE
MISSING_CLIENT_ID_ERROR_MESSAGE
MISSING_CLIENT_SECRET_ERROR_MESSAGE
MISSING_REFRESH_TOKEN_ERROR_MESSAGE
OAUTH_TOKEN_FAILURE_MESSAGE

Public Class Methods

new(client_id, client_secret, redirect_uri, refresh_token, scope=nil) click to toggle source
# File lib/google_refresh.rb, line 20
def initialize(client_id, client_secret, redirect_uri, refresh_token, scope=nil)
  @client_id = client_id
  @client_secret = client_secret
  @redirect_uri = redirect_uri
  @refresh_token = refresh_token
  @scope = scope || DEFAULT_SCOPE
end

Public Instance Methods

retrieve_oauth2_token() click to toggle source
# File lib/google_refresh.rb, line 33
def retrieve_oauth2_token
  ensure_credentials_are_present
  get_access_token
  create_outh2_client
  return_oauth2_token
end
retrieve_string_token() click to toggle source
# File lib/google_refresh.rb, line 28
def retrieve_string_token
  ensure_credentials_are_present
  get_access_token
end

Private Instance Methods

create_outh2_client() click to toggle source
# File lib/google_refresh.rb, line 78
def create_outh2_client
  @client = OAuth2::Client.new(
    @client_id,
    @client_secret, {
      authorize_url: GOOGLE_AUTHORIZE_URL,
      token_url: GOOGLE_TOKEN_URL
    }
  )

  @client.auth_code.authorize_url(
    {
      scope: @scope,
      redirect_uri: @redirect_uri,
      access_type: ACCESS_TYPE
    }
  )
end
ensure_credentials_are_present() click to toggle source
# File lib/google_refresh.rb, line 42
def ensure_credentials_are_present
  if !@client_id
    raise InvalidCredentialsError, MISSING_CLIENT_ID_ERROR_MESSAGE
  elsif !@client_secret
    raise InvalidCredentialsError, MISSING_CLIENT_SECRET_ERROR_MESSAGE
  elsif !@refresh_token
    raise InvalidCredentialsError, MISSING_REFRESH_TOKEN_ERROR_MESSAGE
  else
    true
  end
end
get_access_token() click to toggle source
# File lib/google_refresh.rb, line 69
def get_access_token
  refresh = HTTParty.post(GOOGLE_TOKEN_URL, options)
  if refresh.code == 200
    @token = refresh.parsed_response['access_token']
  else
    raise AccessTokenRetrievalError, ACCESS_TOKEN_FAILURE_MESSAGE
  end
end
options() click to toggle source
# File lib/google_refresh.rb, line 54
def options
  {
    body: {
      client_id: @client_id,
      client_secret: @client_secret,
      refresh_token: @refresh_token,
      grant_type: GRANT_TYPE
    },
    headers: {
      'Content-Type': CONTENT_TYPE

    }
  }
end
return_oauth2_token() click to toggle source
# File lib/google_refresh.rb, line 96
def return_oauth2_token
  oauth = OAuth2::AccessToken.from_hash @client, { access_token: @token }
  if oauth
    oauth
  else
    raise OauthTokenRetrievalError, OAUTH_TOKEN_FAILURE_MESSAGE
  end
end