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
- .invisible_recaptcha_tags(custom) ⇒ Object
- .recaptcha_execute_method_name ⇒ Object
- .recaptcha_ready_method_name ⇒ Object
- .recaptcha_tags(options) ⇒ Object
- .recaptcha_v3(options = {}) ⇒ Object
-
.recaptcha_v3_async_execute_function_name(action) ⇒ Object
Returns the name of an async JavaScript function that executes the reCAPTCHA code.
- .recaptcha_v3_default_callback_name(action) ⇒ Object
-
.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`).
- .to_error_message(key) ⇒ Object
- .to_message(_key, default) ⇒ Object
Class Method Details
.invisible_recaptcha_tags(custom) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/recaptcha/helpers.rb', line 91 def self.(custom) = {callback: 'invisibleRecaptchaSubmit', ui: :button}.merge(custom) text = .delete(:text) html, tag_attributes = components(.dup) html << default_callback() if default_callback_required?() case [: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 `#{[:ui]}` is not valid.") end html.respond_to?(:html_safe) ? html.html_safe : html end |
.recaptcha_execute_method_name ⇒ Object
303 304 305 |
# File 'lib/recaptcha/helpers.rb', line 303 def self.recaptcha_execute_method_name Recaptcha.configuration.enterprise ? "grecaptcha.enterprise.execute" : "grecaptcha.execute" end |
.recaptcha_ready_method_name ⇒ Object
307 308 309 |
# File 'lib/recaptcha/helpers.rb', line 307 def self.recaptcha_ready_method_name Recaptcha.configuration.enterprise ? "grecaptcha.enterprise.ready" : "grecaptcha.ready" end |
.recaptcha_tags(options) ⇒ Object
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 89 |
# File 'lib/recaptcha/helpers.rb', line 48 def self.() if .key?(:stoken) raise(RecaptchaError, "Secure Token is deprecated. Please remove 'stoken' from your calls to recaptcha_tags.") end if .key?(:ssl) raise(RecaptchaError, "SSL is now always true. Please remove 'ssl' from your calls to recaptcha_tags.") end noscript = .delete(:noscript) html, tag_attributes, fallback_uri = components(.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 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 46 |
# File 'lib/recaptcha/helpers.rb', line 10 def self.recaptcha_v3( = {}) site_key = [:site_key] ||= Recaptcha.configuration.site_key! action = .delete(:action) || raise(Recaptcha::RecaptchaError, 'action is required') id = .delete(:id) || "g-recaptcha-response-data-#{dasherize_action(action)}" name = .delete(:name) || "g-recaptcha-response-data[#{action}]" turbo = .delete(:turbo) || .delete(:turbolinks) [:render] = site_key [:script_async] ||= false [:script_defer] ||= false [:ignore_no_element] = .key?(:ignore_no_element) ? [:ignore_no_element] : true element = .delete(:element) element = element == false ? false : :input if element == :input callback = .delete(:callback) || recaptcha_v3_default_callback_name(action) end [:class] = "g-recaptcha-response #{[:class]}" if turbo [:onload] = recaptcha_v3_execute_function_name(action) end html, tag_attributes = components() if turbo html << recaptcha_v3_onload_script(site_key, action, callback, id, ) elsif recaptcha_v3_inline_script?() html << recaptcha_v3_inline_script(site_key, action, callback, id, ) 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 `#{[: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.
265 266 267 |
# File 'lib/recaptcha/helpers.rb', line 265 def self.recaptcha_v3_async_execute_function_name(action) "#{recaptcha_v3_execute_function_name(action)}Async" end |
.recaptcha_v3_default_callback_name(action) ⇒ Object
269 270 271 |
# File 'lib/recaptcha/helpers.rb', line 269 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.
260 261 262 |
# File 'lib/recaptcha/helpers.rb', line 260 def self.recaptcha_v3_execute_function_name(action) "executeRecaptchaFor#{sanitize_action_for_js(action)}" end |
.to_error_message(key) ⇒ Object
110 111 112 113 |
# File 'lib/recaptcha/helpers.rb', line 110 def self.(key) default = DEFAULT_ERRORS.fetch(key) { raise ArgumentError "Unknown reCAPTCHA error - #{key}" } ("recaptcha.errors.#{key}", default) end |
.to_message(_key, default) ⇒ Object
116 117 118 |
# File 'lib/recaptcha/helpers.rb', line 116 def self.(key, default) I18n.translate(key, default: default) end |