Permalink
Browse files

Removed Ambethia namespace, and restructured classes a bit

  • Loading branch information...
ambethia committed Sep 14, 2009
1 parent 4de223b commit bfc5134bfe668c64467dcaa89b891b561b87e2c1
Showing with 114 additions and 113 deletions.
  1. +2 −0 CHANGELOG
  2. +2 −2 Rakefile
  3. +2 −3 init.rb
  4. +13 −3 lib/recaptcha.rb
  5. +42 −0 lib/recaptcha/client_helper.rb
  6. +2 −0 lib/recaptcha/rails.rb
  7. +0 −92 lib/recaptcha/recaptcha.rb
  8. +39 −0 lib/recaptcha/verify.rb
  9. +6 −6 test/recaptcha_test.rb
  10. +6 −7 test/verify_recaptcha_test.rb
View
@@ -1,3 +1,5 @@
+* Removed Ambethia namespace, and restructured classes a bit
+
== 0.2.0 / 2009-09-12
* RecaptchaOptions AJAX API Fix
View
@@ -31,9 +31,9 @@ end
require 'rake/testtask'
Rake::TestTask.new(:test) do |test|
- test.libs << 'lib' << 'test'
+ test.libs << 'test'
test.pattern = 'test/**/*_test.rb'
- test.verbose = true
+ # test.verbose = true
end
begin
View
@@ -1,3 +1,2 @@
-require 'recaptcha.rb'
-ActionView::Base.send :include, Ambethia::ReCaptcha::Helper
-ActionController::Base.send :include, Ambethia::ReCaptcha::Controller
+require 'recaptcha'
+require 'recaptcha/rails'
View
@@ -1,3 +1,13 @@
-require 'recaptcha/recaptcha'
-ActionView::Base.send :include, Ambethia::ReCaptcha::Helper
-ActionController::Base.send :include, Ambethia::ReCaptcha::Controller
+require 'recaptcha/client_helper'
+require 'recaptcha/verify'
+
+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']
+
+ class RecaptchaError < StandardError
+ end
+end
@@ -0,0 +1,42 @@
+module Recaptcha
+ module ClientHelper
+ # 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
+ html = ""
+ if options[:display]
+ html << %{<script type="text/javascript">\n}
+ html << %{ var RecaptchaOptions = #{options[:display].to_json};\n}
+ html << %{</script>\n}
+ end
+ if options[:ajax]
+ html << %{<div id="dynamic_recaptcha"></div>}
+ html << %{<script type="text/javascript" src="#{uri}/js/recaptcha_ajax.js"></script>\n}
+ html << %{<script type="text/javascript">\n}
+ html << %{ Recaptcha.create('#{key}', document.getElementById('dynamic_recaptcha')#{options[:display] ? '' : ',RecaptchaOptions'});}
+ html << %{</script>\n}
+ else
+ html << %{<script type="text/javascript" src="#{uri}/challenge?k=#{key}}
+ html << %{#{error ? "&error=#{CGI::escape(error)}" : ""}"></script>\n}
+ unless options[:noscript] == false
+ html << %{<noscript>\n }
+ html << %{<iframe src="#{uri}/noscript?k=#{key}" }
+ html << %{height="#{options[:iframe_height] ||= 300}" }
+ html << %{width="#{options[:iframe_width] ||= 500}" }
+ html << %{frameborder="0"></iframe><br/>\n }
+ html << %{<textarea name="recaptcha_challenge_field" }
+ html << %{rows="#{options[:textarea_rows] ||= 3}" }
+ html << %{cols="#{options[:textarea_cols] ||= 40}"></textarea>\n }
+ html << %{<input type="hidden" name="recaptcha_response_field" value="manual_challenge">}
+ html << %{</noscript>\n}
+ end
+ end
+ raise RecaptchaError, "No public key specified." unless key
+ return html
+ end # recaptcha_tags
+ end # ClientHelper
+end # Recaptcha
View
@@ -0,0 +1,2 @@
+ActionView::Base.send(:include, Recaptcha::ClientHelper)
+ActionController::Base.send(:include, Recaptcha::Verify)
View
@@ -1,92 +0,0 @@
-# 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
- html = ""
- if options[:display]
- html << %{<script type="text/javascript">\n}
- html << %{ var RecaptchaOptions = #{options[:display].to_json};\n}
- html << %{</script>\n}
- end
- if options[:ajax]
- html << %{<div id="dynamic_recaptcha"></div>}
- html << %{<script type="text/javascript" src="#{uri}/js/recaptcha_ajax.js"></script>\n}
- html << %{<script type="text/javascript">\n}
- html << %{ Recaptcha.create('#{key}', document.getElementById('dynamic_recaptcha')#{options[:display] ? '' : ',RecaptchaOptions'});}
- html << %{</script>\n}
- else
- html << %{<script type="text/javascript" src="#{uri}/challenge?k=#{key}}
- html << %{#{error ? "&error=#{CGI::escape(error)}" : ""}"></script>\n}
- unless options[:noscript] == false
- html << %{<noscript>\n }
- html << %{<iframe src="#{uri}/noscript?k=#{key}" }
- html << %{height="#{options[:iframe_height] ||= 300}" }
- html << %{width="#{options[:iframe_width] ||= 500}" }
- html << %{frameborder="0"></iframe><br/>\n }
- html << %{<textarea name="recaptcha_challenge_field" }
- html << %{rows="#{options[:textarea_rows] ||= 3}" }
- html << %{cols="#{options[:textarea_cols] ||= 40}"></textarea>\n }
- html << %{<input type="hidden" name="recaptcha_response_field" value="manual_challenge">}
- html << %{</noscript>\n}
- end
- end
- raise ReCaptchaError, "No public key specified." unless key
- return html
- 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
View
@@ -0,0 +1,39 @@
+module Recaptcha
+ module Verify
+ # 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 # Verify
+end # Recaptcha
View
@@ -1,11 +1,11 @@
require 'test/unit'
require 'cgi'
-require File.dirname(__FILE__) + '/../lib/recaptcha/recaptcha'
+require File.dirname(__FILE__) + '/../lib/recaptcha'
-class ReCaptchaTest < Test::Unit::TestCase
- include Ambethia::ReCaptcha
- include Ambethia::ReCaptcha::Helper
- include Ambethia::ReCaptcha::Controller
+class RecaptchaClientHelperTest < Test::Unit::TestCase
+ include Recaptcha
+ include Recaptcha::ClientHelper
+ include Recaptcha::Verify
attr_accessor :session
@@ -29,7 +29,7 @@ def test_recaptcha_tags_without_noscript
end
def test_should_raise_exception_without_public_key
- assert_raise ReCaptchaError do
+ assert_raise RecaptchaError do
ENV['RECAPTCHA_PUBLIC_KEY'] = nil
recaptcha_tags
end
@@ -1,12 +1,11 @@
require 'test/unit'
-require 'rubygems'
-require 'rails/version' # For getting the rails version constants
+require 'rails/version' # For getting the rails version constants
require 'active_support/vendor' # For loading I18n
require 'mocha'
require 'net/http'
-require File.dirname(__FILE__) + '/../lib/recaptcha/recaptcha'
+require File.dirname(__FILE__) + '/../lib/recaptcha'
-class VerifyReCaptchaTest < Test::Unit::TestCase
+class RecaptchaVerifyTest < Test::Unit::TestCase
def setup
ENV['RECAPTCHA_PRIVATE_KEY'] = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
@@ -20,11 +19,11 @@ def setup
@expected_post_data["challenge"] = "challenge"
@expected_post_data["response"] = "response"
- @expected_uri = URI.parse("http://#{Ambethia::ReCaptcha::RECAPTCHA_VERIFY_SERVER}/verify")
+ @expected_uri = URI.parse("http://#{Recaptcha::RECAPTCHA_VERIFY_SERVER}/verify")
end
def test_should_raise_exception_without_private_key
- assert_raise Ambethia::ReCaptcha::ReCaptchaError do
+ assert_raise Recaptcha::RecaptchaError do
ENV['RECAPTCHA_PRIVATE_KEY'] = nil
@controller.verify_recaptcha
end
@@ -72,7 +71,7 @@ def test_returns_true_on_success_with_optional_key
private
class TestController
- include Ambethia::ReCaptcha::Controller
+ include Recaptcha::Verify
attr_accessor :request, :params, :session
def initialize

0 comments on commit bfc5134

Please sign in to comment.