From 850eea67ef3b8ba83001b674b838ef0daf49718e Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Mon, 23 May 2016 15:04:27 +0800 Subject: [PATCH] Formate code --- lib/rucaptcha/cache.rb | 4 +--- lib/rucaptcha/captcha.rb | 15 ++++++--------- lib/rucaptcha/controller_helpers.rb | 2 +- lib/rucaptcha/view_helpers.rb | 12 ++++++------ 4 files changed, 14 insertions(+), 19 deletions(-) diff --git a/lib/rucaptcha/cache.rb b/lib/rucaptcha/cache.rb index d82b23c..35bc482 100644 --- a/lib/rucaptcha/cache.rb +++ b/lib/rucaptcha/cache.rb @@ -33,9 +33,7 @@ module RuCaptcha return @cache if defined?(@cache) cache_path = Rails.root.join('tmp', 'cache', 'rucaptcha') - if !File.exists? cache_path - FileUtils.mkdir_p(cache_path) - end + FileUtils.mkdir_p(cache_path) unless File.exist? cache_path @cache = ActiveSupport::Cache::FileStore.new(cache_path) @cache.clear @cache diff --git a/lib/rucaptcha/captcha.rb b/lib/rucaptcha/captcha.rb index 2b2cca9..05beded 100644 --- a/lib/rucaptcha/captcha.rb +++ b/lib/rucaptcha/captcha.rb @@ -37,16 +37,13 @@ module RuCaptcha full_width = font_size * chars.size size = "#{full_width}x#{full_height}" half_width = full_width / 2 - full_height = full_height - half_height = full_height / 2 text_top = 0 text_left = 0 - (font_size * 0.28).to_i - stroke_width = (font_size * 0.05).to_i + 1 + stroke_width = (font_size * 0.05).to_i + 1 text_width = font_size + text_left - label = "=#{' ' * (chars.size - 1)}=" + text_opts = [] + line_opts = [] - text_opts = [] - line_opts = [] chars.each_with_index do |char, i| rgb = random_color text_color = "rgba(#{rgb.join(',')}, 1)" @@ -76,14 +73,14 @@ module RuCaptcha command = "convert -size #{size} xc:White -gravity Center -weight 12 -pointsize 20 -annotate 0 \"#{code}\" -trim #{png_file_path}" require 'open3' _stdout_str, stderr_str = Open3.capture3(command) - raise "RuCaptcha: #{stderr_str.strip}" if stderr_str != nil && stderr_str.length > 0 + raise "RuCaptcha: #{stderr_str.strip}" if stderr_str.present? png_file_path else command.strip! - pid, stdin, stdout, stderr = POSIX::Spawn.popen4(command) + pid, _, stdout, stderr = POSIX::Spawn.popen4(command) Process.waitpid(pid) err = stderr.read - raise "RuCaptcha: #{err.strip}" if err != nil && err.length > 0 + raise "RuCaptcha: #{err.strip}" if err.present? stdout.read end end diff --git a/lib/rucaptcha/controller_helpers.rb b/lib/rucaptcha/controller_helpers.rb index e143515..ceb9c76 100644 --- a/lib/rucaptcha/controller_helpers.rb +++ b/lib/rucaptcha/controller_helpers.rb @@ -7,7 +7,7 @@ module RuCaptcha end def generate_rucaptcha - session[:_rucaptcha] = RuCaptcha::Captcha.random_chars + session[:_rucaptcha] = RuCaptcha::Captcha.random_chars session[:_rucaptcha_at] = Time.now.to_i RuCaptcha::Captcha.create(session[:_rucaptcha]) diff --git a/lib/rucaptcha/view_helpers.rb b/lib/rucaptcha/view_helpers.rb index f9fbec6..d491203 100644 --- a/lib/rucaptcha/view_helpers.rb +++ b/lib/rucaptcha/view_helpers.rb @@ -1,13 +1,13 @@ module RuCaptcha module ViewHelpers def rucaptcha_input_tag(opts = {}) - opts[:name] = '_rucaptcha' - opts[:type] = 'text' - opts[:autocorrect] = 'off' + opts[:name] = '_rucaptcha' + opts[:type] = 'text' + opts[:autocorrect] = 'off' opts[:autocapitalize] = 'off' - opts[:pattern] = '[0-9a-z]*' - opts[:maxlength] = RuCaptcha.config.len - opts[:autocomplete] = 'off' + opts[:pattern] = '[0-9a-z]*' + opts[:maxlength] = RuCaptcha.config.len + opts[:autocomplete] = 'off' tag(:input, opts) end