Module: Sinatra::ReCaptcha

Defined in:
lib/sinatra/recaptcha.rb

Constant Summary collapse

VERSION =
"0.0.1"

Class Attribute Summary collapse

Instance Method Summary collapse

Class Attribute Details

.private_keyObject

Returns the value of attribute private_key.



11
12
13
# File 'lib/sinatra/recaptcha.rb', line 11

def private_key
  @private_key
end

.public_keyObject

Returns the value of attribute public_key.



11
12
13
# File 'lib/sinatra/recaptcha.rb', line 11

def public_key
  @public_key
end

.serverObject

Returns the value of attribute server.



11
12
13
# File 'lib/sinatra/recaptcha.rb', line 11

def server
  @server
end

.verifyObject (readonly)

Returns the value of attribute verify.



12
13
14
# File 'lib/sinatra/recaptcha.rb', line 12

def verify
  @verify
end

Instance Method Details

#recaptcha(type = :iframe) ⇒ Object



15
16
17
18
# File 'lib/sinatra/recaptcha.rb', line 15

def recaptcha(type = :iframe)
  raise "Recaptcha type: #{type} is not known, please use (:iframe or :ajax)" unless [:iframe, :ajax].include?(type.to_sym)
  self.send("recaptcha_#{type}")
end

#recaptcha_correct?Boolean

Returns:

  • (Boolean)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/sinatra/recaptcha.rb', line 20

def recaptcha_correct?
  recaptcha = Net::HTTP.post_form URI.parse("#{Sinatra::ReCaptcha.verify}/verify"), {
    :privatekey => Sinatra::ReCaptcha.private_key,
    :remoteip   => request.ip,
    :challenge  => params[:recaptcha_challenge_field],
    :response   => params[:recaptcha_response_field]
  }
  answer, error = recaptcha.body.split.map { |s| s.chomp }
  unless answer == 'true'
    return false
  else
    return true
  end
end