<commit_before>require 'HTTParty'

module Turrialba
  class Client
    include HTTParty
    base_uri ENV['TURRIALBA_URL'] || 'http://localhost:3000'

    def initialize(auth_token = '<PASSWORD>')
      @auth_header = { 'X-AUTH-TOKEN': auth_token }
    end

    def user(uid)
      response = self.class.get("/user/#{uid}", headers: @auth_header)
      User.new(response.parsed_response)
    end

    def put_user(hash)
      uid = hash[:uid] || hash[:id_str]
      response = self.class.put("/user/#{uid}",
                                  body: filter_user_params(hash),
                                  headers: @auth_header)
      User.new(response.parsed_response)
    end

    def post_user_distribution(uid, source, mining_cost, distribution)
      params = {
        source: source,
        mining_cost: mining_cost,
      }
      params[:followers] = distribution[:followers].to_json if !distribution[:followers].nil?
      params[:following] = distribution[:following].to_json if !distribution[:following].nil?
      params[:favorites] = distribution[:favorites].to_json if !distribution[:favorites].nil?
      params[:statuses] = distribution[:statuses].to_json if !distribution[:statuses].nil?
      params[:retweets] = distribution[:retweets].to_json if !distribution[:retweets].nil?

      response = self.class.post("/user/#{uid}/distribution", body: params, headers: @auth_header)
      response.code == 200
    end

    def next_possessed_user
      response = self.class.get("/next_possessed_user", headers: @auth_header)
      User.new(response.parsed_response)
    end

    def tweet(id_str)
      response = self.class.get("/tweet/#{id_str}", headers: @auth_header)
      Tweet.new(response.parsed_response)
    end

    def put_tweet(uid, hash)
      response = self.class.put("/user/#{uid}/tweet/#{hash[:id_str]}",
                                  body: filter_tweet_params(hash),
                                  headers: @auth_header)
      Tweet.new(response.parsed_response)
    end

    def post_tweet_distribution(id_str, source, mining_cost, distribution)
      response = self.class.post("/tweet/#{id_str}/distribution",
                                  body: {
                                    source: source,
                                    mining_cost: mining_cost,
                                    followers: distribution[:followers].to_json,
                                    following: distribution[:following].to_json,
                                    favorites: distribution[:favorites].to_json,
                                    statuses: distribution[:statuses].to_json
                                  },
                                  headers: @auth_header)
      response.code == 200
    end

    def put_favorite(uid, id_str)
      response = self.class.put("/user/#{uid}/favorite/#{id_str}",<commit_msg>next_possessed_user now returns nil if no user was available<commit_after>require 'HTTParty'

module Turrialba
  class Client
    include HTTParty
    base_uri ENV['TURRIALBA_URL'] || 'http://localhost:3000'

    def initialize(auth_token = '<PASSWORD>')
      @auth_header = { 'X-AUTH-TOKEN': auth_token }
    end

    def user(uid)
      response = self.class.get("/user/#{uid}", headers: @auth_header)
      User.new(response.parsed_response)
    end

    def put_user(hash)
      uid = hash[:uid] || hash[:id_str]
      response = self.class.put("/user/#{uid}",
                                  body: filter_user_params(hash),
                                  headers: @auth_header)
      User.new(response.parsed_response)
    end

    def post_user_distribution(uid, source, mining_cost, distribution)
      params = {
        source: source,
        mining_cost: mining_cost,
      }
      params[:followers] = distribution[:followers].to_json if !distribution[:followers].nil?
      params[:following] = distribution[:following].to_json if !distribution[:following].nil?
      params[:favorites] = distribution[:favorites].to_json if !distribution[:favorites].nil?
      params[:statuses] = distribution[:statuses].to_json if !distribution[:statuses].nil?
      params[:retweets] = distribution[:retweets].to_json if !distribution[:retweets].nil?

      response = self.class.post("/user/#{uid}/distribution", body: params, headers: @auth_header)
      response.code == 200
    end

    def next_possessed_user
      response = self.class.get("/next_possessed_user", headers: @auth_header)
      if response.code == 200
        User.new(response.parsed_response)
      else
        nil
      end
    end

    def tweet(id_str)
      response = self.class.get("/tweet/#{id_str}", headers: @auth_header)
      Tweet.new(response.parsed_response)
    end

    def put_tweet(uid, hash)
      response = self.class.put("/user/#{uid}/tweet/#{hash[:id_str]}",
                                  body: filter_tweet_params(hash),
                                  headers: @auth_header)
      Tweet.new(response.parsed_response)
    end

    def post_tweet_distribution(id_str, source, mining_cost, distribution)
      response = self.class.post("/tweet/#{id_str}/distribution",
                                  body: {
                                    source: source,
                                    mining_cost: mining_cost,
                                    followers: distribution[:followers].to_json,
                                    following: distribution[:following].to_json,
                                    favorites: distribution[:favorites].to_json,
                                    statuses: distribution[:statuses].to_json
                                  },
                                  headers: @auth_header)
      response.code == 200
    end

    def put_favorite(uid, id_str)
      response = self.class.put("/user/#{uid}/favorite/#{id_str}",
{'fraction_non_alphanumeric': 0.11518771331058021, 'fraction_numerical': 0.003924914675767918, 'mean_word_length': 2.073413738856843, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 26, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4367332', 'n_tokens_mistral': 1684, 'n_tokens_neox': 1606, 'n_words': 283}