Class: GoogleSimpleClient::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/google-simple-client/session.rb

Constant Summary collapse

OAUTH_SCOPE =
'https://www.googleapis.com/auth/drive.readonly'
REDIRECT_URI =
'urn:ietf:wg:oauth:2.0:oob'

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Session

Create the object and parse options Options may be provided as paramters or via an init file

./.google-simple-client or ~/.google-simple-client

Parameters:

  • Required (options)

    options are:

    :client_id, :client_secret, :email, and :password
    

    Optional options are:

    :redirect_uri, :scope, and :verbose
    

Raises:



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/google-simple-client/session.rb', line 21

def initialize options = {}
  @options = {
    redirect_uri: REDIRECT_URI,
    scope: OAUTH_SCOPE,
    client_id: nil,
    client_secret: nil,
    email: nil,
    password: nil
  }
  init_file_options = read_options_from_init_file
  @options.merge!(init_file_options)
  @options.merge!(options)
  @verbose = @options.delete(:verbose)
  raise Error.new("Missing option error #{@options.inspect}") unless @options.values.all?
end

Instance Method Details

#authenticateObject

Authenticates with Google Drive



38
39
40
41
42
43
44
45
# File 'lib/google-simple-client/session.rb', line 38

def authenticate
  init_client
  uri = @client.authorization.authorization_uri
  code = scrape_web_and_return_code(uri)
  @client.authorization.code = code
  @client.authorization.fetch_access_token!
  @drive = @client.discovered_api('drive', 'v2')
end

#get(title, format) ⇒ Object

Gets the files matching the title in the requested format

Parameters:

  • The (title)

    name of the requested document

  • The (format)

    format, available formats are: ‘pdf’, ‘html’, ‘csv’, etc.



50
51
52
53
54
55
56
57
58
# File 'lib/google-simple-client/session.rb', line 50

def get(title, format)
  files = find_files(title)
  return "No files found for title #{title}" if files.empty?
  if format
    files.map { |f| download_file(f, format) }
  else
    files.map { |f| f.title }
  end
end