Class: Boar::Providers::GoogleDrive

Inherits:
Base
  • Object
show all
Defined in:
lib/boar/providers/google_drive.rb

Constant Summary collapse

NAME =
"boar"
VERSION =
"1.0.0"
SCOPES =
["https://www.googleapis.com/auth/drive"]

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#update_credentials

Class Method Details

.authorized_client(configuration) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/boar/providers/google_drive.rb', line 22

def self.authorized_client(configuration)
  authorizer = Clavem::Authorizer.new(configuration.fetch(:authorizer_host, "localhost"), configuration.fetch(:authorizer_port, "2501"))

  client = Boar::Providers::GoogleDrive.client(configuration)
  client.authorization.redirect_uri = authorizer.callback_url
  client.authorization.refresh_token = configuration["token"]
  client.authorization.fetch_access_token!

  client
end

.client(configuration) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/boar/providers/google_drive.rb', line 14

def self.client(configuration)
  rv = Google::APIClient.new(authorization: :oauth_2, application_name: Boar::Providers::GoogleDrive::NAME, application_version: Boar::Providers::GoogleDrive::VERSION)
  rv.authorization.client_id = configuration[:client_id]
  rv.authorization.client_secret = configuration[:client_secret]
  rv.authorization.scope = Boar::Providers::GoogleDrive::SCOPES
  rv
end

Instance Method Details

#get_credentials(authorizer, request, response) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/boar/providers/google_drive.rb', line 39

def get_credentials(authorizer, request, response)
  begin
    raise Clavem::Exceptions::AuthorizationDenied if request.query["error"].present? || request.query["code"].blank?

    @client.authorization.code = request.query["code"].to_s
    @client.authorization.fetch_access_token!
    {token: @client.authorization.refresh_token}
  rescue RuntimeError
    nil
  end
end

#redirect_for_authentication(authorizer, configuration) ⇒ Object



33
34
35
36
37
# File 'lib/boar/providers/google_drive.rb', line 33

def redirect_for_authentication(authorizer, configuration)
  @client = Boar::Providers::GoogleDrive.client(configuration)
  @client.authorization.redirect_uri = authorizer.callback_url
  @client.authorization.authorization_uri
end

#search_file(path, params) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/boar/providers/google_drive.rb', line 51

def search_file(path, params)
  rv = nil

  # Split path
  Lazier.load_pathname
  filename = File.basename(path)
  tree = Pathname.new(File.dirname(path)).components

  # Initialize client
  client = Boar::Providers::GoogleDrive.authorized_client(params[:single])
  api = client.discovered_api('drive', 'v2')

  # Find the last folder
  parent = "root"
  tree.each do |folder|
    list = client.execute(api_method: api.children.list, parameters: {"folderId" => parent, "q" => "title='#{folder}'", "maxResults" => 1})
    parent = validate_file_entry(list)
    break if !parent
  end

  # We have the container, query for the file
  if parent then
    list = client.execute(api_method: api.files.list, parameters: {"folderId" => parent, "q" => "title='#{filename}'", "maxResults" => 1})
    rv = validate_file_entry(list)
  end

  rv ? {"id" => rv} : nil
end