Class: Worochi::Agent::GoogleDrive

Inherits:
Worochi::Agent show all
Defined in:
lib/worochi/agent/google_drive.rb

Overview

The Worochi::Agent for Google Drive API.

Instance Attribute Summary

Attributes inherited from Worochi::Agent

#options

Instance Method Summary collapse

Methods inherited from Worochi::Agent

#files, #files_and_folders, #folders, #initialize, #name, new, #push, #push_items, #remove, #set_dir, #set_options, #type

Constructor Details

This class inherits a constructor from Worochi::Agent

Instance Method Details

#clear_cacheObject

Clears the internal resource ID cache



72
73
74
# File 'lib/worochi/agent/google_drive.rb', line 72

def clear_cache
  @id_cache = {}
end

#delete(path) ⇒ Boolean

Deletes the file at ‘path` from Google Drive.

Parameters:

  • path (String)

    path relative to current directory

Returns:

  • (Boolean)

    ‘true` if a file was actually deleted



64
65
66
67
68
69
# File 'lib/worochi/agent/google_drive.rb', line 64

def delete(path)
  abs_path = full_path(path)
  item_id = get_item_id(abs_path)
  return false if item_id.nil?
  delete_by_id(item_id)
end

#init_clientApiClient

Initializes the Google Drive API client.

Returns:

  • (ApiClient)

See Also:



13
14
15
16
17
18
19
20
21
22
# File 'lib/worochi/agent/google_drive.rb', line 13

def init_client
  clear_cache
  disable_write
  @client = Google::APIClient.new(
    application_name: 'Worochi',
    application_version: Worochi::VERSION)
  @client.authorization.access_token = options[:token]
  @drive = @client.discovered_api('drive', 'v2')
  @client
end

#list(path = nil) ⇒ Array<Hash>

Returns a list of files and subdirectories at the remote path specified by ‘options`.

Parameters:

  • path (String) (defaults to: nil)

    path to list instead of the current directory

Returns:

  • (Array<Hash>)

    list of files and subdirectories



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/worochi/agent/google_drive.rb', line 41

def list(path=nil)
  remote_path = list_path(path)
  result, folder_id = navigate_to(remote_path)
  result.map do |elem|
    if elem.mimeType == 'application/vnd.google-apps.folder'
      file_type = 'folder'
    else
      file_type = 'file'
    end
    {
      name: elem.title,
      path: File.join(remote_path, elem.title),
      type: file_type,
      id: elem.id,
      parent_id: folder_id
    }
  end
end

#push_item(item) ⇒ String

Pushes an individual item to Google Drive.

Returns:

  • (String)

    resource ID of the uploaded file.



27
28
29
30
31
32
33
34
# File 'lib/worochi/agent/google_drive.rb', line 27

def push_item(item)
  enable_write
  abs_path = full_path(item.path)
  parent_id = get_parent_id(abs_path)
  id = insert_file(item, parent_id)
  disable_write
  id
end