aritcaptcha 1.18.0 → 1.19.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.18.0
1
+ 1.19.0
data/aritcaptcha.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{aritcaptcha}
8
- s.version = "1.18.0"
8
+ s.version = "1.19.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Guilherme Nascimento"]
12
- s.date = %q{2010-07-05}
12
+ s.date = %q{2010-07-10}
13
13
  s.description = %q{Arithmetic Captcha}
14
14
  s.email = %q{guilherme.ruby@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -1,40 +1,71 @@
1
1
  module Aritcaptcha
2
2
 
3
3
  module AritcaptchaHelper
4
+ ADD = '+'
5
+ SUB = '-'
6
+ DIV = '/'
7
+ MUL = '*'
4
8
 
5
- def aritcaptcha_tag(options={})
6
- equation_key = Time.now.to_i
7
- operations = {:add => "+", :sub => "-", :mul => "*", :div => "/"}
9
+ OPERATIONS = [ADD, SUB, DIV, MUL]
10
+
11
+ def aritcaptcha_tag(options={})
12
+ equation_key = Time.now.to_i
13
+
14
+ default_operations = {:add => "+", :sub => "-", :mul => "*", :div => "/"}
8
15
 
9
16
  operator = nil
10
- if options[:op] == nil
11
- operator = operations.to_a[rand(operations.size)][1]
17
+ if options[:operations] == nil
18
+ operator = default_operations.to_a[rand(default_operations.size)][1]
12
19
  else
13
- non_default_operations = {}
14
- options[:op].each do |op|
15
- non_default_operations[op] = operations[op]
16
- end
17
- operator = non_default_operations.to_a[rand(non_default_operations.size)][1]
20
+ non_default_operations = {}
21
+ options[:operations].each do |op|
22
+ non_default_operations[op] = default_operations[op]
23
+ end
24
+ operator = non_default_operations.to_a[rand(non_default_operations.size)][1]
18
25
  end
19
26
 
20
- equation, result = Aritcaptcha::Calculation.generate_calculation options, 50, 50
21
- #session[:equation] = [equation_key, eval(equation)]
22
- puts "=========================================================="
23
- puts equation
24
- puts result
25
- puts "=========================================================="
27
+ equation, result = Aritcaptcha::Calculation.generate_calculation 50, 50, operator
26
28
 
27
- #options[:op] = operator
29
+ session[:equation] = [equation_key, eval(equation)]
28
30
 
29
31
  if options[:html]
30
32
  options = options[:html].inject([]){|dump, pair| dump << "#{pair[0]}=\"#{pair[1]}\""}
31
- options.join(" ")
32
- end
33
+ options = options.join(" ")
34
+ end
35
+
36
+ html = ""
37
+ if options[:format] == "image"
38
+ session[:image] = equation_key
39
+ format = generate_image equation_key, equation
40
+ html << "<img src=\"#{format}\" style='vertical-align:top;' /> <input type=\"text\" name=\"equation\" size=\"3\" style='vertical-align:top;' #{options} />"
41
+ else
42
+ html << "#{equation} = <input type=\"text\" name=\"equation\" style='vertical-align:top;' size=\"3\" #{options} /></div>"
43
+ end
44
+
45
+ html << "<input type=\"hidden\" name=\"equation_key\" value=\"#{equation_key}\" /> \n"
46
+ end
47
+
48
+ def generate_image(equation_key, equation)
49
+ relative_name = "#{equation_key}.png"
50
+ full_path = "#{Rails.root}/public/images/#{relative_name}"
33
51
 
34
- html = "#{equation} = <input type='text' name='equation' #{options} />"
35
- html << %{<input type="hidden" name="equation_key" value="#{equation_key}" />\n}
36
- html
52
+ unless File.file?(full_path)
53
+ image = Magick::Image.new(85, 25)
54
+ image.format = "PNG"
55
+ title = Magick::Draw.new
56
+
57
+ title.annotate(image, 5, 5, 5, 5, equation + " =") do
58
+ self.fill = "#333"
59
+ self.font = Rails.root + "/fonts/Clarenton LT Bold.ttf"
60
+ self.font_family = "Clarenton LT Bold"
61
+ self.font_weight = Magick::BoldWeight
62
+ self.gravity = Magick::NorthWestGravity
63
+ self.pointsize = 20
64
+ end
65
+ image.write(full_path)
66
+ end
67
+ url = image_path(relative_name)
37
68
  end
38
69
  end
39
70
 
40
- end
71
+ end
@@ -5,10 +5,20 @@ module Aritcaptcha
5
5
  SOLV = 1
6
6
 
7
7
  def verify_aritcaptcha(params)
8
+ clean session[:image]
9
+
8
10
  if (equation_key = params[:equation_key]) and (equation = params[:equation])
9
11
  return (session[:equation] and session[:equation][SOLV_KEY] == equation_key.to_i and session[:equation][SOLV] == equation.to_i)
10
12
  end
11
13
  end
14
+
15
+ def clean image
16
+ unless image == nil
17
+ relative_name = "#{image}.png"
18
+ full_path = "#{Rails.root}/public/images/#{relative_name}"
19
+ File.delete(full_path)
20
+ end
21
+ end
12
22
  end
13
23
 
14
- end
24
+ end
@@ -2,15 +2,15 @@ module Aritcaptcha
2
2
 
3
3
  class Calculation
4
4
 
5
- def self.generate_calculation number1, number2, operation
5
+ def generate_calculation number1, number2, operator
6
+ # Division by zero
7
+ return [nil, nil] if operator == "/" and number1.zero? or number2.zero?
8
+
6
9
  number1 = rand(number1)
7
10
  number2 = rand(number2)
8
- equation = "#{number1} #{operation} #{number2}"
9
-
10
- # Division by zero
11
- return [nil, nil] if operation == "/" and number1.zero? or number2.zero?
12
11
 
13
- result = eval("#{number1.to_f} #{operation} #{number2.to_f}")
12
+ equation = "#{number1} #{operator} #{number2}"
13
+ result = eval("#{number1.to_f} #{operator} #{number2.to_f}")
14
14
 
15
15
  [equation, result]
16
16
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aritcaptcha
3
3
  version: !ruby/object:Gem::Version
4
- hash: 95
4
+ hash: 91
5
5
  prerelease: false
6
6
  segments:
7
7
  - 1
8
- - 18
8
+ - 19
9
9
  - 0
10
- version: 1.18.0
10
+ version: 1.19.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Guilherme Nascimento
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-07-05 00:00:00 -03:00
18
+ date: 2010-07-10 00:00:00 -03:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency