idi_captcha_generator 0.1.1 → 0.2.0
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 +4 -4
- data/lib/idi_captcha_generator/version.rb +1 -1
- data/lib/idi_captcha_generator.rb +15 -16
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 311d16b144c38c8e6ceeb0d324b7ed35cdc91de4c232a63354e5bc0201aafb6d
|
4
|
+
data.tar.gz: d3480b05a4f54dc884b4d59f160ca34c0fdd1e6c5451745aaec2f0d442fc1e73
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d53d2db5dcafd8646c7803b6ffeaf731fe0d0a1342f35e32c0189531758651aac685bf260a03005cf08c62f46b0c76edb182c963356aee10059f515bb94d8ead
|
7
|
+
data.tar.gz: 8dba16bda4c07c04faae971a7395da80422d28ef2f3a759d2ceaeb0ed7f29966a5ce2d52d4e9f6c2913d6e950b63d0cd5a94fbd410686ec58735a8a2633f2d31
|
@@ -1,35 +1,34 @@
|
|
1
|
-
# lib/idi_captcha_generator.rb
|
2
1
|
# frozen_string_literal: true
|
3
2
|
|
4
3
|
require_relative "idi_captcha_generator/version"
|
5
|
-
require '
|
4
|
+
require 'mini_magick'
|
6
5
|
|
7
6
|
module CaptchaGenerator
|
8
7
|
def self.generate_captcha
|
8
|
+
# Randomly generate a simple math question
|
9
9
|
num1 = rand(1..10)
|
10
10
|
num2 = rand(1..10)
|
11
11
|
answer = num1 + num2
|
12
12
|
question = "#{num1} + #{num2} = ?"
|
13
13
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
14
|
+
# Create a white canvas using MiniMagick
|
15
|
+
image = MiniMagick::Image.create('png') do |f|
|
16
|
+
f.write MiniMagick::Image.new('canvas:white', size: '200x100').to_blob
|
17
|
+
end
|
18
|
+
|
19
|
+
# Annotate the captcha text on the image
|
20
|
+
image.combine_options do |c|
|
21
|
+
c.font 'Helvetica'
|
22
|
+
c.fill 'black'
|
23
|
+
c.pointsize '32'
|
24
|
+
c.gravity 'center'
|
25
|
+
c.draw "text 0,0 '#{question}'"
|
23
26
|
end
|
24
|
-
draw.draw(image)
|
25
27
|
|
26
28
|
# Save image to a temporary location
|
27
|
-
image_path =
|
29
|
+
image_path = File.join(Dir.tmpdir, "captcha_#{Time.now.to_i}.png")
|
28
30
|
image.write(image_path)
|
29
31
|
|
30
|
-
# Clean up temporary files after a certain period
|
31
|
-
File.delete(image_path) if File.exist?(image_path) && Time.now - File.mtime(image_path) > 60 # 1 minute expiry
|
32
|
-
|
33
32
|
{ question: question, answer: answer, image_path: image_path }
|
34
33
|
end
|
35
34
|
end
|