kapchonka 0.0.1
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.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +3 -0
- data/Rakefile +32 -0
- data/lib/generators/kapchonka_generator.rb +21 -0
- data/lib/generators/templates/config.rb +14 -0
- data/lib/generators/templates/kapchonka.en.yml +4 -0
- data/lib/generators/templates/partial.slim +12 -0
- data/lib/kapchonka/controller_helper.rb +25 -0
- data/lib/kapchonka/engine.rb +26 -0
- data/lib/kapchonka/session_storage.rb +24 -0
- data/lib/kapchonka/test_storage.rb +25 -0
- data/lib/kapchonka/utils.rb +44 -0
- data/lib/kapchonka/version.rb +5 -0
- data/lib/kapchonka/view_helper.rb +12 -0
- data/lib/kapchonka.rb +76 -0
- metadata +129 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0f8900bee1ff49fe2f0a4aa08c247425b3c474a3
|
4
|
+
data.tar.gz: bea04f5fedbf11ba453a73265e0e6396a86d03b3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4ee823ede479da76daf144cd5d6159886656198091aff81d8a9dd016df2b00cbb43655688d2fd2ef82d66ebe5e7ccd47b1fbdc09c6691a73be4e76b110110b84
|
7
|
+
data.tar.gz: 6130c8285f78de21c6826aead53d0c3510131731304b48c1e08d35d55c540625b27ced68fe1d77826130834b50725910e6b167e5a484867d58aaed5aa390286e
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2014 YOURNAME
|
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
data/Rakefile
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'Kapchonka'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.rdoc')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
|
20
|
+
Bundler::GemHelper.install_tasks
|
21
|
+
|
22
|
+
require 'rake/testtask'
|
23
|
+
|
24
|
+
Rake::TestTask.new(:test) do |t|
|
25
|
+
t.libs << 'lib'
|
26
|
+
t.libs << 'test'
|
27
|
+
t.pattern = 'test/**/*_test.rb'
|
28
|
+
t.verbose = false
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
task default: :test
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
|
3
|
+
class KapchonkaGenerator < Rails::Generators::Base
|
4
|
+
include Rails::Generators::Migration
|
5
|
+
|
6
|
+
def self.source_root
|
7
|
+
@source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'templates/'))
|
8
|
+
end
|
9
|
+
|
10
|
+
def create_partial
|
11
|
+
template 'partial.slim', File.join('app/views', 'shared', '_kapchonka_partial.slim')
|
12
|
+
end
|
13
|
+
|
14
|
+
def create_config
|
15
|
+
template 'config.rb', File.join('config/initializers', '','kapchonka.rb')
|
16
|
+
end
|
17
|
+
|
18
|
+
def create_locale
|
19
|
+
template 'kapchonka.en.yml', File.join('config/locales', '','kapchonka.en.yml')
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
Kapchonka.setup do |k|
|
2
|
+
k.store = :session
|
3
|
+
|
4
|
+
# k.image_width = 180
|
5
|
+
# k.image_height = 90
|
6
|
+
# k.font_family = 'Arial'
|
7
|
+
# k.font_size = 24
|
8
|
+
# k.font_size = 24
|
9
|
+
# k.font_color = 'black'
|
10
|
+
# k.stroke_width = 0
|
11
|
+
# k.stroke_color = 'transparent'
|
12
|
+
# k.operand = %w(+ - *)
|
13
|
+
# k.operant_max_number = 10
|
14
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
scss:
|
2
|
+
.kapchonka {
|
3
|
+
width: 30%; min-width: 300px;
|
4
|
+
}
|
5
|
+
|
6
|
+
.kapchonka
|
7
|
+
.kapchonka-image
|
8
|
+
img src="data:image/png;base64,#{image_data}"
|
9
|
+
.kapchonka-input
|
10
|
+
input type="number" name="#{input_name}" placeholder="#{I18n.t 'kapchonka.placeholder'}" required="required"
|
11
|
+
.kapchonka-label
|
12
|
+
= I18n.t 'kapchonka.label'
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Kapchonka
|
2
|
+
|
3
|
+
module ControllerHelper
|
4
|
+
|
5
|
+
def kapchonka_valid?
|
6
|
+
result = params[:kapchonka].to_s == Storage.get(:kapchonka_result)
|
7
|
+
destroy_kapchonka! if result
|
8
|
+
result
|
9
|
+
end
|
10
|
+
|
11
|
+
def generate_kapchonka
|
12
|
+
Storage.session = session if Kapchonka.store == :session
|
13
|
+
equation = Utils.generate_random_equation(rand(4) + 1)
|
14
|
+
Storage.save! :kapchonka_equation, equation
|
15
|
+
Storage.save! :kapchonka_result, eval(equation).to_s
|
16
|
+
end
|
17
|
+
|
18
|
+
def destroy_kapchonka!
|
19
|
+
Storage.remove! :kapchonka_equation
|
20
|
+
Storage.remove! :kapchonka_result
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'rails'
|
2
|
+
require 'kapchonka'
|
3
|
+
|
4
|
+
module Kapchonka
|
5
|
+
|
6
|
+
class Engine < ::Rails::Engine
|
7
|
+
|
8
|
+
config.before_initialize do
|
9
|
+
case Kapchonka.store
|
10
|
+
when :test
|
11
|
+
require 'kapchonka/test_storage.rb'
|
12
|
+
when :session
|
13
|
+
include ActionDispatch::Session
|
14
|
+
require 'kapchonka/session_storage.rb'
|
15
|
+
else
|
16
|
+
raise StandardError.new("Unknown store: #{Kapchonka.store}")
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
config.after_initialize do
|
21
|
+
ActionView::Base.send(:include, Kapchonka::ViewHelper)
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Kapchonka
|
2
|
+
|
3
|
+
class Storage
|
4
|
+
class << self
|
5
|
+
|
6
|
+
attr_accessor :session
|
7
|
+
|
8
|
+
def save! key, value
|
9
|
+
session[key] = value
|
10
|
+
end
|
11
|
+
|
12
|
+
def get key
|
13
|
+
session[key]
|
14
|
+
end
|
15
|
+
|
16
|
+
def remove! key
|
17
|
+
session[key] = nil
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Kapchonka
|
2
|
+
|
3
|
+
class Storage
|
4
|
+
|
5
|
+
class << self
|
6
|
+
|
7
|
+
attr_accessor :test_storage
|
8
|
+
|
9
|
+
def save! key, value
|
10
|
+
@test_storage = { key => value }
|
11
|
+
end
|
12
|
+
|
13
|
+
def get key
|
14
|
+
@test_storage[key]
|
15
|
+
end
|
16
|
+
|
17
|
+
def remove! key
|
18
|
+
@test_storage = nil
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'base64'
|
2
|
+
require 'RMagick'
|
3
|
+
include Magick
|
4
|
+
|
5
|
+
|
6
|
+
module Kapchonka
|
7
|
+
|
8
|
+
module Utils
|
9
|
+
|
10
|
+
def self.generate_random_equation step_count = 1
|
11
|
+
equation = []
|
12
|
+
step_count.times do |i|
|
13
|
+
equation << rand(Kapchonka.operant_max_number) if i == 0
|
14
|
+
equation << Kapchonka.operands[rand(Kapchonka.operands.size)]
|
15
|
+
equation << rand(Kapchonka.operant_max_number)
|
16
|
+
end
|
17
|
+
equation.join(' ')
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.generate_kapchonka_image_base64(captcha_key)
|
21
|
+
final_image = Magick::Image.new(Kapchonka.image_width, Kapchonka.image_height) { self.format = 'PNG' }
|
22
|
+
text = Magick::Image.new(Kapchonka.image_width, Kapchonka.image_height)
|
23
|
+
|
24
|
+
gc = Magick::Draw.new
|
25
|
+
gc.annotate(text, 0,0,0,0, captcha_key) do
|
26
|
+
gc.gravity = CenterGravity
|
27
|
+
self.fill = Kapchonka.font_color
|
28
|
+
self.pointsize = Kapchonka.font_size
|
29
|
+
self.font_family = Kapchonka.font_family
|
30
|
+
self.font_weight = BoldWeight
|
31
|
+
if Kapchonka.stroke_width > 0
|
32
|
+
self.stroke_width = Kapchonka.stroke_width
|
33
|
+
self.stroke = Kapchonka.stroke_color
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
final_image.composite!(text.rotate(rand(45)).wave( rand(50), 200 + rand(100)), CenterGravity, HardLightCompositeOp)
|
38
|
+
|
39
|
+
Base64.encode64(final_image.to_blob).gsub("\n",'')
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module Kapchonka
|
2
|
+
|
3
|
+
module ViewHelper
|
4
|
+
|
5
|
+
def show_kapchonka
|
6
|
+
image_data = Utils.generate_kapchonka_image_base64 Storage.get(:kapchonka_equation)
|
7
|
+
render partial: 'shared/kapchonka_partial', locals: { image_data: image_data, input_name: 'kapchonka' }
|
8
|
+
end
|
9
|
+
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
data/lib/kapchonka.rb
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
require('active_support/all')
|
2
|
+
|
3
|
+
module Kapchonka
|
4
|
+
|
5
|
+
autoload :Utils, 'kapchonka/utils'
|
6
|
+
autoload :ViewHelper, 'kapchonka/view_helper'
|
7
|
+
autoload :ControllerHelper, 'kapchonka/controller_helper'
|
8
|
+
|
9
|
+
mattr_accessor :store
|
10
|
+
mattr_accessor :image_width
|
11
|
+
mattr_accessor :image_height
|
12
|
+
mattr_accessor :font_family
|
13
|
+
mattr_accessor :font_size
|
14
|
+
mattr_accessor :font_color
|
15
|
+
mattr_accessor :stroke_width
|
16
|
+
mattr_accessor :stroke_color
|
17
|
+
mattr_accessor :operands
|
18
|
+
mattr_accessor :operant_max_number
|
19
|
+
|
20
|
+
|
21
|
+
def self.setup
|
22
|
+
yield self
|
23
|
+
end
|
24
|
+
|
25
|
+
class << self
|
26
|
+
|
27
|
+
def image_width
|
28
|
+
@image_width || 180
|
29
|
+
end
|
30
|
+
|
31
|
+
def image_height
|
32
|
+
@image_height || 90
|
33
|
+
end
|
34
|
+
|
35
|
+
def font_family
|
36
|
+
@font_size || 'Arial'
|
37
|
+
end
|
38
|
+
|
39
|
+
def font_size
|
40
|
+
@font_size || 24
|
41
|
+
end
|
42
|
+
|
43
|
+
def font_color
|
44
|
+
@font_color || 'black'
|
45
|
+
end
|
46
|
+
|
47
|
+
def stroke_width
|
48
|
+
@stroke_width || 0
|
49
|
+
end
|
50
|
+
|
51
|
+
def stroke_color
|
52
|
+
@stroke_color || 'transparent'
|
53
|
+
end
|
54
|
+
|
55
|
+
def operant_max_number
|
56
|
+
@operant_max_number || 10
|
57
|
+
end
|
58
|
+
|
59
|
+
def operands
|
60
|
+
@operands || %w(+ - *)
|
61
|
+
end
|
62
|
+
|
63
|
+
def store
|
64
|
+
@store || :session
|
65
|
+
end
|
66
|
+
|
67
|
+
def store= type
|
68
|
+
require "kapchonka/#{type}_storage.rb" unless defined?(Rails)
|
69
|
+
@store = type
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
75
|
+
|
76
|
+
require 'kapchonka/engine' if defined?(Rails)
|
metadata
ADDED
@@ -0,0 +1,129 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: kapchonka
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Edgar Meinart
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-01-20 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 4.1.8
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 4.1.8
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: slim
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rmagick
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec-rails
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: Simple captcha
|
84
|
+
email:
|
85
|
+
- edgars@meinarts.name
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- MIT-LICENSE
|
91
|
+
- README.rdoc
|
92
|
+
- Rakefile
|
93
|
+
- lib/generators/kapchonka_generator.rb
|
94
|
+
- lib/generators/templates/config.rb
|
95
|
+
- lib/generators/templates/kapchonka.en.yml
|
96
|
+
- lib/generators/templates/partial.slim
|
97
|
+
- lib/kapchonka.rb
|
98
|
+
- lib/kapchonka/controller_helper.rb
|
99
|
+
- lib/kapchonka/engine.rb
|
100
|
+
- lib/kapchonka/session_storage.rb
|
101
|
+
- lib/kapchonka/test_storage.rb
|
102
|
+
- lib/kapchonka/utils.rb
|
103
|
+
- lib/kapchonka/version.rb
|
104
|
+
- lib/kapchonka/view_helper.rb
|
105
|
+
homepage: https://github.com/AlianceBloom/kapchonka
|
106
|
+
licenses:
|
107
|
+
- MIT
|
108
|
+
metadata: {}
|
109
|
+
post_install_message:
|
110
|
+
rdoc_options: []
|
111
|
+
require_paths:
|
112
|
+
- lib
|
113
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
119
|
+
requirements:
|
120
|
+
- - ">="
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: '0'
|
123
|
+
requirements: []
|
124
|
+
rubyforge_project:
|
125
|
+
rubygems_version: 2.2.2
|
126
|
+
signing_key:
|
127
|
+
specification_version: 4
|
128
|
+
summary: Simple captcha
|
129
|
+
test_files: []
|