Module: Ambethia::ReCaptcha::Helper

Defined in:
lib/recaptcha/recaptcha.rb

Instance Method Summary collapse

Instance Method Details

#recaptcha_tags(options = {}) ⇒ Object

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

Raises:



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/recaptcha/recaptcha.rb', line 13

def recaptcha_tags(options = {})
  # Default options
  key   = options[:public_key] ||= ENV['RECAPTCHA_PUBLIC_KEY']
  error = options[:error] ||= session[:recaptcha_error]
  uri   = options[:ssl] ? RECAPTCHA_API_SECURE_SERVER : RECAPTCHA_API_SERVER
  xhtml = Builder::XmlMarkup.new :target => out=(''), :indent => 2 # Because I can.
  if options[:display]
    xhtml.script(:type => "text/javascript"){ |x| x << "var RecaptchaOptions = #{options[:display].to_json};\n"}
  end
  if options[:ajax]
   xhtml.div(:id => 'dynamic_recaptcha') {}
   xhtml.script(:type => "text/javascript", :src => "#{uri}/js/recaptcha_ajax.js") {}
   xhtml.script(:type => "text/javascript") do |x|
     x << "Recaptcha.create('#{key}', document.getElementById('dynamic_recaptcha') );"
   end
  else
    xhtml.script(:type => "text/javascript", :src => :"#{uri}/challenge?k=#{key}&error=#{error}") {}
    unless options[:noscript] == false
      xhtml.noscript do
        xhtml.iframe(:src    => "#{uri}/noscript?k=#{key}",
                     :height => options[:iframe_height] ||= 300,
                     :width  => options[:iframe_width]  ||= 500,
                     :frameborder => 0) {}; xhtml.br
        xhtml.textarea nil, :name => "recaptcha_challenge_field",
                            :rows => options[:textarea_rows] ||= 3,
                            :cols => options[:textarea_cols] ||= 40
        xhtml.input :name => "recaptcha_response_field",
                    :type => "hidden", :value => "manual_challenge"
      end
    end
  end
  raise ReCaptchaError, "No public key specified." unless key
  return out
end