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

#list(options = {}) ⇒ Array<Google::APIClient::Schema::Drive::V2::File>

Lists files.

Parameters:

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

    A customizable set of options. @option options [String] :title The title for searching @option options [String] :folder_id The parent folder ID for searching

Returns:



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/easy_drive/files.rb', line 107

def list(options = {})
  client = self.client
  drive = self.drive

  params = {
    "maxResults" => 1000
  }

  if options.has_key?(:title)
    params['q'] = "title = '#{options[:title]}'"
  end

  api_method =
    if options.has_key?(:folder_id)
      params['folderId'] = options[:folder_id]
      drive.children.list
    else
      drive.files.list
    end

  result = []
  page_token = nil
  begin
    if page_token.to_s != ''
      params['pageToken'] = page_token
    end
    api_result = client.execute(
      :api_method => api_method,
      :parameters => params)

    if api_result.status == 200
      files = api_result.data
      result.concat(files.items)
      page_token = files.next_page_token
    else
      puts "An error occurred: #{result.data['error']['message']}"
      page_token = nil
    end
  end while page_token.to_s != ''

  if result.size > 0 && 
    result.first.class.name == 'Google::APIClient::Schema::Drive::V2::ChildReference'

    _result = []
    batch = Google::APIClient::BatchRequest.new
    result.each do |r|
      batch.add(
        :api_method => drive.files.get,
        :parameters => {
          'fileId' => r.id,
          'alt' => 'json'}
      ){|api_result| _result.push(api_result.data) }
    end        
    client.execute(batch)
    result = _result
  end

  result
end