Created
July 17, 2010 10:01
-
-
Save manveru/479406 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Ramaze | |
module Helper | |
module ReCaptcha | |
# Call this to generate the actual ReCaptcha script into your template. | |
# Options can include | |
# [rcc_pub] public recaptcha key (defaults to RCC_PUB constant) | |
# [rcc_priv] privte recaptcha key (defaults to RCC_PRIV constant) | |
# [ssl] generate ssl-based output, defaults to false | |
# | |
# This method also sets :rcc_err into the session. | |
# Example (rcc_pub and rcc_private not required if RCC_PUB & RCC_PRIV constants are used): | |
# = get_captcha(:rcc_pub => 'foobar', :rcc_priv => 'blegga', :ssl => true) | |
# | |
def recaptcha_get(options = {}) | |
client = ::ReCaptcha::Client.new( | |
options[:rcc_pub] || RCC_PUB, | |
options[:rcc_priv] || RCC_PRIV, | |
options[:ssl] | |
) | |
result = client.get_challenge(session[:rcc_err] || '', options) | |
session[:rcc_err] = '' | |
result | |
end | |
# Validate recaptcha from passed in params. Sets errors into the errors | |
# hash. | |
# | |
# [errors] errors hash-like thing. | |
# [options] Options hash. currently only uses :rcc_pub and :rcc_priv | |
# options for passing in ReCaptcha keys. | |
def recaptcha_validate(errors, options = {}) | |
client = ::ReCaptcha::Client.new( | |
options[:rcc_pub] || RCC_PUB, | |
options[:rcc_priv] || RCC_PRIV | |
) | |
challenge = request[:recaptcha_challenge_field] | |
response = request[:recaptcha_response_field] | |
result = client.validate(request.ip, challenge, response, errors) | |
session[:rcc_err] = client.last_error | |
result | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment