Module: Sinatra::OpenCaptcha
- Defined in:
- lib/sinatra/opencaptcha.rb,
lib/sinatra/opencaptcha_version.rb
Overview
A Sinatra module to seamlessly integrate OpenCaptcha to Sinatra applications. See: www.opencaptcha.com
Constant Summary collapse
- VERSION =
'1.0.0'
- @@default_opts =
{ :width => 110, :height => 50, :show_answer_input => true, :answer_input_label => 'Your answer: ', :answer_input_class => nil }
Class Method Summary collapse
-
.set_proxy(proxy_url) ⇒ Object
A utility method to set the http proxy to be used for connecting to the OpenCatcha server.
Instance Method Summary collapse
-
#open_captcha(opts = {}) ⇒ Object
Generate the html tags for inserting the OpenCaptcha image and (if wanted) the answer input field in the form to be protected against automatic spam.
-
#open_captcha_valid?(answer_field = :open_captcha_answer) ⇒ Boolean
Check whether the user answer to the OpenCaptcha is valid or not.
Class Method Details
.set_proxy(proxy_url) ⇒ Object
A utility method to set the http proxy to be used for connecting to the OpenCatcha server.
26 27 28 |
# File 'lib/sinatra/opencaptcha.rb', line 26 def self.set_proxy(proxy_url) RestClient.proxy = proxy_url end |
Instance Method Details
#open_captcha(opts = {}) ⇒ Object
Generate the html tags for inserting the OpenCaptcha image and (if wanted) the answer input field in the form to be protected against automatic spam.
34 35 36 37 38 |
# File 'lib/sinatra/opencaptcha.rb', line 34 def open_captcha(opts={}) mopts = @@default_opts.merge(opts) mopts[:image_name] = "#{SecureRandom.hex(16)}-#{mopts[:height]}-#{mopts[:width]}.jpgx" erb :opencaptcha, :views => File.dirname(__FILE__), :locals => mopts end |
#open_captcha_valid?(answer_field = :open_captcha_answer) ⇒ Boolean
Check whether the user answer to the OpenCaptcha is valid or not. It returns ‘true’ if the answer is valid, otherwise ‘false’.
43 44 45 46 47 |
# File 'lib/sinatra/opencaptcha.rb', line 43 def open_captcha_valid?(answer_field = :open_captcha_answer) image_name = params[:open_captcha_image_name] answer = params[answer_field] RestClient.get("http://www.opencaptcha.com/validate.php?img=#{image_name}&ans=#{answer}").to_s == 'pass' end |