<commit_before>require 'oauth/request_proxy/base'

module Smugsyncv2
  class Client
    TOKEN_FILE = '/tmp/.token_cache'

    def initialize(key, secret, user_token, user_secret, logger = false)
      @uris = nil
      @key = key
      @secret = secret
      @user_token = user_token
      @user_secret = user_secret
      @logger = logger
    end

    def oauth_opts
      { site: OAUTH_ORIGIN,
        request_token_path: REQUEST_TOKEN_PATH,
        access_token_path: ACCESS_TOKEN_PATH,
        authorize_path: AUTHORIZE_PATH
      }
    end

    def login # rubocop:disable Metrics/MethodLength
      @consumer = OAuth::Consumer.new(@key, @secret, oauth_opts)
      if File.exist?(TOKEN_FILE)
        @access_token = load_cached_token 
        #overload access_token  with user_token and user_secret
        if (!@user_token.blank?) 
          @access_token.token = @user_token 
        end
        if (!@user_secret.blank?) 
          @access_token.secret = @user_secret 
        end        
        return @access_token
      end

      @request_token = @consumer.get_request_token
      authorize_url = @request_token.authorize_url + '&Access=Full'
      puts "Open a web browser and open: #{authorize_url}"
      puts 'Enter the validation code: '
      verification_code = STDIN.gets.chomp
      @access_token = @request_token.get_access_token(
      oauth_verifier: verification_code)
      cache_token(@access_token)
      @access_token
    end

    def access_token
      @access_token ||= login
    end

    def consumer
      if @consumer
        @consumer
      else
        login
        @consumer
      end
    end

    def load_cached_token<commit_msg>blank? is a rails extension. Use empty? instead<commit_after>require 'oauth/request_proxy/base'

module Smugsyncv2
  class Client
    TOKEN_FILE = '/tmp/.token_cache'

    def initialize(key, secret, user_token, user_secret, logger = false)
      @uris = nil
      @key = key
      @secret = secret
      @user_token = user_token
      @user_secret = user_secret
      @logger = logger
    end

    def oauth_opts
      { site: OAUTH_ORIGIN,
        request_token_path: REQUEST_TOKEN_PATH,
        access_token_path: ACCESS_TOKEN_PATH,
        authorize_path: AUTHORIZE_PATH
      }
    end

    def login # rubocop:disable Metrics/MethodLength
      @consumer = OAuth::Consumer.new(@key, @secret, oauth_opts)
      if File.exist?(TOKEN_FILE)
        @access_token = load_cached_token 
        #overload access_token  with user_token and user_secret
        if (!@user_token.empty?) 
          @access_token.token = @user_token 
        end
        if (!@user_secret.empty?) 
          @access_token.secret = @user_secret 
        end        
        return @access_token
      end

      @request_token = @consumer.get_request_token
      authorize_url = @request_token.authorize_url + '&Access=Full'
      puts "Open a web browser and open: #{authorize_url}"
      puts 'Enter the validation code: '
      verification_code = STDIN.gets.chomp
      @access_token = @request_token.get_access_token(
      oauth_verifier: verification_code)
      cache_token(@access_token)
      @access_token
    end

    def access_token
      @access_token ||= login
    end

    def consumer
      if @consumer
        @consumer
      else
        login
        @consumer
      end
    end

    def load_cached_token
{'fraction_non_alphanumeric': 0.07498518079430942, 'fraction_numerical': 0.0005927682276229994, 'mean_word_length': 2.3987915407854983, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1171728', 'n_tokens_mistral': 1064, 'n_tokens_neox': 1034, 'n_words': 234}