diff --git a/.gitignore b/.gitignore index 6e89c571..5efe2f03 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ html -doc \ No newline at end of file +doc +pkg \ No newline at end of file diff --git a/CHANGELOG b/CHANGELOG new file mode 100644 index 00000000..5f3433fe --- /dev/null +++ b/CHANGELOG @@ -0,0 +1,4 @@ +== 0.1.0 / 2008-2-8 + +* 1 major enhancement + * Initial Gem Release \ No newline at end of file diff --git a/Manifest b/Manifest new file mode 100644 index 00000000..81a184f0 --- /dev/null +++ b/Manifest @@ -0,0 +1,12 @@ +init.rb +install.rb +lib/recaptcha/recaptcha.rb +lib/recaptcha.rb +LICENSE +Manifest +Rakefile +README.rdoc +tasks/recaptcha_tasks.rake +test/recaptcha_test.rb +test/verify_recaptcha_test.rb +uninstall.rb diff --git a/README.rdoc b/README.rdoc index 961b4446..56803710 100644 --- a/README.rdoc +++ b/README.rdoc @@ -1,12 +1,18 @@ = ReCAPTCHA Author:: Jason L Perry (http://ambethia.com) + Copyright:: Copyright (c) 2007 Jason L Perry + License:: MIT + RDOC:: http://ambethia.com/recaptcha/ + Git:: http://github.com/ambethia/recaptcha/tree/master + Bugs:: http://ambethia.lighthouseapp.com/projects/11072-recaptcha/overview + This plugin adds helpers for the ReCAPTCHA API (http://recaptcha.net/). In your views you can use the +recaptcha_tags+ method to embed the needed javascript, and you can validate in your controllers with +verify_recaptcha+. diff --git a/Rakefile b/Rakefile index 92837cb4..e5372da1 100644 --- a/Rakefile +++ b/Rakefile @@ -1,6 +1,17 @@ require 'rake' require 'rake/testtask' require 'rake/rdoctask' +require 'echoe' + +Echoe.new('recaptcha', '0.1.0') do |p| + p.description = "This plugin adds helpers for the ReCAPTCHA API " + p.url = "http://github.com/ambethia/recaptcha/tree/master" + p.author = "Jason L. Perry" + p.email = "jasper@ambethia.com" + p.ignore_pattern = ["pkg/**/*"] + p.development_dependencies = [] +end + desc 'Default: run unit tests.' task :default => :test diff --git a/install.rb b/install.rb deleted file mode 100644 index f7732d37..00000000 --- a/install.rb +++ /dev/null @@ -1 +0,0 @@ -# Install hook code here diff --git a/lib/recaptcha.rb b/lib/recaptcha.rb index 0245c111..a1c074d2 100644 --- a/lib/recaptcha.rb +++ b/lib/recaptcha.rb @@ -1,90 +1,3 @@ -# ReCAPTCHA -module Ambethia - module ReCaptcha - RECAPTCHA_API_SERVER = 'http://api.recaptcha.net'; - RECAPTCHA_API_SECURE_SERVER = 'https://api-secure.recaptcha.net'; - RECAPTCHA_VERIFY_SERVER = 'api-verify.recaptcha.net'; - - SKIP_VERIFY_ENV = ['test', 'cucumber'] - - module Helper - # Your public API can be specified in the +options+ hash or preferably the environment - # variable +RECAPTCHA_PUBLIC_KEY+. - def recaptcha_tags(options = {}) - # Default options - key = options[:public_key] ||= ENV['RECAPTCHA_PUBLIC_KEY'] - error = options[:error] ||= session[:recaptcha_error] - uri = options[:ssl] ? RECAPTCHA_API_SECURE_SERVER : RECAPTCHA_API_SERVER - xhtml = Builder::XmlMarkup.new :target => out=(''), :indent => 2 # Because I can. - if options[:display] - xhtml.script(:type => "text/javascript"){ |x| x << "var RecaptchaOptions = #{options[:display].to_json};\n"} - end - if options[:ajax] - xhtml.div(:id => 'dynamic_recaptcha') {} - xhtml.script(:type => "text/javascript", :src => "#{uri}/js/recaptcha_ajax.js") {} - xhtml.script(:type => "text/javascript") do |x| - x << "Recaptcha.create('#{key}', document.getElementById('dynamic_recaptcha')#{(options[:display].blank?)? '' : ',RecaptchaOptions'});" - end - else - xhtml.script(:type => "text/javascript", :src => CGI::escapeHTML("#{uri}/challenge?k=#{key}&error=#{error}")) {} - unless options[:noscript] == false - xhtml.noscript do - xhtml.iframe(:src => "#{uri}/noscript?k=#{key}", - :height => options[:iframe_height] ||= 300, - :width => options[:iframe_width] ||= 500, - :frameborder => 0) {}; xhtml.br - xhtml.textarea nil, :name => "recaptcha_challenge_field", - :rows => options[:textarea_rows] ||= 3, - :cols => options[:textarea_cols] ||= 40 - xhtml.input :name => "recaptcha_response_field", - :type => "hidden", :value => "manual_challenge" - end - end - end - raise ReCaptchaError, "No public key specified." unless key - return out - end # recaptcha_tags - end # Helpers - - module Controller - # Your private API can be specified in the +options+ hash or preferably the environment - # variable +RECAPTCHA_PUBLIC_KEY+. - def verify_recaptcha(options = {}) - return true if SKIP_VERIFY_ENV.include? ENV['RAILS_ENV'] - model = options.is_a?(Hash)? options[:model] : options - private_key = options[:private_key] if options.is_a?(Hash) - private_key ||= ENV['RECAPTCHA_PRIVATE_KEY'] - raise ReCaptchaError, "No private key specified." unless private_key - begin - recaptcha = Net::HTTP.post_form URI.parse("http://#{RECAPTCHA_VERIFY_SERVER}/verify"), { - "privatekey" => private_key, - "remoteip" => request.remote_ip, - "challenge" => params[:recaptcha_challenge_field], - "response" => params[:recaptcha_response_field] - } - answer, error = recaptcha.body.split.map { |s| s.chomp } - unless answer == 'true' - session[:recaptcha_error] = error - if model - model.valid? - if Rails::VERSION::MAJOR == 2 and Rails::VERSION::MINOR >= 2 - model.errors.add :base, I18n.translate("#{model.class.name.underscore}.captcha", :scope => %w(errors models), :default => (options[:message] || "Captcha response is incorrect, please try again.")) - else - model.errors.add :base, options[:message] || "Captcha response is incorrect, please try again." - end - end - return false - else - session[:recaptcha_error] = nil - return true - end - rescue Exception => e - raise ReCaptchaError, e - end - end # verify_recaptcha - end # ControllerHelpers - - class ReCaptchaError < StandardError; end - - end # ReCaptcha -end # Ambethia +require 'recaptcha/recaptcha' +ActionView::Base.send :include, Ambethia::ReCaptcha::Helper +ActionController::Base.send :include, Ambethia::ReCaptcha::Controller diff --git a/lib/recaptcha/recaptcha.rb b/lib/recaptcha/recaptcha.rb new file mode 100644 index 00000000..0245c111 --- /dev/null +++ b/lib/recaptcha/recaptcha.rb @@ -0,0 +1,90 @@ +# ReCAPTCHA +module Ambethia + module ReCaptcha + RECAPTCHA_API_SERVER = 'http://api.recaptcha.net'; + RECAPTCHA_API_SECURE_SERVER = 'https://api-secure.recaptcha.net'; + RECAPTCHA_VERIFY_SERVER = 'api-verify.recaptcha.net'; + + SKIP_VERIFY_ENV = ['test', 'cucumber'] + + module Helper + # Your public API can be specified in the +options+ hash or preferably the environment + # variable +RECAPTCHA_PUBLIC_KEY+. + def recaptcha_tags(options = {}) + # Default options + key = options[:public_key] ||= ENV['RECAPTCHA_PUBLIC_KEY'] + error = options[:error] ||= session[:recaptcha_error] + uri = options[:ssl] ? RECAPTCHA_API_SECURE_SERVER : RECAPTCHA_API_SERVER + xhtml = Builder::XmlMarkup.new :target => out=(''), :indent => 2 # Because I can. + if options[:display] + xhtml.script(:type => "text/javascript"){ |x| x << "var RecaptchaOptions = #{options[:display].to_json};\n"} + end + if options[:ajax] + xhtml.div(:id => 'dynamic_recaptcha') {} + xhtml.script(:type => "text/javascript", :src => "#{uri}/js/recaptcha_ajax.js") {} + xhtml.script(:type => "text/javascript") do |x| + x << "Recaptcha.create('#{key}', document.getElementById('dynamic_recaptcha')#{(options[:display].blank?)? '' : ',RecaptchaOptions'});" + end + else + xhtml.script(:type => "text/javascript", :src => CGI::escapeHTML("#{uri}/challenge?k=#{key}&error=#{error}")) {} + unless options[:noscript] == false + xhtml.noscript do + xhtml.iframe(:src => "#{uri}/noscript?k=#{key}", + :height => options[:iframe_height] ||= 300, + :width => options[:iframe_width] ||= 500, + :frameborder => 0) {}; xhtml.br + xhtml.textarea nil, :name => "recaptcha_challenge_field", + :rows => options[:textarea_rows] ||= 3, + :cols => options[:textarea_cols] ||= 40 + xhtml.input :name => "recaptcha_response_field", + :type => "hidden", :value => "manual_challenge" + end + end + end + raise ReCaptchaError, "No public key specified." unless key + return out + end # recaptcha_tags + end # Helpers + + module Controller + # Your private API can be specified in the +options+ hash or preferably the environment + # variable +RECAPTCHA_PUBLIC_KEY+. + def verify_recaptcha(options = {}) + return true if SKIP_VERIFY_ENV.include? ENV['RAILS_ENV'] + model = options.is_a?(Hash)? options[:model] : options + private_key = options[:private_key] if options.is_a?(Hash) + private_key ||= ENV['RECAPTCHA_PRIVATE_KEY'] + raise ReCaptchaError, "No private key specified." unless private_key + begin + recaptcha = Net::HTTP.post_form URI.parse("http://#{RECAPTCHA_VERIFY_SERVER}/verify"), { + "privatekey" => private_key, + "remoteip" => request.remote_ip, + "challenge" => params[:recaptcha_challenge_field], + "response" => params[:recaptcha_response_field] + } + answer, error = recaptcha.body.split.map { |s| s.chomp } + unless answer == 'true' + session[:recaptcha_error] = error + if model + model.valid? + if Rails::VERSION::MAJOR == 2 and Rails::VERSION::MINOR >= 2 + model.errors.add :base, I18n.translate("#{model.class.name.underscore}.captcha", :scope => %w(errors models), :default => (options[:message] || "Captcha response is incorrect, please try again.")) + else + model.errors.add :base, options[:message] || "Captcha response is incorrect, please try again." + end + end + return false + else + session[:recaptcha_error] = nil + return true + end + rescue Exception => e + raise ReCaptchaError, e + end + end # verify_recaptcha + end # ControllerHelpers + + class ReCaptchaError < StandardError; end + + end # ReCaptcha +end # Ambethia diff --git a/uninstall.rb b/uninstall.rb deleted file mode 100644 index 97383334..00000000 --- a/uninstall.rb +++ /dev/null @@ -1 +0,0 @@ -# Uninstall hook code here