Class: Recaptcha::Verify::VerifyResult

Inherits:
Object
  • Object
show all
Defined in:
lib/recaptcha/verify/verify_result.rb

Overview

Represents the result of us verifying that the verify response is valid against our Configuration.

This is mostly a wrapper for VerifyResponse but depends also upon comparing it to our configured hostname, etc.

Direct Known Subclasses

VerifyResultError, VerifyResultV2, VerifyResultV3

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(verify_response, hostname) ⇒ VerifyResult


17
18
19
20
# File 'lib/recaptcha/verify/verify_result.rb', line 17

def initialize(verify_response, hostname)
  @response = verify_response
  @expected_hostname = hostname || Recaptcha.configuration.hostname
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors


15
16
17
# File 'lib/recaptcha/verify/verify_result.rb', line 15

def errors
  @errors
end

#responseObject (readonly)

Returns the value of attribute response


14
15
16
# File 'lib/recaptcha/verify/verify_result.rb', line 14

def response
  @response
end

Instance Method Details

#hostname_valid?Boolean


40
41
42
43
44
45
46
# File 'lib/recaptcha/verify/verify_result.rb', line 40

def hostname_valid?
  case @expected_hostname
  when nil, FalseClass then true
  when String then @expected_hostname == hostname
  else @expected_hostname.call(hostname)
  end
end

#valid?Boolean

Returns true if it passes all internal validations, else false. Can check errors to see which validation failed if valid? returns false.


28
29
30
31
32
33
34
35
36
37
38
# File 'lib/recaptcha/verify/verify_result.rb', line 28

def valid?
  @errors = []
  unless response.success?
    @errors.concat error_codes if error_codes
    return false
  end
  unless hostname_valid?
    @errors << "Hostname '#{hostname}' did not match expected hostname"
  end
  @errors.none?
end