Class: DriveService

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

Overview

DriveService module implements a service that interfaces with Google Drive using Google Drive API.

Direct Known Subclasses

Gdsh::Gdsh

Instance Method Summary collapse

Constructor Details

#initialize(filename = nil) ⇒ DriveService

Creates a new Google Drive Shell object.

Parameters:

  • filename (String) (defaults to: nil)

    filename of json containing credentials downloaded from Google.



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/gdsh/drive.rb', line 18

def initialize(filename = nil)
  # default to per-file permissions
  @oauth_scope = 'https://www.googleapis.com/auth/drive.file'
  @redirect_uri = 'urn:ietf:wg:oauth:2.0:oob'
  @filename = filename

  if filename && File.exist?(filename)
    credentials_from_file
  else
    credentials_from_stdin
  end
end

Instance Method Details

#authorizeObject



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/gdsh/drive.rb', line 55

def authorize
  init_client

  begin
    authorize_from_refresh_token
  rescue
    puts_refresh_error
    authorize_from_authorization_code
  ensure
    @client.authorization.fetch_access_token!
  end
end

#authorize_from_authorization_codeObject



78
79
80
81
82
83
84
85
# File 'lib/gdsh/drive.rb', line 78

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

  # Exchange authorization code for access token
  print 'Enter authorization code: '.colorize(:light_blue)
  @client.authorization.code = $stdin.gets.chomp
end

#authorize_from_refresh_tokenObject



68
69
70
71
72
73
74
75
76
# File 'lib/gdsh/drive.rb', line 68

def authorize_from_refresh_token
  fail StandardError unless File.exist?('.session.yaml')

  f = File.open('.session.yaml', 'r')
  session = YAML.load(f.read)
  @client.authorization.grant_type = 'refresh_token'
  @client.authorization.refresh_token = session.authorization.refresh_token
  f.close
end

#credentials_from_fileObject



31
32
33
34
35
36
37
38
# File 'lib/gdsh/drive.rb', line 31

def credentials_from_file
  File.open(@filename, 'r') do |f|
    buffer = f.read
    credentials = JSON.parse(buffer)
    @client_id = credentials['installed']['client_id']
    @client_secret = credentials['installed']['client_secret']
  end
end

#credentials_from_stdinObject

Get credentials from shell if no credentials file was specified.



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/gdsh/drive.rb', line 90

def credentials_from_stdin
  # get preset if exists
  @client_id ||= ''
  @client_secret ||= ''

  # Ask from user otherwise
  if @client_id == ''
    print 'Please enter your client id: '
    @client_id = $stdin.gets.chomp
  end

  if @client_secret == ''
    print 'Please enter your client secret: '
    @client_secret = $stdin.gets.chomp
  end
end

#init_clientObject



40
41
42
43
44
45
46
47
48
49
# File 'lib/gdsh/drive.rb', line 40

def init_client
  # Create a new API client & load the Google Drive API
  @client = Google::APIClient.new

  # 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
end

#puts_refresh_errorObject



51
52
53
# File 'lib/gdsh/drive.rb', line 51

def puts_refresh_error
  puts 'Could not refresh token from saved session.'.colorize(:red)
end

#write_session_info_to_fileObject



107
108
109
110
111
112
# File 'lib/gdsh/drive.rb', line 107

def write_session_info_to_file
  return if @client.nil?
  f = File.new('.session.yaml', 'w')
  f.write(@client.to_yaml)
  f.close
end