Module: Rack::Recaptcha::Helpers

Defined in:
lib/rack/recaptcha/helpers.rb

Constant Summary collapse

DEFAULT =
{
  :ssl => true
}

Instance Method Summary collapse

Instance Method Details

#recaptcha_javascript(options = {}) ⇒ Object

Helper method to output the recaptcha javascript. Available options:

:language   - Set the language


26
27
28
29
30
31
32
# File 'lib/rack/recaptcha/helpers.rb', line 26

def recaptcha_javascript(options={})
  options = DEFAULT.merge(options)
  path = options[:ssl] ? Rack::Recaptcha::API_SECURE_URL : Rack::Recaptcha::API_URL
  params = ''
  params += "?hl=" + uri_parser.escape(options[:language].to_s) if options[:language]
  %{<script src='#{path + params}'></script>}
end

#recaptcha_valid?Boolean

Helper to return whether the recaptcha was accepted.

Returns:

  • (Boolean)


35
36
37
38
# File 'lib/rack/recaptcha/helpers.rb', line 35

def recaptcha_valid?
  test = Rack::Recaptcha.test_mode
  test.nil? ? request.env['recaptcha.valid'] : test
end

#recaptcha_widget(options = {}) ⇒ Object

Helper method to output a recaptcha widget. Available options:

:public_key - Set the public key. Overrides the key set in Middleware option


15
16
17
18
19
20
# File 'lib/rack/recaptcha/helpers.rb', line 15

def recaptcha_widget(options={})
  options = DEFAULT.merge(options)
  options[:public_key] ||= Rack::Recaptcha.public_key

  %{<div class="g-recaptcha" data-sitekey="#{options[:public_key]}"></div>}.gsub(/^ +/, '')
end