<commit_before>require "pg_drive/version"
require "google/apis/drive_v2"
require "googleauth"
require "pg_drive/uploader"
require "pg_drive/dump"

module PgDrive
  InvalidEnvironment = Class.new(StandardError)
  BackupFailed = Class.new(StandardError)

  OOB_URI = "urn:ietf:wg:oauth:2.0:oob".freeze
  DEFAULT_BACKUP_TIMEOUT_SECONDS = 60 * 5 # 5 minutes
  MISSING_CRED_WARNING = "Please use the run #{self}.setup_credentials"\
    " from console to set up credentials".freeze
  CREDENTIALS_INTRO = "Please open your browser and go to the following url."\
    "Login with the user you wish to use as target for backup".freeze
  CREDENTIALS_ENV_INSTRUCTIONS = "Please set the following line as the value of "\
  '"PG_DRIVE_CREDENTIALS" key in the environment hash:'.freeze

  BACKUP_CMD = "pg_dump -c -C -b".freeze
  PG_ENV_MAP = {
    "PGPASSWORD" => "password",
    "PGDATABASE" => "database",
    "PGHOST" => "host",
    "PGPORT" => "port",
    "PGUSER" => "username",
  }.freeze
  GZIP_MIME_TYPE = "application/x-gzip".freeze

  class << self
    def perform
      Uploader.call(Dump.call)
    end

    def setup_credentials
      puts CREDENTIALS_INTRO
      puts authorizer.get_authorization_url(base_url: OOB_URI)
      puts "Please enter the token you receive for further instructions"
      code = gets
      puts CREDENTIALS_ENV_INSTRUCTIONS
      puts refresh_token_from_code(code)
    end

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

    def refresh_token_from_code(code)
      authorizer.get_credentials_from_code(
        user_id: :owner,
        code: code,
        base_url: OOB_URI
      )
    end
  end
end<commit_msg>Fix bug of not showing correct refresh token<commit_after>require "pg_drive/version"
require "google/apis/drive_v2"
require "googleauth"
require "pg_drive/uploader"
require "pg_drive/dump"

module PgDrive
  InvalidEnvironment = Class.new(StandardError)
  BackupFailed = Class.new(StandardError)

  OOB_URI = "urn:ietf:wg:oauth:2.0:oob".freeze
  DEFAULT_BACKUP_TIMEOUT_SECONDS = 60 * 5 # 5 minutes
  MISSING_CRED_WARNING = "Please use the run #{self}.setup_credentials"\
    " from console to set up credentials".freeze
  CREDENTIALS_INTRO = "Please open your browser and go to the following url."\
    "Login with the user you wish to use as target for backup".freeze
  CREDENTIALS_ENV_INSTRUCTIONS = "Please set the following line as the value of "\
  '"PG_DRIVE_CREDENTIALS" key in the environment hash:'.freeze

  BACKUP_CMD = "pg_dump -c -C -b".freeze
  PG_ENV_MAP = {
    "PGPASSWORD" => "password",
    "PGDATABASE" => "database",
    "PGHOST" => "host",
    "PGPORT" => "port",
    "PGUSER" => "username",
  }.freeze
  GZIP_MIME_TYPE = "application/x-gzip".freeze

  class << self
    def perform
      Uploader.call(Dump.call)
    end

    def setup_credentials
      puts CREDENTIALS_INTRO
      puts authorizer.get_authorization_url(base_url: OOB_URI)
      puts "Please enter the token you receive for further instructions"
      code = gets
      puts CREDENTIALS_ENV_INSTRUCTIONS
      puts refresh_token_from_code(code)
    end

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

    def refresh_token_from_code(code)
      authorizer.get_credentials_from_code(
        user_id: :owner,
        code: code,
        base_url: OOB_URI
      ).refresh_token
    end
  end
end
{'fraction_non_alphanumeric': 0.09013363662212112, 'fraction_numerical': 0.00398066533977822, 'mean_word_length': 3.2954822954822953, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6224645', 'n_tokens_mistral': 1211, 'n_tokens_neox': 1180, 'n_words': 291}