Permalink
Browse files

Merge pull request #2 from stefants/master

Fixed some issues and created a gem
  • Loading branch information...
2 parents 4d4827b + dc0c1bb commit 5360ff07cd012e3b7f93f226063d1ceeb2d44de1 @subwindow committed Jul 14, 2011
View
@@ -1,10 +1,10 @@
-h1. Negative Captcha Plugin
+h1. Negative Captcha
h2. What is a Negative Captcha?
A negative captcha has the exact same purpose as your run-of-the-mill image captcha: To keep bots from submitting forms. Image ("positive") captchas do this by implementing a step which only humans can do, but bots cannot: read jumbled characters from an image. But this is bad. It creates usability problems, it hurts conversion rates, and it confuses the shit out of lots of people. Why not do it the other way around? Negative captchas create a form that has tasks that only bots can perform, but humans cannot. This has the exact same effect, with (anecdotally) a much lower false positive identification rate when compared with positive captchas. All of this comes without making humans go through any extra trouble to submit the form. It really is win-win.
-h2. How does a Negative Captcha Work?
+h2. How does it work?
In a negative captcha form there are two main parts and three ancillary parts. I'll explain them thusly.
@@ -40,23 +40,25 @@ h4. Secret key
This is simply some key that is used in the hashing method to prevent bots from backing out the name of the field from the hashed field name.
-h2. How does the Negative Captcha Plugin Work?
+h2. How does Negative Captcha work?
-h3. Install the plugin
+h3. Installation
-h4. Subversion Installation
+h4. Rails 3
+
+You can let bundler install Negative Captcha by adding this line to your application’s Gemfile:
<pre>
<code>
- script/plugin install http://code.subwindow.com/negative_captcha
+ gem 'negative-captcha', :git => 'git://github.com/stefants/negative-captcha.git'
</code>
</pre>
-
-h4. Git Installation
+
+And then execute:
<pre>
<code>
- git submodule add git://github.com/subwindow/negative-captcha.git vendor/plugins/negative_captcha
+ bundle install
</code>
</pre>
@@ -108,18 +110,18 @@ Modify your form to include the honeypots and other fields. You can probably le
<pre>
<code>
<% form_tag comments_path do -%>
- <%= negative_captcha(@captcha) %>
+ <%= raw negative_captcha(@captcha) %>
<ul class="contact_us">
<li>
- <%= negative_label_tag(@captcha, :name, 'Name:') %>
+ <%= negative_label_tag(@captcha, :name, 'Name:') %>
<%= negative_text_field_tag @captcha, :name %>
</li>
<li>
- <%= negative_label_tag(@captcha, :email, 'Email:') %>
+ <%= negative_label_tag(@captcha, :email, 'Email:') %>
<%= negative_text_field_tag @captcha, :email %>
</li>
<li>
- <%= negative_label_tag(@captcha, :body, 'Your Comment:') %>
+ <%= negative_label_tag(@captcha, :body, 'Your Comment:') %>
<%= negative_text_area_tag @captcha, :body %>
</li>
<li>
View
@@ -5,18 +5,34 @@ require 'rake/rdoctask'
desc 'Default: run unit tests.'
task :default => :test
-desc 'Test the negative_captcha plugin.'
+desc 'Tests negative-captcha.'
Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.pattern = 'test/**/*_test.rb'
t.verbose = true
end
-desc 'Generate documentation for the negative_captcha plugin.'
+desc 'Generates documentation for negative-captcha.'
Rake::RDocTask.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = 'NegativeCaptcha'
rdoc.options << '--line-numbers' << '--inline-source'
rdoc.rdoc_files.include('README')
rdoc.rdoc_files.include('lib/**/*.rb')
end
+
+desc 'Create the negative-captcha gemspec'
+begin
+ require 'jeweler'
+ Jeweler::Tasks.new do |gem|
+ gem.name = 'negative-captcha'
+ gem.summary = %Q{Makes the process of creating a negative captcha in Rails much less painless.}
+ gem.description = %Q{Makes the process of creating a negative captcha in Rails much less painless.}
+ gem.email = 'erik@skribit.com'
+ gem.homepage = 'https://github.com/stefants/negative-captcha'
+ gem.authors = ['subwindow']
+ end
+ Jeweler::GemcutterTasks.new
+rescue LoadError
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
+end
View
@@ -1,6 +1 @@
-require 'negative_captcha'
-if !ActionView::Base.instance_methods.include? 'negative_captcha'
- require 'negative_captcha_view_helpers'
- ActionView::Base.class_eval { include NegativeCaptchaViewHelpers }
-end
-require "negative_captcha_form_builder"
+require 'rails/init'
@@ -17,7 +17,7 @@ class NegativeCaptcha
def initialize(opts)
@secret = opts[:secret]||(RUBY_19 ? Digest::MD5.hexdigest("this_is_a_secret_key") : MD5.hexdigest("this_is_a_secret_key"))
- @timestamp = (opts.has_key?(:params) ? opts[:params][:timestamp] : nil) || Time.now()
+ @timestamp = (opts.has_key?(:params) ? opts[:params][:timestamp] : nil) || Time.now.to_i
spinner_text = ([@timestamp, @secret] + (opts[:spinner].is_a?(Array) ? opts[:spinner] : [opts[:spinner]]))*'-'
@spinner = RUBY_19 ? Digest::MD5.hexdigest(spinner_text) : MD5.hexdigest(spinner_text)
@message = opts[:message]||"Please try again. This usually happens because an automated script attempted to submit this form."
@@ -60,3 +60,9 @@ def process(params)
end
end
end
+
+if !ActionView::Base.instance_methods.include? 'negative_captcha'
+ require 'negative_captcha_view_helpers'
+ ActionView::Base.class_eval { include NegativeCaptchaViewHelpers }
+end
+require "negative_captcha_form_builder"
@@ -2,16 +2,14 @@ module ActionView
module Helpers
class FormBuilder
def negative_text_field(captcha, method, options = {})
- obj = @template.instance_variable_get("@#{@object_name}")
html = @template.negative_text_field_tag(captcha, method, options).html_safe
- html = "<div class='fieldWithErrors'>#{html}</div>" if obj.errors.on(method).present?
+ html = "<div class='fieldWithErrors'>#{html}</div>" if @object.errors[method].present?
html.html_safe
end
def negative_text_area(captcha, method, options = {})
- obj = @template.instance_variable_get("@#{@object_name}")
html = @template.negative_text_area_tag(captcha, method, options).html_safe
- html = "<div class='fieldWithErrors'>#{html}</div>" if obj.errors.on(method).present?
+ html = "<div class='fieldWithErrors'>#{html}</div>" if @object.errors[method].present?
html.html_safe
end
@@ -20,16 +18,14 @@ def negative_hidden_field(captcha, method, options = {})
end
def negative_password_field(captcha, method, options = {})
- obj = @template.instance_variable_get("@#{@object_name}")
html = @template.negative_password_field_tag(captcha, method, options).html_safe
- html = "<div class='fieldWithErrors'>#{html}</div>" if obj.errors.on(method).present?
+ html = "<div class='fieldWithErrors'>#{html}</div>" if @object.errors[method].present?
html.html_safe
end
def negative_label(captcha, method, name, options = {})
- obj = @template.instance_variable_get("@#{@object_name}")
html = @template.negative_label_tag(captcha, method, name, options).html_safe
- html = "<div class='fieldWithErrors'>#{html}</div>" if obj.errors.on(method).present?
+ html = "<div class='fieldWithErrors'>#{html}</div>" if @object.errors[method].present?
html.html_safe
end
end
@@ -1,7 +1,7 @@
module NegativeCaptchaViewHelpers
def negative_captcha(captcha)
[
- hidden_field_tag('timestamp', captcha.timestamp.to_i),
+ hidden_field_tag('timestamp', captcha.timestamp),
hidden_field_tag('spinner', captcha.spinner),
].join
end
View
@@ -0,0 +1,46 @@
+# Generated by jeweler
+# DO NOT EDIT THIS FILE DIRECTLY
+# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
+# -*- encoding: utf-8 -*-
+
+Gem::Specification.new do |s|
+ s.name = %q{negative-captcha}
+ s.version = "0.1.0"
+
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
+ s.authors = ["subwindow"]
+ s.date = %q{2011-05-23}
+ s.description = %q{Makes the process of creating a negative captcha in Rails much less painless.}
+ s.email = %q{erik@skribit.com}
+ s.extra_rdoc_files = [
+ "LICENSE",
+ "README.textile"
+ ]
+ s.files = [
+ "LICENSE",
+ "README.textile",
+ "Rakefile",
+ "init.rb",
+ "lib/negative-captcha.rb",
+ "lib/negative_captcha_form_builder.rb",
+ "lib/negative_captcha_view_helpers.rb",
+ "meta.yml",
+ "negative-captcha.gemspec",
+ "rails/init.rb",
+ "test/negative_captcha_test.rb"
+ ]
+ s.homepage = %q{https://github.com/stefants/negative-captcha}
+ s.require_paths = ["lib"]
+ s.rubygems_version = %q{1.5.2}
+ s.summary = %q{Makes the process of creating a negative captcha in Rails much less painless.}
+
+ if s.respond_to? :specification_version then
+ s.specification_version = 3
+
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
+ else
+ end
+ else
+ end
+end
+
View
@@ -0,0 +1 @@
+require 'negative-captcha'
@@ -1,7 +1,8 @@
require 'rubygems'
require 'active_support'
+require 'action_view'
require 'test/unit'
-require File.expand_path(File.join(File.dirname(__FILE__), '../lib/negative_captcha'))
+require File.expand_path(File.join(File.dirname(__FILE__), '../lib/negative-captcha'))
class NegativeCaptchaTest < Test::Unit::TestCase
def test_valid_submission

0 comments on commit 5360ff0

Please sign in to comment.