Skip to content

Instantly share code, notes, and snippets.

@manveru
Created July 17, 2010 10:01
Show Gist options
  • Save manveru/479406 to your computer and use it in GitHub Desktop.
Save manveru/479406 to your computer and use it in GitHub Desktop.
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