Skip to content

Commit

Permalink
Fix lib import
Browse files Browse the repository at this point in the history
  • Loading branch information
huacnlee committed Oct 12, 2022
1 parent c075f17 commit 2f0a6a7
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 32 deletions.
23 changes: 0 additions & 23 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,3 @@ task :preview do
warn res[0]
puts res[1].pack("c*")
end

task :memory do
require "rucaptcha"
puts "Starting to profile memory..."
b = {}
puts "Before => #{GC.stat(b)[:heap_live_slots]}"
count = 10_000_000
step = (count / 100).to_i
count.times do |i|
res = RuCaptcha.generate
print_memory if i % step == 0
end

print_memory
puts GC.start
puts "After GC"
print_memory
end

def print_memory
rss = `ps -eo pid,rss | grep #{Process.pid} | awk '{print $2}'`.to_i
puts "rss: #{rss} live objects #{GC.stat[:heap_live_slots]}, total allocated: #{GC.stat[:total_allocated_objects]}"
end
1 change: 1 addition & 0 deletions ext/rucaptcha/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions ext/rucaptcha/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ crate-type = ["cdylib"]
[dependencies]
captcha = "*"
magnus = "0.3"
rand = "0.8.5"
30 changes: 24 additions & 6 deletions ext/rucaptcha/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,43 @@
use captcha::filters::{Cow, Dots, Noise, Wave};
use captcha::filters::{Cow, Dots, Grid, Noise, Wave};
use captcha::{Captcha, Geometry};
use magnus::{define_class, function, Error, Object};

// https://coolors.co/cc0b8f-7c0abe-5700c8-3c2ea4-3d56a8-3fa67e-45bb30-69d003-a0d003-d8db02
const colors: [(u8, u8, u8); 10] = [
(204, 11, 143),
(124, 10, 190),
(87, 0, 200),
(60, 46, 164),
(61, 86, 168),
(63, 166, 126),
(69, 187, 48),
(105, 208, 3),
(160, 208, 3),
(216, 219, 2),
];

pub fn create(style: u32, len: u32, line: bool, noise: bool) -> (String, Vec<u8>) {
let mut c = Captcha::new();
c.add_chars(len).view(220, 120);

if style == 1 {
let color = colors[rand::random::<usize>() % colors.len()];
c.set_color([color.0, color.1, color.2]);
}

if line {
c.apply_filter(
c.apply_filter(Wave::new(2.0, 10.0)).apply_filter(
Cow::new()
.min_radius(40)
.min_radius(30)
.max_radius(50)
.circles(1)
.area(Geometry::new(40, 150, 50, 70)),
);
}

if noise {
c.apply_filter(Noise::new(0.2))
.apply_filter(Wave::new(2.0, 12.0))
.apply_filter(Dots::new(2));
c.apply_filter(Noise::new(0.4))
.apply_filter(Grid::new(10, 10));
}

let chars = c.chars_as_string();
Expand Down
10 changes: 9 additions & 1 deletion lib/rucaptcha.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
require "rails"
require "action_controller"
require "active_support/all"
require "rucaptcha/rucaptcha"

begin
# load the precompiled extension file
ruby_version = /(\d+\.\d+)/.match(::RUBY_VERSION)
require_relative "rucaptcha/#{ruby_version}/rucaptcha"
rescue LoadError
require "rucaptcha/rucaptcha"
end

require "rucaptcha/version"
require "rucaptcha/configuration"
require "rucaptcha/controller_helpers"
Expand Down
4 changes: 2 additions & 2 deletions lib/rucaptcha/view_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ def rucaptcha_input_tag(opts = {})
opts[:type] = "text"
opts[:autocorrect] = "off"
opts[:autocapitalize] = "off"
opts[:pattern] = "[a-zA-Z]*"
opts[:pattern] = "[a-zA-Z0-9]*"
opts[:autocomplete] = "off"
opts[:maxlength] = RuCaptcha.config.length
tag(:input, opts)
end

def rucaptcha_image_tag(opts = {})
@rucaptcha_image_tag__image_path_in_this_request ||= "#{ru_captcha.root_path}?t=#{Time.now.strftime("%s%L")}"
@rucaptcha_image_tag__image_path_in_this_request ||= "#{ru_captcha.root_path}?t=#{Time.now.strftime('%s%L')}"
opts[:class] = opts[:class] || "rucaptcha-image"
opts[:src] = @rucaptcha_image_tag__image_path_in_this_request
opts[:onclick] = "this.src = '#{ru_captcha.root_path}?t=' + Date.now();"
Expand Down

0 comments on commit 2f0a6a7

Please sign in to comment.