Module: DriveV3

Defined in:
lib/drive_v3.rb,
lib/drive_v3/version.rb,
lib/drive_v3/create_credential.rb

Overview

Unofficial helpers for the Google Drive V3 API

Defined Under Namespace

Classes: CreateCredential

Constant Summary collapse

VERSION =

The version of this gem

'0.2.1'

Class Method Summary collapse

Class Method Details

.drive_service(credential_source: nil, scopes: nil, credential_creator: DriveV3::CreateCredential) ⇒ Object

Create a new Google::Apis::DriveV3::DriveService object

Simplifies creating and configuring a the credential.

Examples:

using the credential in ~/.google-api-credential

DriveV3.drive_service

using a credential passed in as a string

credential_source = File.read(File.expand_path('~/.google-api-credential.json'))
DriveV3.drive_service(credential_source:)

using a credential passed in as an IO

credential_source = File.open(File.expand_path('~/.google-api-credential.json'))
DriveV3.drive_service(credential_source:)

Parameters:

  • credential_source (nil, String, IO, Google::Auth::*) (defaults to: nil)

    may be either an already constructed credential, the credential read into a String or an open file with the credential ready to be read. Passing nil will result in the credential being read from ~/.google-api-credential.json.

  • scopes (Object, Array) (defaults to: nil)

    one or more scopes to access.

  • credential_creator (#credential) (defaults to: DriveV3::CreateCredential)

    Used to inject the credential creator for testing.

Returns:

  • a new DriveService instance



43
44
45
46
47
48
49
50
# File 'lib/drive_v3.rb', line 43

def drive_service(credential_source: nil, scopes: nil, credential_creator: DriveV3::CreateCredential)
  credential_source ||= File.read(File.expand_path('~/.google-api-credential.json'))
  scopes ||= [Google::Apis::DriveV3::AUTH_DRIVE]

  Google::Apis::DriveV3::DriveService.new.tap do |service|
    service.authorization = credential_creator.call(credential_source, scopes)
  end
end