Class: Katalyst::GoogleApis::Matchers::ValidateRecaptchaForMatcher

Inherits:
Shoulda::Matchers::ActiveModel::ValidationMatcher
  • Object
show all
Defined in:
lib/katalyst/google_apis/matchers/validate_recaptcha_for_matcher.rb

Instance Method Summary collapse

Constructor Details

#initialize(attribute, expected:) ⇒ ValidateRecaptchaForMatcher

Returns a new instance of ValidateRecaptchaForMatcher.



19
20
21
22
23
# File 'lib/katalyst/google_apis/matchers/validate_recaptcha_for_matcher.rb', line 19

def initialize(attribute, expected:)
  super(attribute)

  @expected_message = expected
end

Instance Method Details

#example_response(subject) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/katalyst/google_apis/matchers/validate_recaptcha_for_matcher.rb', line 47

def example_response(subject)
  case @expected_message
  when :recaptcha_invalid
    { valid?: false }
  when :recaptcha_action_mismatch
    { valid?: true, action: "mismatch" }
  when :recaptcha_suspicious
    { valid?: true, action: subject.class.model_name.param_key, score: 0.1 }
  else
    { valid?: true, action: subject.class.model_name.param_key, score: 0.9 }
  end
end

#example_valueObject



39
40
41
42
43
44
45
# File 'lib/katalyst/google_apis/matchers/validate_recaptcha_for_matcher.rb', line 39

def example_value
  {
    recaptcha_invalid:         "<invalid-token>",
    recaptcha_action_mismatch: "<action-mismatch-token>",
    recaptcha_suspicious:      "<suspicious-token>",
  }[@expected_message]
end

#matches?(subject) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
28
29
30
31
32
33
# File 'lib/katalyst/google_apis/matchers/validate_recaptcha_for_matcher.rb', line 25

def matches?(subject)
  Katalyst::GoogleApis.config.recaptcha.test_mode = false

  super

  disallows_value_of(example_value, @expected_message)
ensure
  Katalyst::GoogleApis.config.recaptcha.test_mode = true
end

#simple_descriptionObject



35
36
37
# File 'lib/katalyst/google_apis/matchers/validate_recaptcha_for_matcher.rb', line 35

def simple_description
  "validate that :#{@attribute} includes reCAPTCHA validation"
end