Sha256: 1343b89aa706288977365b26b86e9302d9222409d5b5603348f79336d5de1a14

Contents?: true

Size: 1.36 KB

Versions: 1

Compression:

Stored size: 1.36 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(content)
        drive = Drive::DriveService.new
        drive.authorization = credentials
        drive.insert_file(
          Drive::File.new(title: "backup-#{Time.now.to_i}"),
          upload_source: gzip(content),
          content_type: GZIP_MIME_TYPE,
          options: { retries: RETRY_COUNT }
        )
      end

      def gzip(string)
        gzipped_io = StringIO.new
        writer = Zlib::GzipWriter.new(gzipped_io)
        writer.write(string)
        writer.close
        StringIO.new(gzipped_io.string)
      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.1.0 lib/pg_drive/uploader.rb