Sha256: 9f38d4690dfab76dfa8a1c39992ec24e3bbe21c9c2544dcce94446c01b2893bd

Contents?: true

Size: 1.35 KB

Versions: 17

Compression:

Stored size: 1.35 KB

Contents

require 'digest/sha1'

module SimpleCaptcha #:nodoc
  module Utils #:nodoc
    # Execute command with params and return output if exit status equal expected_outcodes
    def self.run(cmd, params = "", expected_outcodes = 0)
      command = %Q[#{cmd} #{params}].gsub(/\s+/, " ")
      command = "#{command} 2>&1"

      unless (image_magick_path = SimpleCaptcha.image_magick_path).blank?
        command = File.join(image_magick_path, command)
      end

      output = `#{command}`

      unless [expected_outcodes].flatten.include?($?.exitstatus)
        raise ::StandardError, "Error while running #{cmd}: #{output}"
      end

      output
    end

    def self.simple_captcha_value(key) #:nodoc
      SimpleCaptchaData.get_data(key).value rescue nil
    end

    def self.random_str()
      charset = SimpleCaptcha.charset.split(//)
      size = SimpleCaptcha.length

      (0...size).map{ charset.to_a[rand(charset.size)] }.join
    end

    def self.simple_captcha_new_value(key) #:nodoc
      begin
        d = SimpleCaptchaData.get_data(key)
        d.value = self.random_str
        d.save!
        d.value
      rescue
        nil
      end
    end

    def self.simple_captcha_passed!(key) #:nodoc
      SimpleCaptchaData.remove_data(key)
    end

    def self.generate_key(*args)
      args << Time.now.to_s
      Digest::SHA1.hexdigest(args.join)
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
glebtv-simple_captcha-0.8.1 lib/simple_captcha/utils.rb
glebtv-simple_captcha-0.8.0 lib/simple_captcha/utils.rb
glebtv-simple_captcha-0.7.0 lib/simple_captcha/utils.rb
glebtv-simple_captcha-0.6.7 lib/simple_captcha/utils.rb
glebtv-simple_captcha-0.6.5 lib/simple_captcha/utils.rb
glebtv-simple_captcha-0.6.4 lib/simple_captcha/utils.rb
glebtv-simple_captcha-0.6.3 lib/simple_captcha/utils.rb
glebtv-simple_captcha-0.6.2 lib/simple_captcha/utils.rb
glebtv-simple_captcha-0.6.1 lib/simple_captcha/utils.rb
glebtv-simple_captcha-0.6.0 lib/simple_captcha/utils.rb
glebtv-simple_captcha-0.5.2 lib/simple_captcha/utils.rb
glebtv-simple_captcha-0.5.1 lib/simple_captcha/utils.rb
glebtv-simple_captcha-0.4.6 lib/simple_captcha/utils.rb
glebtv-simple_captcha-0.4.5 lib/simple_captcha/utils.rb
glebtv-simple_captcha-0.4.4 lib/simple_captcha/utils.rb
glebtv-simple_captcha-0.4.3 lib/simple_captcha/utils.rb
glebtv-simple_captcha-0.4.1 lib/simple_captcha/utils.rb