Module: Hcaptcha::Helpers

Defined in:
lib/hcaptcha/helpers.rb

Constant Summary collapse

DEFAULT_ERRORS =
{
  hcaptcha_unreachable: 'Oops, we failed to validate your hCaptcha response. Please try again.',
  verification_failed: 'hCaptcha verification failed, please try again.'
}.freeze
DEFAULT_OPTIONS =
{
  external_script: true,
  script: true,
  script_async: true,
  script_defer: true,
  theme: :dark
}.freeze

Class Method Summary collapse

Class Method Details

.hcaptcha(options) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/hcaptcha/helpers.rb', line 17

def self.hcaptcha(options)
  if options.key?(:stoken)
    raise(HcaptchaError, "Secure Token is deprecated. Please remove 'stoken' from your calls to hcaptcha_tags.")
  end
  if options.key?(:ssl)
    raise(HcaptchaError, "SSL is now always true. Please remove 'ssl' from your calls to hcaptcha_tags.")
  end

  html = generate_tags(options)
  html.respond_to?(:html_safe) ? html.html_safe : html
end

.to_error_message(key) ⇒ Object



29
30
31
32
# File 'lib/hcaptcha/helpers.rb', line 29

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

.to_message(_key, default) ⇒ Object



35
36
37
# File 'lib/hcaptcha/helpers.rb', line 35

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