Class: GoogleDriver::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/google_driver/document.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response, api) ⇒ Document

Returns a new instance of Document.



5
6
7
8
9
10
11
12
# File 'lib/google_driver/document.rb', line 5

def initialize(response, api)
  @api = api
  @drive = get_drive_api

  @response = response
  @file_id = response.data.to_hash['id']
  @exports = response.data.to_hash['exportLinks']
end

Instance Attribute Details

#exportsObject

Returns the value of attribute exports.



3
4
5
# File 'lib/google_driver/document.rb', line 3

def exports
  @exports
end

#file_idObject

Returns the value of attribute file_id.



3
4
5
# File 'lib/google_driver/document.rb', line 3

def file_id
  @file_id
end

#responseObject

Returns the value of attribute response.



3
4
5
# File 'lib/google_driver/document.rb', line 3

def response
  @response
end

Instance Method Details

#download(type) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/google_driver/document.rb', line 34

def download(type)
  loops = 0
  while loops < 5 do
    self.update()
    if @exports and @exports.keys.include?(type)
      return @api.client.execute(uri: @exports[type]).body
    else
      loops+=1
      sleep(5*loops) # Wait an increasing amount of time between loops
    end
  end
end

#get_drive_apiObject



30
31
32
# File 'lib/google_driver/document.rb', line 30

def get_drive_api
  @drive = @api.client.discovered_api('drive', 'v2')
end

#listObject



26
27
28
# File 'lib/google_driver/document.rb', line 26

def list
  @exports.keys
end

#updateObject

refresh document information used when we don’t immediate get exportLinks in the response



16
17
18
19
20
21
22
23
24
# File 'lib/google_driver/document.rb', line 16

def update
  response = @api.client.execute(
      api_method: @drive.files.get,
      :parameters => { 'fileId' => @file_id }
      )
  @response = response
  @file_id = response.data.to_hash['id']
  @exports = response.data.to_hash['exportLinks']
end