Module: Card::Set::All::Recaptcha

Extended by:
Card::Set
Defined in:
tmpsets/set/mod028-recaptcha/all/recaptcha.rb

Overview

Set: All cards (Recaptcha)

Defined Under Namespace

Modules: HtmlFormat

Constant Summary collapse

RECAPTCHA_ERROR_CODES =

LOCALIZE

{  # LOCALIZE
  "missing-input-secret" =>	"secret parameter is missing",
  "invalid-input-secret" =>	"secret parameter is invalid or malformed",
  "missing-input-response" =>	"response parameter is missing",
  "invalid-input-response" =>	"response parameter is invalid or malformed",
  "bad-request" =>	"request is invalid or malformed"
}

Class Method Summary collapse

Instance Method Summary collapse

Methods included from I18nScope

#mod_name, #scope

Methods included from Loader

#clean_empty_module_from_hash, #clean_empty_modules, #extended, #process_base_modules, #register_set

Methods included from Helpers

#abstract_set?, #all_set?, #num_set_parts, #shortname, #underscore

Methods included from Card::Set::AdvancedApi

#attachment, #ensure_set, #stage_method

Methods included from Format

#before, #format, layout_method_name, #view, view_method_name, view_setting_method_name, wrapper_method_name

Methods included from Inheritance

#include_set, #include_set_formats

Methods included from Basket

#abstract_basket, #add_to_basket, #basket, #unshift_basket

Methods included from Trait

#card_accessor, #card_reader, #card_writer, #require_field

Methods included from Event::Api

#event

Class Method Details

.source_locationObject



7
# File 'tmpsets/set/mod028-recaptcha/all/recaptcha.rb', line 7

def self.source_location; "/Users/ethan/dev/decko/gem/card/mod/recaptcha/set/all/recaptcha.rb"; end

Instance Method Details

#add_recaptcha_errors(error_codes) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'tmpsets/set/mod028-recaptcha/all/recaptcha.rb', line 37

def add_recaptcha_errors error_codes
  if error_codes.present?
    error_codes.each do |code|
      errors.add :recaptcha, RECAPTCHA_ERROR_CODES.fetch(code, code)
    end
  else
    errors.add :recaptcha, "Looks like you are not a human" # LOCALIZE
  end
end

#consider_recaptcha?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'tmpsets/set/mod028-recaptcha/all/recaptcha.rb', line 23

def consider_recaptcha?
  true
end

#handle_recaptcha_config_errorsObject



69
70
71
72
73
74
75
76
77
# File 'tmpsets/set/mod028-recaptcha/all/recaptcha.rb', line 69

def handle_recaptcha_config_errors
  if Env.params[:recaptcha_token] == "grecaptcha-undefined"
    errors.add "recaptcha", "needs correct v3 configuration" # LOCALILZE
  elsif Env.params[:recaptcha_token] == "recaptcha-token-field-missing"
    raise Card::Error, "recaptcha token field missing" # LOCALILZE
  else
    yield
  end
end

#human?Boolean

Returns:

  • (Boolean)


16
17
18
19
20
21
# File 'tmpsets/set/mod028-recaptcha/all/recaptcha.rb', line 16

def human?
  result = JSON.parse recaptcha_response
  return if recaptcha_success?(result)

  add_recaptcha_errors result["error-codes"]
end

#recaptcha_keys?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'tmpsets/set/mod028-recaptcha/all/recaptcha.rb', line 58

def recaptcha_keys?
  Card.config.recaptcha_site_key && Card.config.recaptcha_secret_key
end

#recaptcha_on?Boolean

Returns:

  • (Boolean)


27
28
29
30
31
32
33
34
35
# File 'tmpsets/set/mod028-recaptcha/all/recaptcha.rb', line 27

def recaptcha_on?
  consider_recaptcha?  &&
    recaptcha_keys?    &&
    Env[:controller]   &&
    !Auth.signed_in?   &&
    !Auth.needs_setup? &&
    !Auth.always_ok?   &&
    Card.toggle(rule(:captcha))
end

#recaptcha_responseObject



53
54
55
56
# File 'tmpsets/set/mod028-recaptcha/all/recaptcha.rb', line 53

def recaptcha_response
  ::Recaptcha.get({ secret: Card.config.recaptcha_secret_key,
                    response: Env.params[:recaptcha_token] }, {})
end

#recaptcha_success?(result) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
50
51
# File 'tmpsets/set/mod028-recaptcha/all/recaptcha.rb', line 47

def recaptcha_success? result
  result['success'] &&
    (result['score'].to_f >= Cardio.config.recaptcha_minimum_score) &&
    (result['action'].to_sym == action.to_sym)
end

#validate_recaptcha?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'tmpsets/set/mod028-recaptcha/all/recaptcha.rb', line 80

def validate_recaptcha?
  !@supercard && !Env[:recaptcha_used] && recaptcha_on?
end