class Paperclip::GoogleDrive::Session

A session for Google Drive operations.

Use from_credentials, from_access_token, from_service_account_key or from_config class method to construct a Paperclip::GoogleDrive::Session object.

Constants

DEFAULT_SCOPE

Public Class Methods

config_from_options(config, options) click to toggle source

@param options [ Hash ] @param config [ Paperclip::GoogleDrive::Config ] @return [ Paperclip::GoogleDrive::Config ]

# File lib/paperclip/google_drive/session.rb, line 75
def config_from_options(config, options)
  if options[:client_id] && options[:client_secret]
    config.client_id = options[:client_id]
    config.client_secret = options[:client_secret]
  end
  config
end
from_config(config_path, options = {}) click to toggle source
# File lib/paperclip/google_drive/session.rb, line 36
def from_config(config_path, options = {})
  validate_options(options)
  config = get_cofiguration(config_path, options)
  credentials = Google::Auth::UserRefreshCredentials.new(
    client_id: config.client_id,
    client_secret: config.client_secret,
    scope: config.scope,
    redirect_uri: 'urn:ietf:wg:oauth:2.0:oob'
  )
  if config.refresh_token
    credentials.refresh_token = config.refresh_token
    credentials.fetch_access_token!
  else
    $stderr.print("\n1. Open this page:\n%s\n\n" % credentials.authorization_uri)
    $stderr.print('2. Enter the authorization code shown in the page: ')
    credentials.code = $stdin.gets.chomp
    credentials.fetch_access_token!
    config.refresh_token = credentials.refresh_token
  end
  config.save
  init_drive_service(options[:application_name], credentials)
end
get_cofiguration(config_path, options) click to toggle source

@param config_path [ String ] @param options [ Hash ] @return [ Paperclip::GoogleDrive::Config ]

# File lib/paperclip/google_drive/session.rb, line 62
def get_cofiguration(config_path, options)
  if config_path.is_a?(String)
    config = Paperclip::GoogleDrive::Config.new(config_path)
  else
    raise(ArgumentError, 'You must set a valid config_path path')
  end
  config.scope ||= DEFAULT_SCOPE
  config_from_options(config, options)
end
init_drive_service(application_name, credentials) click to toggle source

@param application_name [ String ] @param credentials [ Google::Auth::UserRefreshCredentials ] @return [ Google::Apis::DriveV3::DriveService ]

# File lib/paperclip/google_drive/session.rb, line 98
def init_drive_service(application_name, credentials)
  # Initialize the API
  client = Google::Apis::DriveV3::DriveService.new
  client.client_options.application_name = application_name
  client.authorization = credentials
  client
end
invalid_client_options?(options) click to toggle source

@param options [ Hash ] @return [ Boolean ]

# File lib/paperclip/google_drive/session.rb, line 91
def invalid_client_options?(options)
  (options[:client_id] && !options[:client_secret]) || (!options[:client_id] && options[:client_secret])
end
validate_options(options) click to toggle source

@param options [ Hash ]

# File lib/paperclip/google_drive/session.rb, line 84
def validate_options(options)
  raise(ArgumentError, 'You must specify the application_name option') unless options[:application_name]
  raise(ArgumentError, 'client_id and client_secret must be both specified or both omitted') if invalid_client_options?(options)
end