Sha256: b3719679a75ba8ce48aef5654515f4aab90e90a7d94b698f8c906c52163afdec

Contents?: true

Size: 1.37 KB

Versions: 1

Compression:

Stored size: 1.37 KB

Contents

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

    class << self
      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

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

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

      def google_key
        ENV.fetch("PG_DRIVE_GOOGLE_KEY")
      end

      def google_secret
        ENV.fetch("PG_DRIVE_GOOGLE_SECRET")
      end

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pg_drive-0.2.0 lib/pg_drive/uploader.rb