Class: NewGoogleRecaptcha::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/new_google_recaptcha/validator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token:, action:, minimum_score:) ⇒ Validator

Returns a new instance of Validator.



8
9
10
11
12
# File 'lib/new_google_recaptcha/validator.rb', line 8

def initialize(token:, action:, minimum_score:)
  @token         = token
  @action        = action
  @minimum_score = minimum_score
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



6
7
8
# File 'lib/new_google_recaptcha/validator.rb', line 6

def action
  @action
end

#minimum_scoreObject (readonly)

Returns the value of attribute minimum_score.



6
7
8
# File 'lib/new_google_recaptcha/validator.rb', line 6

def minimum_score
  @minimum_score
end

#scoreObject (readonly)

Returns the value of attribute score.



5
6
7
# File 'lib/new_google_recaptcha/validator.rb', line 5

def score
  @score
end

#tokenObject (readonly)

Returns the value of attribute token.



6
7
8
# File 'lib/new_google_recaptcha/validator.rb', line 6

def token
  @token
end

Instance Method Details

#callObject



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/new_google_recaptcha/validator.rb', line 14

def call
  uri    = NewGoogleRecaptcha.compose_uri(token)
  result = JSON.parse(Net::HTTP.get(uri))

  @score = result['score'].to_f

  conditions = []
  conditions << !!result['success']
  conditions << (result['score'].to_f >= minimum_score)
  conditions << (result['action'].to_s == action.to_s)
  conditions.none?(&:!)
end