<commit_before>require 'json'

module Attune
  class DisabledException < Faraday::Error::ClientError
    def initialize(message="Attune library disabled though config")
      super(message)
    end
  end

  class Client
    include Attune::Configurable

    # Initializes a new Client
    #
    # @example
    #   client = Attune::Client.new(
    #     endpoint: "http://example.com:8080/",
    #     timeout:  10
    #   )
    #
    # @param [Hash] options Options for connection (see Attune::Configurable)
    # @returns A new client object
    def initialize(options={})
      Attune::Configurable::KEYS.each do |key|
        send("#{key}=", options[key] || Attune::Default.send(key))
      end
    end

    # Create an anonymous tracked user
    #
    # @example Generate a new id (preferred)
    #   anonymous_id = client.create_anonymous(
    #     user_agent: 'Mozilla/5.0'
    #   )
    # @example Create using an existing id
    #   client.create_anonymous(
    #     id: '0cddbc0-6114-11e3-949a-0800200c9a66',
    #     user_agent: 'Mozilla/5.0'
    #   )
    # @param [Hash] options
    # @option options [String] :id optional. An id will be generated if this is not provided
    # @option options [String] :user_agent The user agent for the application used by the anonymous users
    # @return id [String]
    # @raise [ArgumentError] if user_agent is not provided
    # @raise [Faraday::Error] if the request fails or exceeds the timeout
    def create_anonymous(options)
      raise ArgumentError, "user_agent required" unless options[:user_agent]
      if id = options[:id]
        put("anonymous/#{id}", {user_agent: options[:user_agent]})
        id
      else
        if response = post("anonymous", {user_agent: options[:user_agent]})
          response[:location][/\Aurn:id:([a-z0-9\-]+)\Z/, 1]
        else
          # Return a new UUID if there was an exception and we're in mock mode
          SecureRandom.uuid
        end
      end
    end

    # Returns all entities from the specified collection in order of the user's preference
    #
    # @example
    #   rankings = client.get_rankings(
    #     id: '0cddbc0-6114-11e3-949a-0800200c9a66',
    #     view: 'b/mens-pants',
    #     collection: 'products',
    #     entities: %w[1001, 1002, 1003, 1004]
    #   )
    # @param [Hash] options
    # @option options [String] :id The anonymous user id for whom to grab rankings
    # @option options [String] :view The page or app URN on which the entities will be displayed
    # @option options [String] :collection name of the collection of entities
    # @option options [Array<String>] :entities entities to be ranked. These should be numeric strings or integers.
    # @option options [String] :ip ip address of remote user. Used for geolocation (optional)
    # @option options [String] :customer id of customer (optional)
    # @return ranking [Array<String>] The entities in their ranked order
    # @raise [ArgumentError] if required parameters are missing
    # @raise [Faraday::Error] if the request fails or exceeds the timeout
    def get_rankings(options)
      qs = encoded_ranking_params(options)
      if response = get("rankings/#{qs}", customer: options.fetch(:customer, 'none'))
        JSON.parse(response.body)['ranking']
      else
        # In mock mode: return the entities in the order passed in
        options[:entities]
      end
    end

    # Get multiple rankings in one call
    #
    # @example
    #   rankings = client.get_rankings([
    #     {
    #       id: '0cddbc0-6114-11e3-949a-0800200c9a66',
    #       view: 'b/mens-pants',
    #       collection: 'products',
    #       entities: %w[1001, 1002, 1003, 1004]
    #     },<commit_msg>another documentation typo<commit_after>require 'json'

module Attune
  class DisabledException < Faraday::Error::ClientError
    def initialize(message="Attune library disabled though config")
      super(message)
    end
  end

  class Client
    include Attune::Configurable

    # Initializes a new Client
    #
    # @example
    #   client = Attune::Client.new(
    #     endpoint: "http://example.com:8080/",
    #     timeout:  10
    #   )
    #
    # @param [Hash] options Options for connection (see Attune::Configurable)
    # @returns A new client object
    def initialize(options={})
      Attune::Configurable::KEYS.each do |key|
        send("#{key}=", options[key] || Attune::Default.send(key))
      end
    end

    # Create an anonymous tracked user
    #
    # @example Generate a new id (preferred)
    #   anonymous_id = client.create_anonymous(
    #     user_agent: 'Mozilla/5.0'
    #   )
    # @example Create using an existing id
    #   client.create_anonymous(
    #     id: '0cddbc0-6114-11e3-949a-0800200c9a66',
    #     user_agent: 'Mozilla/5.0'
    #   )
    # @param [Hash] options
    # @option options [String] :id optional. An id will be generated if this is not provided
    # @option options [String] :user_agent The user agent for the application used by the anonymous users
    # @return id [String]
    # @raise [ArgumentError] if user_agent is not provided
    # @raise [Faraday::Error] if the request fails or exceeds the timeout
    def create_anonymous(options)
      raise ArgumentError, "user_agent required" unless options[:user_agent]
      if id = options[:id]
        put("anonymous/#{id}", {user_agent: options[:user_agent]})
        id
      else
        if response = post("anonymous", {user_agent: options[:user_agent]})
          response[:location][/\Aurn:id:([a-z0-9\-]+)\Z/, 1]
        else
          # Return a new UUID if there was an exception and we're in mock mode
          SecureRandom.uuid
        end
      end
    end

    # Returns all entities from the specified collection in order of the user's preference
    #
    # @example
    #   rankings = client.get_rankings(
    #     id: '0cddbc0-6114-11e3-949a-0800200c9a66',
    #     view: 'b/mens-pants',
    #     collection: 'products',
    #     entities: %w[1001, 1002, 1003, 1004]
    #   )
    # @param [Hash] options
    # @option options [String] :id The anonymous user id for whom to grab rankings
    # @option options [String] :view The page or app URN on which the entities will be displayed
    # @option options [String] :collection name of the collection of entities
    # @option options [Array<String>] :entities entities to be ranked. These should be numeric strings or integers.
    # @option options [String] :ip ip address of remote user. Used for geolocation (optional)
    # @option options [String] :customer id of customer (optional)
    # @return ranking [Array<String>] The entities in their ranked order
    # @raise [ArgumentError] if required parameters are missing
    # @raise [Faraday::Error] if the request fails or exceeds the timeout
    def get_rankings(options)
      qs = encoded_ranking_params(options)
      if response = get("rankings/#{qs}", customer: options.fetch(:customer, 'none'))
        JSON.parse(response.body)['ranking']
      else
        # In mock mode: return the entities in the order passed in
        options[:entities]
      end
    end

    # Get multiple rankings in one call
    #
    # @example
    #   rankings = client.multi_get_rankings([
    #     {
    #       id: '0cddbc0-6114-11e3-949a-0800200c9a66',
    #       view: 'b/mens-pants',
    #       collection: 'products',
    #       entities: %w[1001, 1002, 1003, 1004]
    #     },
{'fraction_non_alphanumeric': 0.10622562861368831, 'fraction_numerical': 0.029850746268656716, 'mean_word_length': 2.709725685785536, 'pattern_counts': {'":': 0, '<': 9, '<?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': '470543', 'n_tokens_mistral': 2395, 'n_tokens_neox': 2243, 'n_words': 706}