Module: PgDrive::Uploader

Defined in:
lib/pg_drive/uploader.rb

Constant Summary collapse

Drive =
Google::Apis::DriveV2
AUTH_SCOPE =
"https://www.googleapis.com/auth/drive".freeze
RETRY_COUNT =
3

Class Method Summary collapse

Class Method Details

.authorizerObject



20
21
22
23
24
25
26
# File 'lib/pg_drive/uploader.rb', line 20

def authorizer
  Google::Auth::UserAuthorizer.new(
    Uploader.client_id,
    Uploader::AUTH_SCOPE,
    nil
  )
end

.call(pipe) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/pg_drive/uploader.rb', line 8

def call(pipe)
  drive = Drive::DriveService.new
  drive.authorization = credentials
  app_name = Rails.application.class.parent_name
  drive.insert_file(
    Drive::File.new(title: "#{app_name}-#{Time.now.utc.iso8601}.dmp"),
    upload_source: pipe,
    content_type: BINARY_MIME_TYPE,
    options: { retries: RETRY_COUNT }
  )
end

.client_idObject



28
29
30
# File 'lib/pg_drive/uploader.rb', line 28

def client_id
  Google::Auth::ClientId.new(google_key, google_secret)
end

.credentialsObject



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/pg_drive/uploader.rb', line 40

def credentials
  refresh_token = ENV["PG_DRIVE_CREDENTIALS"]
  if refresh_token.nil? || refresh_token.empty?
    raise InvalidEnvironment, MISSING_CRED_WARNING
  end
  Google::Auth::UserRefreshCredentials.new(
    client_id: google_key,
    client_secret: google_secret,
    refresh_token: refresh_token,
    scope: AUTH_SCOPE
  )
end

.google_keyObject



32
33
34
# File 'lib/pg_drive/uploader.rb', line 32

def google_key
  ENV.fetch("PG_DRIVE_GOOGLE_KEY")
end

.google_secretObject



36
37
38
# File 'lib/pg_drive/uploader.rb', line 36

def google_secret
  ENV.fetch("PG_DRIVE_GOOGLE_SECRET")
end