Module: Recaptcha::ClientHelper

Defined in:
lib/recaptcha/client_helper.rb

Instance Method Summary collapse

Instance Method Details

#recaptcha_tags(options = {}) ⇒ Object

Your public API can be specified in the options hash or preferably using the Configuration.

Raises:



6
7
8
9
10
11
12
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
# File 'lib/recaptcha/client_helper.rb', line 6

def recaptcha_tags(options = {})
  # Default options
  key   = options[:public_key] ||= Recaptcha.configuration.public_key
  raise RecaptchaError, "No public key specified." unless key
  error = options[:error] ||= (defined? flash ? flash[:recaptcha_error] : "")
  uri   = Recaptcha.configuration.api_server_url(options[:ssl])
  html  = ""
  if options[:display]
    html << %{<script type="text/javascript">\n}
    html << %{  var RecaptchaOptions = #{options[:display].to_json};\n}
    html << %{</script>\n}
  end
  if options[:ajax]
    html << %{<script type="text/javascript" src="#{uri}/js/recaptcha_ajax.js"></script>\n}
    html << %{<div id="dynamic_recaptcha"></div>}
    html << %{<script type="text/javascript">\n}
    html << %{setTimeout("Func1()", 100);}
    html << %{  function Func1() { Recaptcha.create('#{key}', document.getElementById('dynamic_recaptcha')#{options[:display] ? ',RecaptchaOptions' : ''});}}
    html << %{</script>\n}  

  else
    html << %{<script type="text/javascript" src="#{uri}/challenge?k=#{key}}
    html << %{#{error ? "&amp;error=#{CGI::escape(error)}" : ""}"></script>\n}
    unless options[:noscript] == false
      html << %{<noscript>\n  }
      html << %{<iframe src="#{uri}/noscript?k=#{key}" }
      html << %{height="#{options[:iframe_height] ||= 300}" }
      html << %{width="#{options[:iframe_width]   ||= 500}" }
      html << %{style="border:none;"></iframe><br/>\n  }
      html << %{<textarea name="recaptcha_challenge_field" }
      html << %{rows="#{options[:textarea_rows] ||= 3}" }
      html << %{cols="#{options[:textarea_cols] ||= 40}"></textarea>\n  }
      html << %{<input type="hidden" name="recaptcha_response_field" value="manual_challenge"/>}
      html << %{</noscript>\n}
    end
  end
  return html.html_safe
end