Module: Recaptcha::ClientHelper

Defined in:
lib/recaptcha/client_helper.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.recaptcha_components(options = {}) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/recaptcha/client_helper.rb', line 59

def self.recaptcha_components(options = {})
  html = ""
  attributes = {}
  fallback_uri = ""

  # Since leftover options get passed directly through as tag
  # attributes, we must unconditionally delete all our options
  options = options.dup
  env = options.delete(:env)
  class_attribute = options.delete(:class)
  site_key = options.delete(:site_key)
  hl = options.delete(:hl).to_s
  skip_script = (options.delete(:script) == false)
  data_attributes = {}
  [:badge, :theme, :type, :callback, :expired_callback, :size, :tabindex].each do |data_attribute|
    value = options.delete(data_attribute)
    data_attributes["data-#{data_attribute.to_s.tr('_', '-')}"] = value if value
  end

  unless Recaptcha::Verify.skip?(env)
    site_key ||= Recaptcha.configuration.site_key!
    script_url = Recaptcha.configuration.api_server_url
    script_url += "?hl=#{hl}" unless hl == ""
    html << %(<script src="#{script_url}" async defer></script>\n) unless skip_script
    fallback_uri = %(#{script_url.chomp(".js")}/fallback?k=#{site_key})
    attributes["data-sitekey"] = site_key
    attributes.merge! data_attributes
  end

  # Append whatever that's left of options to be attributes on the tag.
  attributes["class"] = "g-recaptcha #{class_attribute}"
  tag_attributes = attributes.merge(options).map { |k, v| %(#{k}="#{v}") }.join(" ")

  [html, tag_attributes, fallback_uri]
end

Instance Method Details

#invisible_recaptcha_tags(options = {}) ⇒ Object

Invisible reCAPTCHA implementation



50
51
52
53
54
55
56
57
# File 'lib/recaptcha/client_helper.rb', line 50

def invisible_recaptcha_tags(options = {})
  options = { :callback => 'invisibleRecaptchaSubmit' }.merge(options)
  text = options.delete(:text)
  html, tag_attributes = Recaptcha::ClientHelper.recaptcha_components(options)
  html << recaptcha_default_callback if recaptcha_default_callback_required?(options)
  html << %(<button type="submit" #{tag_attributes}>#{text}</button>\n)
  html.respond_to?(:html_safe) ? html.html_safe : html
end

#recaptcha_tags(options = {}) ⇒ Object

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



5
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
44
45
46
47
# File 'lib/recaptcha/client_helper.rb', line 5

def recaptcha_tags(options = {})
  if options.key?(:stoken)
    raise(RecaptchaError, "Secure Token is deprecated. Please remove 'stoken' from your calls to recaptcha_tags.")
  end
  if options.key?(:ssl)
    raise(RecaptchaError, "SSL is now always true. Please remove 'ssl' from your calls to recaptcha_tags.")
  end

  noscript = options.delete(:noscript)

  html, tag_attributes, fallback_uri = Recaptcha::ClientHelper.recaptcha_components(options)
  html << %(<div #{tag_attributes}></div>\n)

  if noscript != false
    html << <<-HTML
      <noscript>
        <div>
          <div style="width: 302px; height: 422px; position: relative;">
            <div style="width: 302px; height: 422px; position: absolute;">
              <iframe
                src="#{fallback_uri}"
                frameborder="0" scrolling="no"
                style="width: 302px; height:422px; border-style: none;">
                title="ReCAPTCHA"
              </iframe>
            </div>
          </div>
          <div style="width: 300px; height: 60px; border-style: none;
            bottom: 12px; left: 25px; margin: 0px; padding: 0px; right: 25px;
            background: #f9f9f9; border: 1px solid #c1c1c1; border-radius: 3px;">
            <textarea id="g-recaptcha-response" name="g-recaptcha-response"
              class="g-recaptcha-response"
              style="width: 250px; height: 40px; border: 1px solid #c1c1c1;
              margin: 10px 25px; padding: 0px; resize: none;" value="">
            </textarea>
          </div>
        </div>
      </noscript>
    HTML
  end

  html.respond_to?(:html_safe) ? html.html_safe : html
end