Module: EasyDrive::Files

Included in:
Client
Defined in:
lib/easy_drive/files.rb

Instance Method Summary collapse

Instance Method Details

#copy(file_id, folder_id, options = {}) ⇒ Google::APIClient::Schema::Drive::V2::File

Creates a copy of the specified file

Parameters:

  • file_id (String)

    ID of the origin file to copy

  • folder_id (String)

    ID of the destination folder which contains this file

  • options (Hash) (defaults to: {})

    A customizable set of options. @option options [String] :title The title of this file

Returns:



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/easy_drive/files.rb', line 76

def copy(file_id, folder_id, options = {})
  client = self.client
  drive = self.drive

  file = drive.files.copy.request_schema.new({
    'title' => options[:title],
    'parents' => [{:id => folder_id}]
  })

  result = client.execute(
    :api_method => drive.files.copy,
    :body_object => file,
    :parameters => {
      'fileId' => file_id,
      'alt' => 'json'})

  if result.status == 200
    result.data
  else
    puts "An error occurred: #{result.data['error']['message']}"
  end
end

#get(file_id) ⇒ Google::APIClient::Schema::Drive::V2::File

Gets a file’s metadata by ID

Parameters:

  • file_id (String)

    ID of the file to get

Returns:



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/easy_drive/files.rb', line 9

def get(file_id)
  client = self.client
  drive = self.drive

  result = client.execute(
    :api_method => drive.files.get,
    :parameters => {
      'fileId' => file_id,
      'alt' => 'json'})

  if result.status == 200
    result.data
  else
    puts "An error occurred: #{result.data['error']['message']}"
  end
end

#insert(file_name, folder_id, options = {}) ⇒ Google::APIClient::Schema::Drive::V2::File Also known as: upload

Insert(Upload) a new file

Parameters:

  • file_name (String)

    Name of file to upload

  • folder_id (String)

    ID of the destination folder which contains this file

  • options (Hash) (defaults to: {})

    A customizable set of options. @option options [String] :title The title of this file @option options [String] :description The description of this file @option options [String] :mine_type The mine type of this file

Returns:



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/easy_drive/files.rb', line 38

def insert(file_name, folder_id, options = {})
  client = self.client
  drive = self.drive

  file = drive.files.insert.request_schema.new({
    'title' => options[:title],
    'description' => options[:description],
    'mimeType' => options[:mime_type],
    'parents' => [{:id => folder_id}]
  })

  media = Google::APIClient::UploadIO.new(file_name, options[:mime_type])
  result = client.execute(
    :api_method => drive.files.insert,
    :body_object => file,
    :media => media,
    :parameters => {
      'uploadType' => 'multipart',
      'alt' => 'json'})

  if result.status == 200
    return result.data
  else
    puts "An error occurred: #{result.data['error']['message']}"
  end
end