Module Recaptcha::Verify
In: lib/recaptcha/verify.rb

Methods

Public Instance methods

Your private API can be specified in the options hash or preferably the environment variable RECAPTCHA_PUBLIC_KEY.

[Source]

    # File lib/recaptcha/verify.rb, line 5
 5:     def verify_recaptcha(options = {})
 6:       return true if SKIP_VERIFY_ENV.include? ENV['RAILS_ENV']
 7:       model = options.is_a?(Hash)? options[:model] : options
 8:       private_key = options[:private_key] if options.is_a?(Hash)
 9:       private_key ||= ENV['RECAPTCHA_PRIVATE_KEY']
10:       raise RecaptchaError, "No private key specified." unless private_key
11:       begin
12:         recaptcha = nil
13:         Timeout::timeout(options[:timeout] || 3) do
14:           recaptcha = Net::HTTP.post_form URI.parse("http://#{RECAPTCHA_VERIFY_SERVER}/verify"), {
15:             "privatekey" => private_key,
16:             "remoteip"   => request.remote_ip,
17:             "challenge"  => params[:recaptcha_challenge_field],
18:             "response"   => params[:recaptcha_response_field]
19:           }
20:         end
21:         answer, error = recaptcha.body.split.map { |s| s.chomp }
22:         unless answer == 'true'
23:           session[:recaptcha_error] = error
24:           if model
25:             model.valid?
26:             model.errors.add :base, options[:message] || "Captcha response is incorrect, please try again."
27:           end
28:           return false
29:         else
30:           session[:recaptcha_error] = nil
31:           return true
32:         end
33:       rescue Timeout::Error 
34:         session[:recaptcha_error] = "recaptcha-not-reachable"
35:         if model
36:           model.valid?
37:           model.errors.add :base, options[:message] || "Oops, we failed to validate your Captcha. Please try again."
38:         end
39:         return false
40:       rescue Exception => e
41:         raise RecaptchaError, e.message, e.backtrace
42:       end
43:     end

[Validate]