smart_capcha 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "shoulda", ">= 0"
10
+ gem "bundler", "~> 1.0.0"
11
+ gem "jeweler", "~> 1.5.2"
12
+ gem "rcov", ">= 0"
13
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,20 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ git (1.2.5)
5
+ jeweler (1.5.2)
6
+ bundler (~> 1.0.0)
7
+ git (>= 1.2.5)
8
+ rake
9
+ rake (0.8.7)
10
+ rcov (0.9.9)
11
+ shoulda (2.11.3)
12
+
13
+ PLATFORMS
14
+ ruby
15
+
16
+ DEPENDENCIES
17
+ bundler (~> 1.0.0)
18
+ jeweler (~> 1.5.2)
19
+ rcov
20
+ shoulda
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Viacheslav Khrenov
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = smart_capcha
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to smart_capcha
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
9
+ * Fork the project
10
+ * Start a feature/bugfix branch
11
+ * Commit and push until you are happy with your contribution
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2010 Viacheslav Khrenov. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "smart_capcha"
16
+ gem.homepage = "http://github.com/khrvi/smart_capcha"
17
+ gem.license = "MIT"
18
+ gem.summary = %Q{ - Simple smart capcha - }
19
+ gem.description = %Q{ longer description of your gem}
20
+ gem.email = "khrviach@gmail.com"
21
+ gem.authors = ["Viacheslav Khrenov"]
22
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
23
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
24
+ # gem.add_runtime_dependency 'jabber4r', '> 0.1'
25
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rake/testtask'
30
+ Rake::TestTask.new(:test) do |test|
31
+ test.libs << 'lib' << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+
36
+ require 'rcov/rcovtask'
37
+ Rcov::RcovTask.new do |test|
38
+ test.libs << 'test'
39
+ test.pattern = 'test/**/test_*.rb'
40
+ test.verbose = true
41
+ end
42
+
43
+ task :default => :test
44
+
45
+ require 'rake/rdoctask'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "smart_capcha #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.2
@@ -0,0 +1,4 @@
1
+ Creates a template config file for the smart_capcha gem.
2
+ To generate config file should be used the following command:
3
+ rails 2.x: script/generate questionnaire_config
4
+ rails 3: rails generate questionnaire_config
@@ -0,0 +1,8 @@
1
+ class QuestionnaireConfigGenerator < Rails::Generator::Base
2
+ def manifest
3
+ record do |m|
4
+ m.file "config/questionnaire.yml", "config/questionnaire.yml"
5
+ m.readme "../../../README"
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,14 @@
1
+ # General setting
2
+ interval: 100
3
+ salt: 9ac056a151ed812a6076cfd4aefc364a7cc43a2a
4
+ failure_message: Your captcha answer is incorrect - please try again.
5
+
6
+ # Helper settings
7
+ label_text: Anti-spam question:
8
+ questions:
9
+ en:
10
+ 'What is $1 plus $2?': '$1+$2'
11
+ '$1 times $2 is what?': '$1*$2'
12
+ 'What is the number before $1?': '$1-1'
13
+ '$1 divided by $2 is what?': '$1/$2'
14
+ 'What is $1 minus $2?': '$1-$2'
@@ -0,0 +1,44 @@
1
+ # Delegate to Integer to allow translating numbers to English words
2
+ # Credit goes to Ruby Quiz #25 http://www.rubyquiz.com/quiz25.html
3
+ # and Glenn Parker's solution http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/135449
4
+ require 'delegate'
5
+
6
+ class HumaneInteger < DelegateClass(Integer)
7
+ Ones = %w[ zero one two three four five six seven eight nine ]
8
+ Teen = %w[ ten eleven twelve thirteen fourteen fifteen
9
+ sixteen seventeen eighteen nineteen ]
10
+ Tens = %w[ zero ten twenty thirty forty fifty
11
+ sixty seventy eighty ninety ]
12
+ Mega = %w[ none thousand million billion ]
13
+
14
+ def to_english
15
+ places = to_s.split(//).collect {|s| s.to_i}.reverse
16
+ name = []
17
+ ((places.length + 2) / 3).times do |p|
18
+ strings = self.class.trio(places[p * 3, 3])
19
+ name.push(Mega[p]) if strings.length > 0 and p > 0
20
+ name += strings
21
+ end
22
+ name.push(Ones[0]) unless name.length > 0
23
+ name.reverse.join(" ")
24
+ end
25
+
26
+ private
27
+
28
+ def self.trio(places)
29
+ strings = []
30
+ if places[1] == 1
31
+ strings.push(Teen[places[0]])
32
+ elsif places[1] and places[1] > 0
33
+ strings.push(places[0] == 0 ? Tens[places[1]] :
34
+ "#{Tens[places[1]]}-#{Ones[places[0]]}")
35
+ elsif places[0] > 0
36
+ strings.push(Ones[places[0]])
37
+ end
38
+ if places[2] and places[2] > 0
39
+ strings.push("hundred", Ones[places[2]])
40
+ end
41
+ strings
42
+ end
43
+
44
+ end
@@ -0,0 +1,29 @@
1
+ require 'humane_integer'
2
+
3
+ # Simple model to hold sets of questions and answers.
4
+ class Questionnaire
5
+
6
+ def self.config
7
+ @@config ||= begin
8
+ file = File.join(Rails.root, "config/questionnaire.yml")
9
+ YAML::load(File.open(file))
10
+ end
11
+ rescue
12
+ raise "Config file is not found. Please use 'rails g questionnaire_config'."
13
+ end
14
+
15
+ def self.find_random_question
16
+ questions = config['questions']['en']#[I18n.locale.to_s]
17
+ q = questions.keys[rand(questions.size)].dup
18
+ interval = (config['interval'] || 50).to_i
19
+ formula = questions[q].dup
20
+ first_number = rand(interval)
21
+ last_number = rand(interval)
22
+ formula.gsub!('$1', first_number.to_s)
23
+ formula.gsub!('$2', last_number.to_s)
24
+ result = eval(formula)
25
+ q.gsub!('$1', HumaneInteger.new(first_number).to_english)
26
+ q.gsub!('$2', HumaneInteger.new(last_number).to_english)
27
+ [q, result]
28
+ end
29
+ end
@@ -0,0 +1,39 @@
1
+ require 'digest/sha2'
2
+ require 'questionnaire'
3
+ require 'smart_capcha_helper'
4
+ module SmartCapcha
5
+ module Controller
6
+
7
+ def self.included(obj)
8
+ obj.helper_method :captcha_passed?, :prepare_capcha_question
9
+ obj.helper SmartCapcha::Helper
10
+ end
11
+
12
+ def valid_smart_capcha?
13
+ return captcha_failure unless params[:captcha_answer]
14
+ captcha_passed? ? true : captcha_failure
15
+ end
16
+
17
+ def prepare_capcha_question
18
+ result = Questionnaire.find_random_question
19
+ session[:captcha_status] = encrypt(result.last)
20
+ result.first
21
+ end
22
+
23
+ def captcha_passed?
24
+ session[:captcha_status] == encrypt(params[:captcha_answer])
25
+ end
26
+
27
+ def captcha_failure
28
+ flash[:capcha_error] = Questionnaire.config[:failure_message]
29
+ false
30
+ end
31
+
32
+ private
33
+
34
+ def encrypt(answer)
35
+ Digest::SHA512.hexdigest("--#{answer}--#{Questionnaire.config[:salt]}--")
36
+ end
37
+ end
38
+ end
39
+ ActionController::Base.send :include, SmartCapcha::Controller
@@ -0,0 +1,13 @@
1
+ module SmartCapcha
2
+ module Helper
3
+ def smart_capcha
4
+ result = ''
5
+ result << "<span id='smart_capcha'>"
6
+ result << "<label for='captcha_answer'>"
7
+ result << "#{Questionnaire.config[:label_text]} #{prepare_capcha_question}</label>"
8
+ result << text_field_tag(:captcha_answer, params[:captcha_answer], :size => 30)
9
+ result << "</span>"
10
+ result.html_safe
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,69 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{smart_capcha}
8
+ s.version = "0.1.2"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Viacheslav Khrenov"]
12
+ s.date = %q{2011-03-13}
13
+ s.description = %q{ longer description of your gem}
14
+ s.email = %q{khrviach@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ "Gemfile",
22
+ "Gemfile.lock",
23
+ "LICENSE.txt",
24
+ "README.rdoc",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "generators/questionnaire_config/USAGE",
28
+ "generators/questionnaire_config/questionnaire_config_generator.rb",
29
+ "generators/questionnaire_config/templates/config/questionnaire.yml",
30
+ "lib/humane_integer.rb",
31
+ "lib/questionnaire.rb",
32
+ "lib/smart_capcha.rb",
33
+ "lib/smart_capcha_helper.rb",
34
+ "smart_capcha.gemspec",
35
+ "test/helper.rb",
36
+ "test/test_let-me-think.rb"
37
+ ]
38
+ s.homepage = %q{http://github.com/khrvi/smart_capcha}
39
+ s.licenses = ["MIT"]
40
+ s.require_paths = ["lib"]
41
+ s.rubygems_version = %q{1.5.3}
42
+ s.summary = %q{- Simple smart capcha -}
43
+ s.test_files = [
44
+ "test/helper.rb",
45
+ "test/test_let-me-think.rb"
46
+ ]
47
+
48
+ if s.respond_to? :specification_version then
49
+ s.specification_version = 3
50
+
51
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
52
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
53
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
54
+ s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
55
+ s.add_development_dependency(%q<rcov>, [">= 0"])
56
+ else
57
+ s.add_dependency(%q<shoulda>, [">= 0"])
58
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
59
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
60
+ s.add_dependency(%q<rcov>, [">= 0"])
61
+ end
62
+ else
63
+ s.add_dependency(%q<shoulda>, [">= 0"])
64
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
65
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
66
+ s.add_dependency(%q<rcov>, [">= 0"])
67
+ end
68
+ end
69
+
data/test/helper.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'smart_capcha'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestSmartCapcha < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,144 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: smart_capcha
3
+ version: !ruby/object:Gem::Version
4
+ hash: 31
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 2
10
+ version: 0.1.2
11
+ platform: ruby
12
+ authors:
13
+ - Viacheslav Khrenov
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-03-13 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: shoulda
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :development
33
+ prerelease: false
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: bundler
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ~>
41
+ - !ruby/object:Gem::Version
42
+ hash: 23
43
+ segments:
44
+ - 1
45
+ - 0
46
+ - 0
47
+ version: 1.0.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: *id002
51
+ - !ruby/object:Gem::Dependency
52
+ name: jeweler
53
+ requirement: &id003 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ~>
57
+ - !ruby/object:Gem::Version
58
+ hash: 7
59
+ segments:
60
+ - 1
61
+ - 5
62
+ - 2
63
+ version: 1.5.2
64
+ type: :development
65
+ prerelease: false
66
+ version_requirements: *id003
67
+ - !ruby/object:Gem::Dependency
68
+ name: rcov
69
+ requirement: &id004 !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ hash: 3
75
+ segments:
76
+ - 0
77
+ version: "0"
78
+ type: :development
79
+ prerelease: false
80
+ version_requirements: *id004
81
+ description: " longer description of your gem"
82
+ email: khrviach@gmail.com
83
+ executables: []
84
+
85
+ extensions: []
86
+
87
+ extra_rdoc_files:
88
+ - LICENSE.txt
89
+ - README.rdoc
90
+ files:
91
+ - .document
92
+ - Gemfile
93
+ - Gemfile.lock
94
+ - LICENSE.txt
95
+ - README.rdoc
96
+ - Rakefile
97
+ - VERSION
98
+ - generators/questionnaire_config/USAGE
99
+ - generators/questionnaire_config/questionnaire_config_generator.rb
100
+ - generators/questionnaire_config/templates/config/questionnaire.yml
101
+ - lib/humane_integer.rb
102
+ - lib/questionnaire.rb
103
+ - lib/smart_capcha.rb
104
+ - lib/smart_capcha_helper.rb
105
+ - smart_capcha.gemspec
106
+ - test/helper.rb
107
+ - test/test_let-me-think.rb
108
+ has_rdoc: true
109
+ homepage: http://github.com/khrvi/smart_capcha
110
+ licenses:
111
+ - MIT
112
+ post_install_message:
113
+ rdoc_options: []
114
+
115
+ require_paths:
116
+ - lib
117
+ required_ruby_version: !ruby/object:Gem::Requirement
118
+ none: false
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ hash: 3
123
+ segments:
124
+ - 0
125
+ version: "0"
126
+ required_rubygems_version: !ruby/object:Gem::Requirement
127
+ none: false
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ hash: 3
132
+ segments:
133
+ - 0
134
+ version: "0"
135
+ requirements: []
136
+
137
+ rubyforge_project:
138
+ rubygems_version: 1.5.3
139
+ signing_key:
140
+ specification_version: 3
141
+ summary: "- Simple smart capcha -"
142
+ test_files:
143
+ - test/helper.rb
144
+ - test/test_let-me-think.rb