Module: Locabulary::JsonCreator::GoogleAccessTokenFetcher

Defined in:
lib/locabulary/json_creator.rb

Overview

:nocov: Responsible for building credentials from code

Constant Summary collapse

OOB_URI =
"urn:ietf:wg:oauth:2.0:oob".freeze
READ_ONLY_SCOPE =
"https://www.googleapis.com/auth/drive.readonly".freeze

Class Method Summary collapse

Class Method Details

.callObject



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/locabulary/json_creator.rb', line 66

def self.call
  # This looks a bit funny in that we can cache the tokens that are returned. However I don't want to do that.
  # So instead, I'm adding a symbol that should barf if the underlying interface changes.
  token_store = :token_store

  client_id = Google::Auth::ClientId.new(client_secrets.fetch('client_id'), client_secrets.fetch('client_secret'))
  authorizer = Google::Auth::UserAuthorizer.new(client_id, READ_ONLY_SCOPE, token_store)
  authorization_url = authorizer.get_authorization_url(base_url: OOB_URI)
  puts "\n Open the following URL, login with your credentials and get the authorization code \n\n #{authorization_url}\n\n"
  authorization_code = ask('Authorization Code: ')
  authorizer.get_credentials_from_code(base_url: OOB_URI, code: authorization_code)
end

.client_secretsObject



79
80
81
# File 'lib/locabulary/json_creator.rb', line 79

def self.client_secrets
  @secrets ||= YAML.load(File.open(File.join(secrets_path)))
end

.secrets_pathObject



83
84
85
86
87
88
89
# File 'lib/locabulary/json_creator.rb', line 83

def self.secrets_path
  if File.exist? File.join(File.dirname(__FILE__), '../../config/client_secrets.yml')
    File.join(File.dirname(__FILE__), '../../config/client_secrets.yml')
  else
    File.join(File.dirname(__FILE__), '../../config/client_secrets.example.yml')
  end
end