Module: SimpleCaptcha::Utils

Defined in:
lib/simple_captcha/utils.rb

Overview

:nodoc

Class Method Summary collapse

Class Method Details

.generate_image_url(key, *args) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'lib/simple_captcha/utils.rb', line 60

def self.generate_image_url key, *args
  options = args.extract_options!

  defaults = {}
  defaults[:time] = options[:time] || Time.now.to_i

  base_url = args.first || ENV['RAILS_RELATIVE_URL_ROOT']

  url = "#{base_url}/simple_captcha?code=#{key}&#{defaults.to_query}"
end

.generate_key(*args) ⇒ Object



32
33
34
35
# File 'lib/simple_captcha/utils.rb', line 32

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

.generate_key_use_session(session, *args) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'lib/simple_captcha/utils.rb', line 71

def self.generate_key_use_session session, *args
  options = args.extract_options!
  key_name = args.first

  if key_name.nil?
    session[:captcha] ||= SimpleCaptcha::Utils.generate_key(session[:id].to_s, 'captcha')
  else
    SimpleCaptcha::Utils.generate_key(session[:id].to_s, key_name)
  end
end

.generate_simple_captcha_data(code) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/simple_captcha/utils.rb', line 46

def self.generate_simple_captcha_data(code)
  value = ''

  case code
  when 'numeric' then 
    SimpleCaptcha.length.times{value << (48 + rand(10)).chr}
  else
    SimpleCaptcha.length.times{value << (65 + rand(26)).chr}
  end

  return value
end

.run(cmd, params = "", expected_outcodes = 0) ⇒ Object

Execute command with params and return output if exit status equal expected_outcodes



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/simple_captcha/utils.rb', line 7

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

.set_simple_captcha_data(key, options = {}) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/simple_captcha/utils.rb', line 37

def self.set_simple_captcha_data(key, options={})
  code_type = options[:code_type]

  data = SimpleCaptcha::SimpleCaptchaData.get_data(key)
  data.value = generate_simple_captcha_data(code_type)
  data.save
  key
end

.simple_captcha_passed!(key) ⇒ Object

:nodoc



28
29
30
# File 'lib/simple_captcha/utils.rb', line 28

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

.simple_captcha_value(key) ⇒ Object

:nodoc



24
25
26
# File 'lib/simple_captcha/utils.rb', line 24

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