kaptcha 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Gemfile +3 -0
- data/README.rdoc +5 -0
- data/Rakefile +29 -0
- data/lib/core/kaptcha.rb +87 -0
- data/lib/kaptcha.rb +7 -0
- metadata +69 -0
data/Gemfile
ADDED
data/README.rdoc
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require 'rubygems'
|
3
|
+
begin
|
4
|
+
require 'bundler/setup'
|
5
|
+
rescue LoadError
|
6
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
7
|
+
end
|
8
|
+
|
9
|
+
require 'rake'
|
10
|
+
require 'rake/rdoctask'
|
11
|
+
|
12
|
+
require 'rake/testtask'
|
13
|
+
|
14
|
+
Rake::TestTask.new(:test) do |t|
|
15
|
+
t.libs << 'lib'
|
16
|
+
t.libs << 'test'
|
17
|
+
t.pattern = 'test/**/*_test.rb'
|
18
|
+
t.verbose = false
|
19
|
+
end
|
20
|
+
|
21
|
+
task :default => :test
|
22
|
+
|
23
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
24
|
+
rdoc.rdoc_dir = 'rdoc'
|
25
|
+
rdoc.title = 'Captcha'
|
26
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
27
|
+
rdoc.rdoc_files.include('README.rdoc')
|
28
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
29
|
+
end
|
data/lib/core/kaptcha.rb
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
module Captcha
|
2
|
+
|
3
|
+
def self.included(base)
|
4
|
+
base.send(:extend, ClassMethods)
|
5
|
+
end
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
|
9
|
+
def has_captcha
|
10
|
+
send(:include, InstanceMethods)
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
|
15
|
+
module InstanceMethods
|
16
|
+
|
17
|
+
|
18
|
+
# @comment.human_captcha_desc
|
19
|
+
# %input{:type => 'text', :name => 'comment[captcha_form_result]', :autocomplete => :off, :style => 'width: 50px; display: inline;'}
|
20
|
+
# = f.hidden_field :captcha_code, :value => @comment.captcha_desc
|
21
|
+
|
22
|
+
# def __captcha__
|
23
|
+
# title = self.human_captcha_desc
|
24
|
+
|
25
|
+
# end
|
26
|
+
|
27
|
+
|
28
|
+
attr_accessor :captcha_code, :captcha_form_result
|
29
|
+
|
30
|
+
NUMBERS = (1..9).to_a
|
31
|
+
OPERATIONS = ['+', '-', '*']
|
32
|
+
|
33
|
+
def captcha_code
|
34
|
+
@@captcha_code.to_s ||= raise 'Error occurred.'
|
35
|
+
end
|
36
|
+
|
37
|
+
def initialize(params={}, &block)
|
38
|
+
begin
|
39
|
+
captcha_a = params[:captcha_code].split[0].to_i
|
40
|
+
rescue NoMethodError
|
41
|
+
captcha_a ||= NUMBERS[rand(NUMBERS.size)]
|
42
|
+
end
|
43
|
+
|
44
|
+
begin
|
45
|
+
captcha_b = params[:captcha_code].split[2].to_i
|
46
|
+
rescue NoMethodError
|
47
|
+
captcha_b ||= NUMBERS[rand(NUMBERS.size)]
|
48
|
+
end
|
49
|
+
|
50
|
+
begin
|
51
|
+
captcha_operation = params[:captcha_code].split[1]
|
52
|
+
rescue NoMethodError
|
53
|
+
captcha_operation ||= OPERATIONS[rand(OPERATIONS.size)]
|
54
|
+
end
|
55
|
+
|
56
|
+
case captcha_operation.to_s
|
57
|
+
when '-'
|
58
|
+
@@human_captcha_operation = 'минус'
|
59
|
+
when '+'
|
60
|
+
@@human_captcha_operation = 'плюс'
|
61
|
+
when '*'
|
62
|
+
@@human_captcha_operation = 'умножить на'
|
63
|
+
end
|
64
|
+
|
65
|
+
@@captcha_desc = "#{captcha_a} #{captcha_operation} #{captcha_b}"
|
66
|
+
@@human_captcha_desc = "Скажите, сколько будет #{captcha_a} #{@@human_captcha_operation} #{captcha_b} ?"
|
67
|
+
|
68
|
+
@@captcha_code = captcha_a.send(captcha_operation, captcha_b)
|
69
|
+
super(params, &block)
|
70
|
+
end
|
71
|
+
|
72
|
+
def validate
|
73
|
+
return errors.add(:captcha_code, 'Пожалуйста, проверьте контрольный вопрос.') unless captcha_form_result == captcha_code
|
74
|
+
true
|
75
|
+
end
|
76
|
+
|
77
|
+
def captcha_desc
|
78
|
+
@@captcha_desc ||= raise 'Error occurred.'
|
79
|
+
end
|
80
|
+
|
81
|
+
def human_captcha_desc
|
82
|
+
@@human_captcha_desc ||= raise 'Error occurred.'
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|
data/lib/kaptcha.rb
ADDED
metadata
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: kaptcha
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 25
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 3
|
10
|
+
version: 0.0.3
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Bogdan Kulbida
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2012-01-07 00:00:00 Z
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: Form protection with simple captcha calculations.
|
22
|
+
email:
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files: []
|
28
|
+
|
29
|
+
files:
|
30
|
+
- lib/core/kaptcha.rb
|
31
|
+
- lib/kaptcha.rb
|
32
|
+
- Rakefile
|
33
|
+
- Gemfile
|
34
|
+
- README.rdoc
|
35
|
+
homepage:
|
36
|
+
licenses: []
|
37
|
+
|
38
|
+
post_install_message:
|
39
|
+
rdoc_options: []
|
40
|
+
|
41
|
+
require_paths:
|
42
|
+
- lib
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
none: false
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
hash: 3
|
49
|
+
segments:
|
50
|
+
- 0
|
51
|
+
version: "0"
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
hash: 3
|
58
|
+
segments:
|
59
|
+
- 0
|
60
|
+
version: "0"
|
61
|
+
requirements: []
|
62
|
+
|
63
|
+
rubyforge_project:
|
64
|
+
rubygems_version: 1.8.15
|
65
|
+
signing_key:
|
66
|
+
specification_version: 3
|
67
|
+
summary: Simple form protection.
|
68
|
+
test_files: []
|
69
|
+
|