Class: Recaptcha::Reply

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

Instance Method Summary collapse

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

#actionObject



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

Returns:

  • (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_tsObject



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

Returns:

  • (Boolean)


102
103
104
# File 'lib/recaptcha/reply.rb', line 102

def enterprise?
  @enterprise
end

#error_codesObject



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

#hostnameObject



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

Returns:

  • (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

#scoreObject



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

Returns:

  • (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

Returns:

  • (Boolean)


98
99
100
# File 'lib/recaptcha/reply.rb', line 98

def score_below_threshold?(maximum_score)
  !maximum_score || (score && score <= maximum_score)
end

#successObject



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

Returns:

  • (Boolean)


14
15
16
17
18
19
20
# File 'lib/recaptcha/reply.rb', line 14

def success?(options = {})
  success.to_s == 'true' &&
    hostname_valid?(options[:hostname]) &&
    action_valid?(options[:action]) &&
    score_above_threshold?(options[:minimum_score]) &&
    score_below_threshold?(options[:maximum_score])
end

#to_hObject



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_sObject



110
111
112
# File 'lib/recaptcha/reply.rb', line 110

def to_s
  @raw_reply.to_s
end

#token_propertiesObject



22
23
24
# File 'lib/recaptcha/reply.rb', line 22

def token_properties
  @raw_reply['tokenProperties'] if enterprise?
end