<commit_before>module Recaptcha
  module Verify
    # Your private API can be specified in the +options+ hash or preferably
    # the environment variable +RECAPTCHA_PUBLIC_KEY+.
    def verify_recaptcha(options = {})
      return true if SKIP_VERIFY_ENV.include? ENV['RAILS_ENV']
      model = options.is_a?(Hash)? options[:model] : options
      private_key = options[:private_key] if options.is_a?(Hash)
      private_key ||= ENV['RECAPTCHA_PRIVATE_KEY']
      raise RecaptchaError, "No private key specified." unless private_key
      begin
        recaptcha = nil
        Timeout::timeout(options[:timeout] || 3) do
          recaptcha = Net::HTTP.post_form URI.parse("http://#{RECAPTCHA_VERIFY_SERVER}/verify"), {
            "privatekey" => private_key,
            "remoteip"   => request.remote_ip,
            "challenge"  => params[:recaptcha_challenge_field],
            "response"   => params[:recaptcha_response_field]
          }
        end
        answer, error = recaptcha.body.split.map { |s| s.chomp }
        unless answer == 'true'
          flash[:recaptcha_error] = error
          if model
            model.valid?
            model.errors.add :captcha, options[:message] || "Word verification response is incorrect, please try again."
          end
          return false
        else
          flash[:recaptcha_error] = nil
          return true
        end
      rescue Timeout::Error 
        flash[:recaptcha_error] = "recaptcha-not-reachable"
        if model
          model.valid?
          model.errors.add :base, options[:message] || "Oops, we failed to validate your Captcha. Please try again."
        end
        return false
      rescue Exception => e
        raise RecaptchaError, e.message, e.backtrace
      end
    end # verify_recaptcha
  end # Verify
end # Recaptcha<commit_msg>More sensible defaults for error handling and messaging<commit_after>module Recaptcha
  module Verify
    # Your private API can be specified in the +options+ hash or preferably
    # the environment variable +RECAPTCHA_PUBLIC_KEY+.
    def verify_recaptcha(options = {})
      return true if SKIP_VERIFY_ENV.include? ENV['RAILS_ENV']
      model = options.is_a?(Hash)? options[:model] : options
      private_key = options[:private_key] if options.is_a?(Hash)
      private_key ||= ENV['RECAPTCHA_PRIVATE_KEY']
      raise RecaptchaError, "No private key specified." unless private_key
      begin
        recaptcha = nil
        Timeout::timeout(options[:timeout] || 3) do
          recaptcha = Net::HTTP.post_form URI.parse("http://#{RECAPTCHA_VERIFY_SERVER}/verify"), {
            "privatekey" => private_key,
            "remoteip"   => request.remote_ip,
            "challenge"  => params[:recaptcha_challenge_field],
            "response"   => params[:recaptcha_response_field]
          }
        end
        answer, error = recaptcha.body.split.map { |s| s.chomp }
        unless answer == 'true'
          flash[:recaptcha_error] = error
          if model
            model.valid?
            model.errors.add :base, options[:message] || "Word verification response is incorrect, please try again."
          end
          return false
        else
          flash[:recaptcha_error] = nil
          return true
        end
      rescue Timeout::Error 
        flash[:recaptcha_error] = "recaptcha-not-reachable"
        if model
          model.valid?
          model.errors.add :base, options[:message] || "Oops, we failed to validate your word verification response. Please try again."
        end
        return false
      rescue Exception => e
        raise RecaptchaError, e.message, e.backtrace
      end
    end # verify_recaptcha
  end # Verify
end # Recaptcha
{'fraction_non_alphanumeric': 0.09546925566343042, 'fraction_numerical': 0.0005393743257820927, 'mean_word_length': 2.4152854511970534, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '3271828', 'n_tokens_mistral': 1068, 'n_tokens_neox': 1053, 'n_words': 274}