Class: ReCaptcha::Client

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

Overview

This class implements a client object capable of communicating ReCaptcha validation requests to the ReCaptcha service.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pubkey, privkey, ssl = false) ⇒ Client

pubkey

public ReCaptcha key

privkey

private ReCaptcha key (keep this a secret!)

ssl?

use https for requests when set. defaults to false.



136
137
138
139
140
141
142
143
144
# File 'lib/recaptcha.rb', line 136

def initialize(pubkey, privkey, ssl=false)
  @pubkey = pubkey
  @privkey=privkey
  @host = ssl ? 'www.google.com':'www.google.com'
  @vhost = 'www.google.com'
  @proto = ssl ? 'https' : 'http'
  @ssl = ssl
  @last_error=nil
end

Instance Attribute Details

#last_errorObject (readonly)

last recaptcha error



132
133
134
# File 'lib/recaptcha.rb', line 132

def last_error
  @last_error
end

Instance Method Details

#get_challenge(error = '', options = {}) ⇒ Object

get ReCaptcha challenge text, optionally setting the error message displayed on failure.

error

error message to be displayed on error

options

options hash. This is translated into a javascript hash and sent along to the ReCaptcha service as RecaptchaOptions



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/recaptcha.rb', line 150

def get_challenge(error='', options={})
  s=''
  if options[:options]
    s << "<script type=\"text/javascript\">\nvar RecaptchaOptions = { "
    options[:options].each do |k,v|
      val = (v.class == Fixnum) ? "#{v}" : "\"#{v}\""
      s << "#{k} : #{val}, "
    end
    s.sub!(/, $/, '};')
    s << "\n</script>\n"
  end
  errslug = (error.empty?||error==nil||error=="success") ? '' :  "&error=#{CGI.escape(error)}"
  s << %{
  <script type="text/javascript" src="#{@proto}://#{@host}/recaptcha/api/challenge?k=#{CGI.escape(@pubkey)}#{errslug}"> </script>
  <noscript>
  <iframe src="#{@proto}://#{@host}/recaptcha/api/noscript?k=#{CGI.escape(@pubkey)}#{errslug}"
  height="300" width="500" frameborder="0"></iframe><br>
  <textarea name="recaptcha_challenge_field" rows="3" cols="40">
  </textarea>
  <input type="hidden" name="recaptcha_response_field" 
  value="manual_challenge">
  </noscript>
  }
end

#validate(remoteip, challenge, response, errors, msg) ⇒ Object

Validate request. Note that this function actually makes a network request.

remoteip

request remote ip address

challenge

reCaptcha challenge

response

reCaptcha response

errors

errors hash-likethingy (usually from ActiveRecord::Base.errors)



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/recaptcha.rb', line 180

def validate(remoteip, challenge, response, errors, msg)
  unless response and challenge
    errors.add_to_base(msg)
    return false
  end
  proxy_host, proxy_port = nil, nil
  proxy_host, proxy_port = ENV['proxy_host'].split(':')  if ENV.has_key?('proxy_host')
  http = Net::HTTP::Proxy(proxy_host, proxy_port).start(@vhost)
  path='/recaptcha/api/verify'
  data = "privatekey=#{CGI.escape(@privkey)}&remoteip=#{CGI.escape(remoteip)}&challenge=#{CGI.escape(challenge)}&response=#{CGI.escape(response)}"
  resp = http.post(path, data, {'Content-Type'=>'application/x-www-form-urlencoded'})
  response = resp.body.split
  result = response[0].chomp
  @last_error=response[1].chomp
  errors.add_to_base(msg) if  result != 'true'
  result == 'true' 
end