Module: RuCaptcha

Defined in:
lib/rucaptcha.rb,
lib/rucaptcha/cache.rb,
lib/rucaptcha/engine.rb,
lib/rucaptcha/version.rb,
lib/rucaptcha/view_helpers.rb,
lib/rucaptcha/configuration.rb,
lib/rucaptcha/controller_helpers.rb,
lib/rucaptcha/errors/configuration.rb,
app/controllers/ru_captcha/captcha_controller.rb

Defined Under Namespace

Modules: ControllerHelpers, Errors, ViewHelpers Classes: CaptchaController, Configuration, Engine

Constant Summary collapse

VERSION =
"3.2.3"

Class Method Summary collapse

Class Method Details

.cacheObject



5
6
7
8
9
10
# File 'lib/rucaptcha/cache.rb', line 5

def cache
  return @cache if defined? @cache

  @cache = ActiveSupport::Cache.lookup_store(RuCaptcha.config.cache_store)
  @cache
end

.check_cache_store!Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/rucaptcha.rb', line 58

def check_cache_store!
  cache_store = RuCaptcha.config.cache_store
  store_name = cache_store.is_a?(Array) ? cache_store.first : cache_store
  if %i[memory_store null_store file_store].include?(store_name)
    RuCaptcha.config.cache_store = [:file_store, Rails.root.join("tmp/cache/rucaptcha/session")]

    puts "

  RuCaptcha's cache_store requirements are stored across processes and machines,
  such as :mem_cache_store, :redis_store, or other distributed storage.
  But your current set is #{cache_store}, it has changed to :file_store for working.
  NOTE: :file_store is still not a good way, it only works with single server case.

  Please make config file `config/initializers/rucaptcha.rb` to setup `cache_store`.
  More infomation please read GitHub RuCaptcha README file.
  https://github.com/huacnlee/rucaptcha

"
  end
end

.configObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rucaptcha.rb', line 23

def config
  return @config if defined?(@config)

  @config = Configuration.new
  @config.length = 5
  @config.difficulty = 3
  @config.expires_in = 2.minutes
  @config.skip_cache_store_check = false
  @config.line = true
  @config.noise = true
  @config.format = "png"
  @config.mount_path = "/rucaptcha"

  @config.cache_store = if Rails.application
                          Rails.application.config.cache_store
                        else
                          :mem_cache_store
                        end
  @config.cache_store
  @config
end

.configure(&block) ⇒ Object



45
46
47
# File 'lib/rucaptcha.rb', line 45

def configure(&block)
  config.instance_exec(&block)
end

.generateObject



49
50
51
52
53
54
55
56
# File 'lib/rucaptcha.rb', line 49

def generate
  length = config.length

  raise RuCaptcha::Errors::Configuration, "length config error, value must in 3..7" unless length.in?(3..7)

  result = RuCaptchaCore.create(length, config.difficulty || 5, config.line, config.noise, config.format)
  [result[0].downcase, result[1].pack("c*")]
end