Class: Recaptcha::Reply
- Inherits:
-
Object
- Object
- Recaptcha::Reply
- Defined in:
- lib/recaptcha/reply.rb
Instance Method Summary collapse
- #[](key) ⇒ Object
- #action ⇒ Object
- #action_valid?(expected_action) ⇒ Boolean
- #challenge_ts ⇒ Object
- #enterprise? ⇒ Boolean
- #error_codes ⇒ Object
- #hostname ⇒ Object
- #hostname_valid?(validation) ⇒ Boolean
-
#initialize(raw_reply, enterprise:) ⇒ Reply
constructor
A new instance of Reply.
- #score ⇒ Object
- #score_above_threshold?(minimum_score) ⇒ Boolean
- #score_below_threshold?(maximum_score) ⇒ Boolean
- #success ⇒ Object
- #success?(options = {}) ⇒ Boolean
- #to_h ⇒ Object
- #to_json(*args) ⇒ Object
- #to_s ⇒ Object
- #token_properties ⇒ Object
Constructor Details
#initialize(raw_reply, enterprise:) ⇒ Reply
Returns a new instance of Reply.
5 6 7 8 |
# File 'lib/recaptcha/reply.rb', line 5 def initialize(raw_reply, enterprise:) @raw_reply = raw_reply @enterprise = enterprise end |
Instance Method Details
#[](key) ⇒ Object
10 11 12 |
# File 'lib/recaptcha/reply.rb', line 10 def [](key) @raw_reply[key.to_s] end |
#action ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/recaptcha/reply.rb', line 42 def action if enterprise? token_properties&.dig('action') else @raw_reply['action'] end end |
#action_valid?(expected_action) ⇒ Boolean
85 86 87 88 89 90 91 92 |
# File 'lib/recaptcha/reply.rb', line 85 def action_valid?(expected_action) case expected_action when nil, FalseClass true else action == expected_action.to_s end end |
#challenge_ts ⇒ Object
66 67 68 69 70 |
# File 'lib/recaptcha/reply.rb', line 66 def challenge_ts return @raw_reply['challenge_ts'] unless enterprise? token_properties&.dig('createTime') end |
#enterprise? ⇒ Boolean
102 103 104 |
# File 'lib/recaptcha/reply.rb', line 102 def enterprise? @enterprise end |
#error_codes ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/recaptcha/reply.rb', line 58 def error_codes if enterprise? [] else @raw_reply['error-codes'] || [] end end |
#hostname ⇒ Object
34 35 36 37 38 39 40 |
# File 'lib/recaptcha/reply.rb', line 34 def hostname if enterprise? token_properties&.dig('hostname') else @raw_reply['hostname'] end end |
#hostname_valid?(validation) ⇒ Boolean
72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/recaptcha/reply.rb', line 72 def hostname_valid?(validation) validation ||= Recaptcha.configuration.hostname case validation when nil, FalseClass true when String validation == hostname else validation.call(hostname) end end |
#score ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/recaptcha/reply.rb', line 50 def score if enterprise? @raw_reply.dig('riskAnalysis', 'score') else @raw_reply['score'] unless enterprise? end end |
#score_above_threshold?(minimum_score) ⇒ Boolean
94 95 96 |
# File 'lib/recaptcha/reply.rb', line 94 def score_above_threshold?(minimum_score) !minimum_score || (score && score >= minimum_score) end |
#score_below_threshold?(maximum_score) ⇒ Boolean
98 99 100 |
# File 'lib/recaptcha/reply.rb', line 98 def score_below_threshold?(maximum_score) !maximum_score || (score && score <= maximum_score) end |
#success ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/recaptcha/reply.rb', line 26 def success if enterprise? token_properties&.dig('valid') else @raw_reply['success'] end end |
#success?(options = {}) ⇒ Boolean
14 15 16 17 18 19 20 |
# File 'lib/recaptcha/reply.rb', line 14 def success?( = {}) success.to_s == 'true' && hostname_valid?([:hostname]) && action_valid?([:action]) && score_above_threshold?([:minimum_score]) && score_below_threshold?([:maximum_score]) end |
#to_h ⇒ Object
106 107 108 |
# File 'lib/recaptcha/reply.rb', line 106 def to_h @raw_reply end |
#to_json(*args) ⇒ Object
114 115 116 |
# File 'lib/recaptcha/reply.rb', line 114 def to_json(*args) @raw_reply.to_json(*args) end |
#to_s ⇒ Object
110 111 112 |
# File 'lib/recaptcha/reply.rb', line 110 def to_s @raw_reply.to_s end |
#token_properties ⇒ Object
22 23 24 |
# File 'lib/recaptcha/reply.rb', line 22 def token_properties @raw_reply['tokenProperties'] if enterprise? end |