Class: FileConvert::Drive

Inherits:
Object
  • Object
show all
Defined in:
lib/file_convert/drive.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Drive

Returns a new instance of Drive.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/file_convert/drive.rb', line 8

def initialize(config)
  # Get your credentials from the APIs Console
  client_id = config.client_id
  client_secret = config.client_secret
  oauth_scope = config.oauth_scope
  redirect_uri = config.redirect_uri

  # Create a new API @client & load the Google Drive API
  @client = Google::APIClient.new
  @drive = @client.discovered_api('drive', 'v2')

  # Request authorization
  @client.authorization.client_id = client_id
  @client.authorization.client_secret = client_secret
  @client.authorization.scope = oauth_scope
  @client.authorization.redirect_uri = redirect_uri

  uri = @client.authorization.authorization_uri
  Launchy.open(uri)

  # Exchange authorization code for access token
  $stdout.write 'Enter authorization code: '
  @client.authorization.code = $stdin.gets.chomp
  @client.authorization.fetch_access_token!
end

Instance Method Details

#get_convert_txt_url(file_path) ⇒ Object



34
35
36
37
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
64
# File 'lib/file_convert/drive.rb', line 34

def get_convert_txt_url(file_path)
  file = @drive.files.insert.request_schema.new({
                                                  title: file_path,
                                                  description: 'A test resume document',
                                                })

  mime = MIME::Types.type_for(file_path).first.to_s
  media = Google::APIClient::UploadIO.new(file_path, mime)

  needs_ocr = mime == 'application/pdf'

  result = @client.execute(
    :api_method => @drive.files.insert,
    :body_object => file,
    :media => media,
    :parameters => {
      'uploadType' => 'multipart',
      convert: true,
      ocr: (true if needs_ocr),
      ocrLanguage: (:en if needs_ocr),
      useContentAsIndexableText: (true if needs_ocr),
      'alt' => 'json'}.reject { |k, v| v.nil? })

  file_id = result.data.id

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

  result.data.export_links['text/plain']
end