Module: Recaptcha::Helpers

Defined in:
lib/recaptcha/helpers.rb

Constant Summary collapse

DEFAULT_ERRORS =
{
  recaptcha_unreachable: 'Oops, we failed to validate your reCAPTCHA response. Please try again.',
  verification_failed: 'reCAPTCHA verification failed, please try again.'
}.freeze

Class Method Summary collapse

Class Method Details

.invisible_recaptcha_tags(custom) ⇒ Object


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/recaptcha/helpers.rb', line 90

def self.invisible_recaptcha_tags(custom)
  options = {callback: 'invisibleRecaptchaSubmit', ui: :button}.merge(custom)
  text = options.delete(:text)
  html, tag_attributes = components(options.dup)
  html << default_callback(options) if default_callback_required?(options)

  case options[:ui]
  when :button
    html << %(<button type="submit" #{tag_attributes}>#{text}</button>\n)
  when :invisible
    html << %(<div data-size="invisible" #{tag_attributes}></div>\n)
  when :input
    html << %(<input type="submit" #{tag_attributes} value="#{text}"/>\n)
  else
    raise(RecaptchaError, "ReCAPTCHA ui `#{options[:ui]}` is not valid.")
  end
  html.respond_to?(:html_safe) ? html.html_safe : html
end

.recaptcha_execute_method_nameObject


301
302
303
# File 'lib/recaptcha/helpers.rb', line 301

def self.recaptcha_execute_method_name
  Recaptcha.configuration.enterprise ? "grecaptcha.enterprise.execute" : "grecaptcha.execute"
end

.recaptcha_ready_method_nameObject


305
306
307
# File 'lib/recaptcha/helpers.rb', line 305

def self.recaptcha_ready_method_name
  Recaptcha.configuration.enterprise ? "grecaptcha.enterprise.ready" : "grecaptcha.ready"
end

.recaptcha_tags(options) ⇒ Object


47
48
49
50
51
52
53
54
55
56
57
58
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
# File 'lib/recaptcha/helpers.rb', line 47

def self.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 = components(options.dup)
  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}"
                name="ReCAPTCHA"
                style="width: 302px; height: 422px; border-style: none; border: 0; overflow: hidden;">
              </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;">
            </textarea>
          </div>
        </div>
      </noscript>
    HTML
  end

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

.recaptcha_v3(options = {}) ⇒ Object


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
# File 'lib/recaptcha/helpers.rb', line 10

def self.recaptcha_v3(options = {})
  site_key = options[:site_key] ||= Recaptcha.configuration.site_key!
  action = options.delete(:action) || raise(Recaptcha::RecaptchaError, 'action is required')
  id = options.delete(:id) || "g-recaptcha-response-data-#{dasherize_action(action)}"
  name = options.delete(:name) || "g-recaptcha-response-data[#{action}]"
  turbolinks = options.delete(:turbolinks)
  options[:render] = site_key
  options[:script_async] ||= false
  options[:script_defer] ||= false
  element = options.delete(:element)
  element = element == false ? false : :input
  if element == :input
    callback = options.delete(:callback) || recaptcha_v3_default_callback_name(action)
  end
  options[:class] = "g-recaptcha-response #{options[:class]}"

  if turbolinks
    options[:onload] = recaptcha_v3_execute_function_name(action)
  end
  html, tag_attributes = components(options)
  if turbolinks
    html << recaptcha_v3_onload_script(site_key, action, callback, id, options)
  elsif recaptcha_v3_inline_script?(options)
    html << recaptcha_v3_inline_script(site_key, action, callback, id, options)
  end
  case element
  when :input
    html << %(<input type="hidden" name="#{name}" id="#{id}" #{tag_attributes}/>\n)
  when false
    # No tag
    nil
  else
    raise(RecaptchaError, "ReCAPTCHA element `#{options[:element]}` is not valid.")
  end
  html.respond_to?(:html_safe) ? html.html_safe : html
end

.recaptcha_v3_async_execute_function_name(action) ⇒ Object

Returns the name of an async JavaScript function that executes the reCAPTCHA code.


263
264
265
# File 'lib/recaptcha/helpers.rb', line 263

def self.recaptcha_v3_async_execute_function_name(action)
  "#{recaptcha_v3_execute_function_name(action)}Async"
end

.recaptcha_v3_default_callback_name(action) ⇒ Object


267
268
269
# File 'lib/recaptcha/helpers.rb', line 267

def self.recaptcha_v3_default_callback_name(action)
  "setInputWithRecaptchaResponseTokenFor#{sanitize_action_for_js(action)}"
end

.recaptcha_v3_execute_function_name(action) ⇒ Object

Returns the name of the JavaScript function that actually executes the reCAPTCHA code (calls `grecaptcha.execute` or `grecaptcha.enterprise.execute`). You can call it again later to reset it.


258
259
260
# File 'lib/recaptcha/helpers.rb', line 258

def self.recaptcha_v3_execute_function_name(action)
  "executeRecaptchaFor#{sanitize_action_for_js(action)}"
end

.to_error_message(key) ⇒ Object


109
110
111
112
# File 'lib/recaptcha/helpers.rb', line 109

def self.to_error_message(key)
  default = DEFAULT_ERRORS.fetch(key) { raise ArgumentError "Unknown reCAPTCHA error - #{key}" }
  to_message("recaptcha.errors.#{key}", default)
end

.to_message(_key, default) ⇒ Object


115
116
117
# File 'lib/recaptcha/helpers.rb', line 115

def self.to_message(key, default)
  I18n.translate(key, default: default)
end