rcaptcha 0.1.0 → 0.2.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.
Files changed (2) hide show
  1. data/lib/rcaptcha.rb +23 -19
  2. metadata +1 -1
@@ -4,14 +4,14 @@ require 'RMagick'
4
4
  class Rcaptcha
5
5
  attr_reader :image
6
6
 
7
- def initialize(length, directory)
8
- @length = length
9
- @directory = directory
7
+ def initialize(options = {})
8
+ @length = options[:length] || 5
9
+ @directory = options[:dir] || "tmp"
10
10
  @file = ""
11
11
  end
12
12
 
13
- def valid?(my_captcha)
14
- @captcha == my_captcha
13
+ def valid?(text)
14
+ @captcha == text
15
15
  end
16
16
 
17
17
  def clean!
@@ -21,28 +21,32 @@ class Rcaptcha
21
21
  def generate
22
22
  clean!
23
23
 
24
- @captcha = SecureRandom.urlsafe_base64(@length)
24
+ @captcha = SecureRandom.hex[0...@length]
25
25
  create_image
26
26
  end
27
27
 
28
- # TODO: make images 'more twisted' to make it difficult to break
29
28
  def create_image
30
29
  filename = "#{SecureRandom.urlsafe_base64}.png"
31
30
  @image = "#{filename}"
32
31
  @file = "#{File.expand_path(@directory)}/#{filename}"
33
32
 
34
- granite = Magick::ImageList.new('granite:')
35
- canvas = Magick::ImageList.new
36
- canvas.new_image(180, 60, Magick::TextureFill.new(granite))
37
- text = Magick::Draw.new
38
- text.font_family = 'Monaco'
39
- text.pointsize = 42
40
- text.gravity = Magick::CenterGravity
41
-
42
- text.annotate(canvas, 0,0,2,2, @captcha) { self.fill = 'gray83' }
43
- text.annotate(canvas, 0,0,-1.5,-1.5, @captcha) { self.fill = 'gray40' }
44
- text.annotate(canvas, 0,0,0,0, @captcha) { self.fill = 'darkred' }
33
+ canvas = Magick::Image.new(30 * @length, 60, Magick::HatchFill.new('#ffffff','#4169e1'))
45
34
 
46
- canvas.write(@file)
35
+ text = Magick::Draw.new
36
+ text.annotate(canvas, 100, 50, 30, 5, @captcha) do
37
+ self.fill = "#000000"
38
+ self.stroke = "transparent"
39
+ self.pointsize = 32
40
+ self.font_weight = Magick::BoldWeight
41
+ self.gravity = Magick::SouthGravity
42
+ end
43
+
44
+ canvas = canvas.implode(-0.4)
45
+ data = canvas.to_blob { self.format = "jpg" }
46
+
47
+ f = File.new(@file, 'w')
48
+ f.write(data)
49
+ f.flush
50
+ f.close
47
51
  end
48
52
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rcaptcha
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: