<commit_before>require 'active_model/naming'
require 'active_model/translation'
require 'active_model/validator'
require 'active_model/validations'

module Skimlinks
  class ProductSearch
    extend ActiveModel::Naming
    extend ActiveModel::Translation

    include ActiveModel::Validations

    LOCALES = %w(
      us
      uk
    )
    REQUIRED_PARAMS = %w(
      ids
      query
      locale
      min_price
      max_price
      merchant_id
      category
    )

    attr_accessor :ids, :query, :category, :page, :locale, :merchant_id, :min_price, :max_price, :rows

    validates :page, :presence => true, :numericality => true
    validates :locale, :presence => true, :inclusion => LOCALES
    validates :rows, :presence => true, :numericality => true

    validate do
      self.errors.add :base, "One of these params must be set: #{REQUIRED_PARAMS.join(', ')}" if REQUIRED_PARAMS.all? { |param| instance_variable_get("@#{param}").blank? }
    end

    def initialize(args = {})
      args.each do |k, v|
        self.send "#{k}=", v
      end

      @client = Skimlinks::Client.new
    end

    class << self
      def category_list
        Hash[
          Skimlinks::Client.new.product_categories.
            invert.
            sort.
            map { |category, id| [category, id.to_i] }
        ]
      end

      def category_hash
        {}.tap do |all_categories|
          self.category_list.each do |category, id|
            category_hash = category.split(' > ').reverse.inject({}) do |hash, category_part|
              { category_part => hash.presence }
            end
            all_categories.deep_merge! category_hash
          end
        end
      end
    end

    def products
      category_ids = if @category.present?
        self.class.category_list.select { |category, id| category =~ /^#{Regexp.escape(@category)}/ }.values.tap do |c_ids|
          raise StandardError, "No category IDs for category #{@category} found" if c_ids.empty?<commit_msg>Revert "More Ruby 1.8 syntax for multiline chaining"<commit_after>require 'active_model/naming'
require 'active_model/translation'
require 'active_model/validator'
require 'active_model/validations'

module Skimlinks
  class ProductSearch
    extend ActiveModel::Naming
    extend ActiveModel::Translation

    include ActiveModel::Validations

    LOCALES = %w(
      us
      uk
    )
    REQUIRED_PARAMS = %w(
      ids
      query
      locale
      min_price
      max_price
      merchant_id
      category
    )

    attr_accessor :ids, :query, :category, :page, :locale, :merchant_id, :min_price, :max_price, :rows

    validates :page, :presence => true, :numericality => true
    validates :locale, :presence => true, :inclusion => LOCALES
    validates :rows, :presence => true, :numericality => true

    validate do
      self.errors.add :base, "One of these params must be set: #{REQUIRED_PARAMS.join(', ')}" if REQUIRED_PARAMS.all? { |param| instance_variable_get("@#{param}").blank? }
    end

    def initialize(args = {})
      args.each do |k, v|
        self.send "#{k}=", v
      end

      @client = Skimlinks::Client.new
    end

    class << self
      def category_list
        Hash[
          Skimlinks::Client.new.product_categories
            .invert
            .sort
            .map { |category, id| [category, id.to_i] }
        ]
      end

      def category_hash
        {}.tap do |all_categories|
          self.category_list.each do |category, id|
            category_hash = category.split(' > ').reverse.inject({}) do |hash, category_part|
              { category_part => hash.presence }
            end
            all_categories.deep_merge! category_hash
          end
        end
      end
    end

    def products
      category_ids = if @category.present?
        self.class.category_list.select { |category, id| category =~ /^#{Regexp.escape(@category)}/ }.values.tap do |c_ids|
          raise StandardError, "No category IDs for category #{@category} found" if c_ids.empty?
{'fraction_non_alphanumeric': 0.10905914649363614, 'fraction_numerical': 0.0004991265285749938, 'mean_word_length': 2.4227156276686594, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 19, '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': '2466953', 'n_tokens_mistral': 1238, 'n_tokens_neox': 1217, 'n_words': 308}