<commit_before>require 'twitter'

module SocialLogin
  class TwitterService < Service

    def self.init_with(auth_token={})
      request = create_connection(auth_token)

      return create_with_request(
        request.user.id,
        User.create_with_twitter_request(request.user),
        "Authenticated",
        {access_token: request.access_token, access_token_secret: request.access_token_secret}
      )

    rescue Twitter::Error::Unauthorized => e
      raise InvalidToken.new(e.message)
    end

    def self.connect_with(user, auth_token={})
      request = create_connection(auth_token)

      return create_with_request(
        request.user.id,
        user,
        "Connected",
        {access_token: request.access_token, access_token_secret: request.access_token_secret}
      )

    rescue Twitter::Error::Unauthorized => e
      raise InvalidToken.new(e.message)
    end

    def self.create_connection(auth_token={})
      Twitter::REST::Client.new do |config|
        config.consumer_key        = SocialLogin.twitter_consumer_key
        config.consumer_secret     = SocialLogin.twitter_consumer_secret
        config.access_token        = auth_token[:access_token]
        config.access_token_secret = auth_token[:access_token_secret]
      end
    end

    def friend_ids
      if redis_instance.exists(redis_key(:friends))
        friend_ids = redis_instance.smembers(redis_key(:friends))
      else
        friend_ids = self.class.create_connection(access_token).friend_ids.to_hash[:ids]
        unless friend_ids.empty?
          redis_instance.del(redis_key(:friends))
          redis_instance.sadd(redis_key(:friends), friend_ids.to_s)<commit_msg>Twitter friend_ids returns an array of ints but we store all remote_ids as strings so needed to perform a conversion<commit_after>require 'twitter'

module SocialLogin
  class TwitterService < Service

    def self.init_with(auth_token={})
      request = create_connection(auth_token)

      return create_with_request(
        request.user.id,
        User.create_with_twitter_request(request.user),
        "Authenticated",
        {access_token: request.access_token, access_token_secret: request.access_token_secret}
      )

    rescue Twitter::Error::Unauthorized => e
      raise InvalidToken.new(e.message)
    end

    def self.connect_with(user, auth_token={})
      request = create_connection(auth_token)

      return create_with_request(
        request.user.id,
        user,
        "Connected",
        {access_token: request.access_token, access_token_secret: request.access_token_secret}
      )

    rescue Twitter::Error::Unauthorized => e
      raise InvalidToken.new(e.message)
    end

    def self.create_connection(auth_token={})
      Twitter::REST::Client.new do |config|
        config.consumer_key        = SocialLogin.twitter_consumer_key
        config.consumer_secret     = SocialLogin.twitter_consumer_secret
        config.access_token        = auth_token[:access_token]
        config.access_token_secret = auth_token[:access_token_secret]
      end
    end

    def friend_ids
      if redis_instance.exists(redis_key(:friends))
        friend_ids = redis_instance.smembers(redis_key(:friends))
      else
        friend_ids = self.class.create_connection(access_token).friend_ids.to_hash[:ids].map(&:to_s)
        unless friend_ids.empty?
          redis_instance.del(redis_key(:friends))
          redis_instance.sadd(redis_key(:friends), friend_ids.to_s)
{'fraction_non_alphanumeric': 0.08719424460431655, 'fraction_numerical': 0.0, 'mean_word_length': 3.254589963280294, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '6259673', 'n_tokens_mistral': 1041, 'n_tokens_neox': 1008, 'n_words': 168}