Class: Rad::Captcha

Inherits:
Object
  • Object
show all
Defined in:
lib/components/captcha.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#enabledObject

Returns the value of attribute enabled.



2
3
4
# File 'lib/components/captcha.rb', line 2

def enabled
  @enabled
end

#private_keyObject

Returns the value of attribute private_key.



2
3
4
# File 'lib/components/captcha.rb', line 2

def private_key
  @private_key
end

#public_keyObject

Returns the value of attribute public_key.



2
3
4
# File 'lib/components/captcha.rb', line 2

def public_key
  @public_key
end

#timeoutObject

Returns the value of attribute timeout.



2
3
4
# File 'lib/components/captcha.rb', line 2

def timeout
  @timeout
end

#verify_urlObject

Returns the value of attribute verify_url.



2
3
4
# File 'lib/components/captcha.rb', line 2

def verify_url
  @verify_url
end

Instance Method Details

#enabled?Boolean

Returns:

  • (Boolean)


4
# File 'lib/components/captcha.rb', line 4

def enabled?; !!enabled end

#verify(request, params) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/components/captcha.rb', line 6

def verify request, params
  recaptcha = nil
  Timeout::timeout(timeout || 3) do
    recaptcha = Net::HTTP.post_form URI.parse(verify_url), {
      'privatekey' => private_key,
      'remoteip' => request.ip,
      'challenge' => params.recaptcha_challenge_field,
      'response' => params.recaptcha_response_field
    }
  end
  result = recaptcha.body.split.map { |s| s.chomp }
  answer, error = result
  answer == 'true'
end