Class: ReCaptcha::Client
- Inherits:
-
Object
- Object
- ReCaptcha::Client
- 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
-
#last_error ⇒ Object
readonly
last recaptcha error.
Instance Method Summary collapse
- #add_base_error(msg, errors) ⇒ Object
-
#get_challenge(error = '', options = {}) ⇒ Object
get ReCaptcha challenge text, optionally setting the error message displayed on failure.
-
#initialize(pubkey, privkey, ssl = false) ⇒ Client
constructor
- pubkey
-
public ReCaptcha key [privkey] private ReCaptcha key (keep this a secret!) [ssl?] use https for requests when set.
-
#validate(remoteip, challenge, response, errors, msg) ⇒ Object
Validate request.
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_error ⇒ Object (readonly)
last recaptcha error
132 133 134 |
# File 'lib/recaptcha.rb', line 132 def last_error @last_error end |
Instance Method Details
#add_base_error(msg, errors) ⇒ Object
174 175 176 177 178 179 180 |
# File 'lib/recaptcha.rb', line 174 def add_base_error(msg, errors) if errors.respond_to?(:add_to_base) errors.add_to_base(msg) else errors.add(:base, msg) end end |
#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='', ={}) s='' if [:options] s << "<script type=\"text/javascript\">\nvar RecaptchaOptions = { " [: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)
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/recaptcha.rb', line 187 def validate(remoteip, challenge, response, errors, msg) unless response and challenge add_base_error(msg, errors) return false end proxy_host, proxy_port = nil, nil proxy_host, proxy_port = ENV['proxy_host'].split(':') if ENV.has_key?('proxy_host') proxy_user=ENV['proxy_user'] proxy_pass=ENV['proxy_pass'] http = Net::HTTP::Proxy(proxy_host, proxy_port, proxy_user, proxy_pass).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'}) data = resp.body response = resp.body.split result = response[0].chomp @last_error=response[1].chomp add_base_error(msg, errors) if result != 'true' result == 'true' end |