Class: Clian::Authorizer

Inherits:
Object
  • Object
show all
Defined in:
lib/clian/authorizer.rb

Overview

Instance Method Summary collapse

Constructor Details

#initialize(client_id, client_secret, scope, token_store_path) ⇒ Authorizer

Returns a new instance of Authorizer.



12
13
14
15
16
17
18
# File 'lib/clian/authorizer.rb', line 12

def initialize(client_id, client_secret, scope, token_store_path)
  @authorizer = Google::Auth::UserAuthorizer.new(
    Google::Auth::ClientId.new(client_id, client_secret),
    scope,
    Google::Auth::Stores::FileTokenStore.new(file: token_store_path)
  )
end

Instance Method Details

#auth_interactively(user_id = "default", shell = Thor.new.shell) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/clian/authorizer.rb', line 24

def auth_interactively(user_id = "default", shell = Thor.new.shell)
  callback_uri = "http://localhost:8000"

  url = @authorizer.get_authorization_url(base_url: callback_uri)
  begin
    Launchy.open(url)
  rescue
    puts "Open URL in your browser:\n #{url}"
  end

  puts "Waiting OAuth code at #{callback_uri} ..."

  code = get_oauth_code_by_webserver(8000) ||
         shell.ask("Enter the resulting code:")
  puts "Done."

  @authorizer.get_and_store_credentials_from_code(
    user_id:  user_id,
    code:     code,
    base_url: callback_uri
  )
end

#credentials(user_id = "default") ⇒ Object



20
21
22
# File 'lib/clian/authorizer.rb', line 20

def credentials(user_id = "default")
  @authorizer.get_credentials(user_id)
end