math_captcha 0.0.2

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 25c2b24c99855e42d71b25daa5a8258862ec6ea9
4
+ data.tar.gz: 7ff04166b5f77bad2fd587b2916735bdcf25f80d
5
+ SHA512:
6
+ metadata.gz: 931becb25605c9391a150b9f03dbea19df1bacc3aa07a756f522f2d436270948f5491c3d157ce6f6a150df6f966dbaa9d97f876492fc91fe46839a77b11bb528
7
+ data.tar.gz: e1ce991e6673ac832ece621acb554964e405f4ff07a57d6f858ef8a9be962f77deee5a576b5b303e319e1c5fe214ced4707e27eabad1d70ae7bf0e1515bcc35f
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Tania
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,47 @@
1
+ # Math_captcha
2
+
3
+ Math_captcha helps you generating localized captchas for forms. It presents a simple math operation that should be performed by user in order to ensure she is a human.
4
+ This Gem is strongly inspired in visualcaptcha gem (https://github.com/kimenye/visualcaptcha)
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'math_captcha'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install math_captcha
19
+
20
+ ## Setup
21
+
22
+ After installation, follow these simple steps to setup the plugin. The setup will depend on the version of rails your application is using.
23
+
24
+ $ rails generate math_captcha
25
+
26
+ ## Usage
27
+
28
+ ###Controller Based
29
+
30
+ In the view file within the form tags add this code
31
+
32
+ <%= show_math_captcha %>
33
+
34
+ Add the following line in the file "app/controllers/application.rb"
35
+
36
+ ApplicationController < ActionController::Base
37
+ include MathCaptcha::ControllerHelpers
38
+ end
39
+
40
+
41
+ ## Contributing
42
+
43
+ 1. Fork it
44
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
45
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
46
+ 4. Push to the branch (`git push origin my-new-feature`)
47
+ 5. Create new Pull Request
@@ -0,0 +1,14 @@
1
+ require 'rails/generators'
2
+
3
+ class MathCaptchaGenerator < 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.erb", File.join('app/views', 'math_captcha', "_math_captcha.erb")
12
+ end
13
+ end
14
+
@@ -0,0 +1,11 @@
1
+ <div class="math-captcha clearfix <%%= captcha_options[:type] %>">
2
+ <span class="captcha-explanation <%%= captcha_options[:type] %>">
3
+ <%%= challenge.question %>
4
+ </span>
5
+ <input class='captcha-input' type='text' name='captcha-value' size='4'>
6
+ <div class="captcha-possibilities <%%= captcha_options[:type] %> clearfix">
7
+ <%% challenge.options.each do |option| %>
8
+ <img src="/assets/<%%= option.image %>" class="vc-<%%= option.encrypted %>" data-value="<%%= option.encrypted %>" />
9
+ <%% end %>
10
+ </div>
11
+ </div>
@@ -0,0 +1,103 @@
1
+ require "math_captcha/version"
2
+ require "math_captcha/engine"
3
+
4
+ module MathCaptcha
5
+ autoload :ViewHelper, 'math_captcha/view'
6
+ autoload :ControllerHelpers, 'math_captcha/controller'
7
+ autoload :Utils, 'math_captcha/utils'
8
+
9
+ class Challenge
10
+ attr_accessor :question, :answer, :encrypted, :options
11
+
12
+ def initialize(question, answer, encrypted, options=nil)
13
+ @question = question
14
+ @answer = answer
15
+ @encrypted = encrypted
16
+ @options = options
17
+ end
18
+ end
19
+
20
+ class Captcha
21
+ def initialize
22
+ @numbers = [0,1,2,3,4,5,6,7,8,9]
23
+ @numbers_alpha = {
24
+ 'es' => {
25
+ 0 => 'cero',
26
+ 1 => 'uno',
27
+ 2 => 'dos',
28
+ 3 => 'tres',
29
+ 4 => 'cuatro',
30
+ 5 => 'cinco',
31
+ 6 => 'seis',
32
+ 7 => 'siete',
33
+ 8 => 'ocho',
34
+ 9 => 'nueve'
35
+ },
36
+ 'eu' => {
37
+ 0 => 'huts',
38
+ 1 => 'bat',
39
+ 2 => 'bi',
40
+ 3 => 'hiru',
41
+ 4 => 'lau',
42
+ 5 => 'bost',
43
+ 6 => 'sei',
44
+ 7 => 'zazpi',
45
+ 8 => 'zortzi',
46
+ 9 => 'bederatzi'
47
+ },
48
+ 'en' => {
49
+ 0 => 'zero',
50
+ 1 => 'one',
51
+ 2 => 'two',
52
+ 3 => 'three',
53
+ 4 => 'four',
54
+ 5 => 'five',
55
+ 6 => 'six',
56
+ 7 => 'seven',
57
+ 8 => 'eight',
58
+ 9 => 'nine'
59
+ }
60
+ }
61
+
62
+ @ops = {
63
+ 'es' => {
64
+ '+' => 'más',
65
+ '-' => 'menos',
66
+ '*' => 'por',
67
+ '/' => 'entre'
68
+ },
69
+ 'eu' => {
70
+ '+' => 'gehi',
71
+ '-' => 'ken',
72
+ '*' => 'bider',
73
+ '/' => 'zati'
74
+ },
75
+ 'en' => {
76
+ '+' => 'plus',
77
+ '-' => 'minus',
78
+ '*' => 'times',
79
+ '/' => 'divided by'
80
+ }
81
+ }
82
+ end
83
+
84
+ def locale
85
+ return @locale unless @locale.nil?
86
+ @locale = I18n.locale.to_s || I18n.default_locale.to_s || 'es'
87
+ end
88
+
89
+ def build_challenge(options={})
90
+ digit1 = @numbers[rand(9)]
91
+ digit2 = @numbers[rand(9)]
92
+ op = @ops[locale].keys[rand(4)]
93
+
94
+
95
+ value = eval("#{digit1}#{op}#{digit2}")
96
+ encrypted = MathCaptcha::Utils.generate_key(value)
97
+ question = "#{@numbers_alpha[locale][digit1]} #{@ops[locale][op]} #{@numbers_alpha[locale][digit2]}"
98
+ challenge = Challenge.new(question, value, encrypted, options)
99
+
100
+ return challenge
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,16 @@
1
+ module MathCaptcha
2
+ module ControllerHelpers
3
+ def math_captcha_valid?
4
+ return true if Rails.env.test?
5
+ if params["captcha-value"]
6
+ session_challenge = session[:captcha]
7
+ crypted_challenge = MathCaptcha::Utils.generate_key(params['captcha-value'])
8
+ result = crypted_challenge === session_challenge
9
+ pr "result #{result}"
10
+ return result
11
+ else
12
+ return false
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,10 @@
1
+ require 'rails'
2
+ require 'math_captcha'
3
+
4
+ module MathCaptcha
5
+ class Engine < ::Rails::Engine
6
+ config.after_initialize do
7
+ ActionView::Base.send(:include, MathCaptcha::ViewHelper)
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,11 @@
1
+ require 'digest/sha1'
2
+
3
+ module MathCaptcha #:nodoc
4
+ module Utils #:nodoc
5
+ def self.generate_key(*args)
6
+ args << 'math-captcha'
7
+ Digest::SHA1.hexdigest(args.join)
8
+ end
9
+ end
10
+ end
11
+
@@ -0,0 +1,3 @@
1
+ module MathCaptcha
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,33 @@
1
+ module MathCaptcha
2
+ module ViewHelper
3
+
4
+ def show_math_captcha(options = {})
5
+ options = sanitize_options(options)
6
+
7
+ #key = visual_captcha_key('captcha')
8
+ # captcha = VisualCaptcha::Captcha.new(number)
9
+ # challenge = captcha.build
10
+ captcha = MathCaptcha::Captcha.new
11
+ challenge = captcha.build_challenge
12
+
13
+ session[:captcha] = challenge.encrypted
14
+
15
+ render :partial => 'math_captcha/math_captcha', :locals => { :captcha_options => options, :challenge => challenge }
16
+ end
17
+
18
+ private
19
+
20
+ def sanitize_options(options)
21
+ options
22
+ end
23
+
24
+ # not used
25
+ # def math_captcha_key(key_name = nil)
26
+ # if key_name.nil?
27
+ # session[:captcha] ||= MathCaptcha::Utils.generate_key(session[:id].to_s, 'captcha')
28
+ # else
29
+ # MathCaptcha::Utils.generate_key(session[:id].to_s, key_name)
30
+ # end
31
+ # end
32
+ end
33
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: math_captcha
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Tania Rubio
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Simple lib to generate a math_captcha to prevent spammers in form. Supports
42
+ localization.
43
+ email:
44
+ - tania@efaber.net
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - LICENSE.txt
50
+ - README.md
51
+ - lib/generators/math_captcha_generator.rb
52
+ - lib/generators/templates/partial.erb
53
+ - lib/math_captcha.rb
54
+ - lib/math_captcha/controller.rb
55
+ - lib/math_captcha/engine.rb
56
+ - lib/math_captcha/utils.rb
57
+ - lib/math_captcha/version.rb
58
+ - lib/math_captcha/view.rb
59
+ homepage: http://toadd.mathcaptcha.com
60
+ licenses:
61
+ - MIT
62
+ metadata: {}
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubyforge_project:
79
+ rubygems_version: 2.2.2
80
+ signing_key:
81
+ specification_version: 4
82
+ summary: Add math_captcha in forms to prevent spammers
83
+ test_files: []