<commit_before>require 'digest/md5'
require 'action_view'

class NegativeCaptcha
  attr_accessor :fields,
    :values,
    :secret,
    :spinner,
    :message,
    :timestamp,
    :error

  TEST_MODE = false
  def self.test_mode=(value)
    const_set("TEST_MODE", value)
  end

  def initialize(opts)
    self.secret = opts[:secret] ||
      Digest::MD5.hexdigest("this_is_a_secret_key")

    if opts.has_key?(:params)
      self.timestamp = opts[:params][:timestamp] || Time.now.to_i
    else
      self.timestamp = Time.now.to_i
    end

    self.spinner = Digest::MD5.hexdigest(
      ([timestamp, secret] + Array(opts[:spinner])).join('-')
    )

    self.message = opts[:message] || <<-MESSAGE
Please try again.
This usually happens because an automated script attempted to submit this form.
    MESSAGE

    self.fields = opts[:fields].inject({}) do |hash, field_name|
      hash[field_name] = TEST_MODE ? "test-#{field_name}" : Digest::MD5.hexdigest(
        [field_name, spinner, secret].join('-')
      )

      hash
    end

    self.values = {}
    self.error = "No params provided"

    if opts[:params] && (opts[:params][:spinner] || opts[:params][:timestamp])
      process(opts[:params])
    end
  end

  def [](name)
    fields[name]
  end

  def valid?
    error.nil? || error == "" || error.empty?
  end

  def process(params)<commit_msg>Changing constant with class variable for cleaner, warning-less code.<commit_after>require 'digest/md5'
require 'action_view'

class NegativeCaptcha
  attr_accessor :fields,
    :values,
    :secret,
    :spinner,
    :message,
    :timestamp,
    :error

  @@test_mode = false
  def self.test_mode=(value)
    class_variable_set(:@@test_mode, value)
  end

  def initialize(opts)
    self.secret = opts[:secret] ||
      Digest::MD5.hexdigest("this_is_a_secret_key")

    if opts.has_key?(:params)
      self.timestamp = opts[:params][:timestamp] || Time.now.to_i
    else
      self.timestamp = Time.now.to_i
    end

    self.spinner = Digest::MD5.hexdigest(
      ([timestamp, secret] + Array(opts[:spinner])).join('-')
    )

    self.message = opts[:message] || <<-MESSAGE
Please try again.
This usually happens because an automated script attempted to submit this form.
    MESSAGE

    self.fields = opts[:fields].inject({}) do |hash, field_name|
      hash[field_name] = @@test_mode ? "test-#{field_name}" : Digest::MD5.hexdigest(
        [field_name, spinner, secret].join('-')
      )

      hash
    end

    self.values = {}
    self.error = "No params provided"

    if opts[:params] && (opts[:params][:spinner] || opts[:params][:timestamp])
      process(opts[:params])
    end
  end

  def [](name)
    fields[name]
  end

  def valid?
    error.nil? || error == "" || error.empty?
  end

  def process(params)
{'fraction_non_alphanumeric': 0.14521926671459381, 'fraction_numerical': 0.002875629043853343, 'mean_word_length': 3.1107828655834564, 'pattern_counts': {'":': 0, '<': 7, '<?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': '6165205', 'n_tokens_mistral': 953, 'n_tokens_neox': 910, 'n_words': 201}