is_bot 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ is_bot.database
2
+ is_bot.db
data/LICENSE ADDED
File without changes
data/README.markdown ADDED
@@ -0,0 +1,40 @@
1
+ is_bot
2
+ ======
3
+
4
+ About
5
+ -----
6
+
7
+ **is_bot** is a clean rewrite of **less_reverse_captcha**
8
+
9
+ It is a simple way to make your forms bot-proof. The effectiveness of the solution is open to discussion. I have used it on a low traffic site and it seems to have reduced the bot-attack considerably.
10
+
11
+
12
+ Usage
13
+ -----
14
+ It is very simple to use. Just install it in your rails app and perform the following steps.
15
+
16
+ In your model class:
17
+
18
+ class User < ActiveRecord::Base
19
+ validate_captcha
20
+ end
21
+
22
+ In your model's view:
23
+
24
+ form_for @user do |F|
25
+ f.captcha_reverse_field :user
26
+ end
27
+
28
+ And that's it.
29
+
30
+
31
+ Bugs
32
+ ----
33
+
34
+ Please report the issues through gitHub's issues.
35
+
36
+
37
+ Authors
38
+ -------
39
+
40
+ * Anuj Dutta - [andHapp.com](http://www.andhapp.com/blog)
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ begin
2
+ require 'jeweler'
3
+ Jeweler::Tasks.new do |gemspec|
4
+ gemspec.name = "is_bot"
5
+ gemspec.summary = "Fight the bot."
6
+ gemspec.description = "Simple gem to reduce the spam attacks on sign-up, sign-in and other similar sorts of forms."
7
+ gemspec.email = "anuj@andhapp.com"
8
+ gemspec.homepage = "http://github.com/andhapp/is_bot"
9
+ gemspec.authors = ["Anuj Dutta"]
10
+ end
11
+ Jeweler::GemcutterTasks.new
12
+ rescue LoadError
13
+ puts "Jeweler not available. Install it with: sudo gem install jeweler"
14
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/init.rb ADDED
@@ -0,0 +1,4 @@
1
+ require File.join(File.dirname(__FILE__), "lib", "is_bot")
2
+
3
+ ActiveRecord::Base.send(:include, IsBot::CaptchaInReverse)
4
+ ActionView::Base.send(:include, IsBot::ViewHelper)
@@ -0,0 +1,29 @@
1
+ module IsBot
2
+ module CaptchaInReverse
3
+ def self.included(base)
4
+ base.class_eval do
5
+ extend ClassMethods
6
+ class_inheritable_accessor :captcha_in_reverse
7
+ end
8
+ end
9
+
10
+ module ClassMethods
11
+ def validate_captcha(options = {})
12
+ configuration = { :message => 'can not create this because you are a bot.', :on => :save }
13
+ configuration.merge!(options)
14
+ validates_each :captcha_in_reverse, configuration do |record, attr, value|
15
+ record.errors.add attr, configuration[:message] if not value.blank?
16
+ end
17
+ end
18
+ end
19
+ end
20
+
21
+ module ViewHelper
22
+ def captcha_reverse_field(object, options={})
23
+ style = (options.delete(:style) || '') << ";"
24
+ style << "display: none;"
25
+ ActionView::Helpers::InstanceTag.new(object, :captcha_in_reverse, self).to_input_field_tag("text", options.merge(:style => style) )
26
+ end
27
+ end
28
+
29
+ end
data/lib/is_bot.rb ADDED
@@ -0,0 +1,3 @@
1
+ $: << File.join(File.dirname(__FILE__), "..", "lib")
2
+
3
+ require "is_bot/is_bot"
data/spec/database.yml ADDED
@@ -0,0 +1,3 @@
1
+ test:
2
+ :adapter: sqlite3
3
+ :database: is_bot.db
@@ -0,0 +1,93 @@
1
+ %w(spec_helper ../init models).each{|path| require File.join(File.dirname(__FILE__), path) }
2
+
3
+ # ********************* SET-UP **************************
4
+ # ---------- Models
5
+ # There are two models that are used for describing IsBot
6
+ # 1. Bot
7
+ # 2. Robot
8
+ # Both the models can be found in the models.rb file
9
+ #
10
+ # ---------- Schema
11
+ # The schema is described in scehma.rb
12
+ #
13
+ # ---------- Database configuration
14
+ # The specs use sqlite3 adapter and the configuration is described in database.yml
15
+
16
+ describe IsBot do
17
+ context "ClassMethods" do
18
+ context "Set up" do
19
+
20
+ it "should be in the list of included modules" do
21
+ Bot.included_modules.should include(IsBot::CaptchaInReverse)
22
+ end
23
+
24
+ it "should add a virtual accessor 'captcha_in_reverse' to the class" do
25
+ bot = Bot.new
26
+ bot.should.respond_to?(:captcha_in_reverse)
27
+ bot.should.respond_to?(:captcha_in_reverse=)
28
+ end
29
+
30
+ it "should add a class method 'validate_captcha'" do
31
+ Bot.should.respond_to?(:validate_captcha)
32
+ end
33
+ end
34
+
35
+ context "Validation" do
36
+
37
+ before(:each) do
38
+ @bot = Bot.new
39
+ end
40
+
41
+ # because a bot would fill in the hidden field really
42
+ it "should add to the errors if 'captcha_in_reverse' is not blank?" do
43
+ lambda {
44
+ @bot.captcha_in_reverse = "I am a Bot!"
45
+ @bot.valid?
46
+ }.should change(@bot.errors, :size).by(1)
47
+ @bot.errors.on "captcha_in_reverse" == "can not create this because you are a bot."
48
+ end
49
+
50
+ # because human is not expected to fill this field
51
+ it "should not add to errors when 'captcha_in_reverse' is blank? " do
52
+ lambda {
53
+ @bot.captcha_in_reverse = nil
54
+ @bot.valid?
55
+ }.should_not change(@bot.errors, :size).by(1)
56
+ end
57
+
58
+ context "Custom validation" do
59
+ it "should accept a custom error message" do
60
+ robot = Robot.new(:captcha_in_reverse => "I am a Bot!")
61
+ robot.valid?
62
+ robot.errors.on "captcha_in_reverse" == "You are a BOT. Go away."
63
+ end
64
+ end
65
+
66
+ end
67
+ end
68
+
69
+ context "Helper" do
70
+ context "set up" do
71
+ it "should include the defined custom helper module" do
72
+ ViewHelper::ViewField.included_modules.should include(IsBot::ViewHelper)
73
+ end
74
+ end
75
+
76
+ context "input tag" do
77
+ it "should create one for adding captcha" do
78
+ ViewHelper::ViewField.new.should.respond_to?(:captcha_reverse_field)
79
+ end
80
+
81
+ it "should be hidden" do
82
+ captcha_field = ViewHelper::ViewField.new.captcha_reverse_field(:bot)
83
+ captcha_field.should include("style=\";display: none;\"")
84
+ end
85
+
86
+ it "should include the custom defined styles" do
87
+ captcha_field = ViewHelper::ViewField.new.captcha_reverse_field(:bot, {:style => "color:red"})
88
+ captcha_field.should include("style=\"color:red;display: none;\"")
89
+ end
90
+ end
91
+ end
92
+
93
+ end
data/spec/models.rb ADDED
@@ -0,0 +1,7 @@
1
+ class Bot < ActiveRecord::Base
2
+ validate_captcha
3
+ end
4
+
5
+ class Robot < ActiveRecord::Base
6
+ validate_captcha :message => "You are a BOT. Go away."
7
+ end
data/spec/schema.db ADDED
@@ -0,0 +1,14 @@
1
+ ActiveRecord::Schema.define(:version => 1) do
2
+ create_table "bots", :force => true do |t|
3
+ t.string "name"
4
+ t.datetime "created_at"
5
+ t.datetime "updated_at"
6
+ end
7
+
8
+ create_table "robots", :force => true do |t|
9
+ t.string "name"
10
+ t.datetime "created_at"
11
+ t.datetime "updated_at"
12
+ end
13
+
14
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1,2 @@
1
+ --format nested
2
+ --color
@@ -0,0 +1,20 @@
1
+ require File.join(File.dirname(__FILE__), "..", "lib", "is_bot")
2
+
3
+ require 'activerecord'
4
+ require 'action_view'
5
+ require 'yaml'
6
+ require 'spec'
7
+
8
+ def connect(environment)
9
+ conf = YAML::load(File.open(File.dirname(__FILE__) + '/database.yml'))
10
+ ActiveRecord::Base.establish_connection(conf[environment])
11
+ end
12
+
13
+ connect('test')
14
+ load(File.join(File.dirname(__FILE__), "schema.db"))
15
+
16
+ module ViewHelper; class ViewField < ActionView::Base; end end
17
+
18
+ Spec::Runner.configure do |config|
19
+ config.include(ViewHelper)
20
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: is_bot
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Anuj Dutta
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-11-20 00:00:00 +05:30
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Simple gem to reduce the spam attacks on sign-up, sign-in and other similar sorts of forms.
17
+ email: anuj@andhapp.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.markdown
25
+ files:
26
+ - .gitignore
27
+ - LICENSE
28
+ - README.markdown
29
+ - Rakefile
30
+ - VERSION
31
+ - init.rb
32
+ - lib/is_bot.rb
33
+ - lib/is_bot/is_bot.rb
34
+ - spec/database.yml
35
+ - spec/is_bot_spec.rb
36
+ - spec/models.rb
37
+ - spec/schema.db
38
+ - spec/spec.opts
39
+ - spec/spec_helper.rb
40
+ has_rdoc: true
41
+ homepage: http://github.com/andhapp/is_bot
42
+ licenses: []
43
+
44
+ post_install_message:
45
+ rdoc_options:
46
+ - --charset=UTF-8
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: "0"
60
+ version:
61
+ requirements: []
62
+
63
+ rubyforge_project:
64
+ rubygems_version: 1.3.5
65
+ signing_key:
66
+ specification_version: 3
67
+ summary: Fight the bot.
68
+ test_files:
69
+ - spec/is_bot_spec.rb
70
+ - spec/models.rb
71
+ - spec/spec_helper.rb