Skip to content

Commit

Permalink
Removed Ambethia namespace, and restructured classes a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason L Perry committed Sep 14, 2009
1 parent 4de223b commit bfc5134
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 113 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
@@ -1,3 +1,5 @@
* Removed Ambethia namespace, and restructured classes a bit

== 0.2.0 / 2009-09-12

* RecaptchaOptions AJAX API Fix
Expand Down
4 changes: 2 additions & 2 deletions Rakefile
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions init.rb
@@ -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'
16 changes: 13 additions & 3 deletions lib/recaptcha.rb
@@ -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
42 changes: 42 additions & 0 deletions lib/recaptcha/client_helper.rb
@@ -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
2 changes: 2 additions & 0 deletions lib/recaptcha/rails.rb
@@ -0,0 +1,2 @@
ActionView::Base.send(:include, Recaptcha::ClientHelper)
ActionController::Base.send(:include, Recaptcha::Verify)
92 changes: 0 additions & 92 deletions lib/recaptcha/recaptcha.rb

This file was deleted.

39 changes: 39 additions & 0 deletions lib/recaptcha/verify.rb
@@ -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
12 changes: 6 additions & 6 deletions test/recaptcha_test.rb
@@ -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

Expand All @@ -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
Expand Down
13 changes: 6 additions & 7 deletions test/verify_recaptcha_test.rb
@@ -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'
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit bfc5134

Please sign in to comment.