Sha256: 7f93aeee358e46aa48a360d444ecf39ae42ac1b23fb8714b657cb222f2d6f27e

Contents?: true

Size: 1.93 KB

Versions: 3

Compression:

Stored size: 1.93 KB

Contents

require 'invisible_captcha/version'
require 'invisible_captcha/controller_ext'
require 'invisible_captcha/view_helpers'
require 'invisible_captcha/form_helpers'
require 'invisible_captcha/railtie'

module InvisibleCaptcha
  class << self
    attr_writer :sentence_for_humans,
                :timestamp_error_message,
                :error_message

    attr_accessor :honeypots,
                  :timestamp_threshold,
                  :timestamp_enabled,
                  :visual_honeypots

    def init!
      # Default sentence for real users if text field was visible
      self.sentence_for_humans = -> { I18n.t('invisible_captcha.sentence_for_humans', default: 'If you are a human, ignore this field') }

      # Default error message for validator
      self.error_message = -> { I18n.t('invisible_captcha.error_message', default: 'You are a robot!') }

      # Default fake fields for controller based workflow
      self.honeypots = ['foo_id', 'bar_id', 'baz_id']

      # Fastest time (in seconds) to expect a human to submit the form
      self.timestamp_threshold = 4

      # Timestamp check enabled by default
      self.timestamp_enabled = true

      # Default error message for validator when form submitted too quickly
      self.timestamp_error_message = -> { I18n.t('invisible_captcha.timestamp_error_message', default: 'Sorry, that was too quick! Please resubmit.') }

      # Make honeypots visibles
      self.visual_honeypots = false
    end

    def sentence_for_humans
      call_lambda_or_return(@sentence_for_humans)
    end

    def error_message
      call_lambda_or_return(@error_message)
    end

    def timestamp_error_message
      call_lambda_or_return(@timestamp_error_message)
    end

    def setup
      yield(self) if block_given?
    end

    def get_honeypot
      honeypots.sample
    end

    private

    def call_lambda_or_return(obj)
      obj.respond_to?(:call) ? obj.call : obj
    end
  end
end

InvisibleCaptcha.init!

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
invisible_captcha-0.9.3 lib/invisible_captcha.rb
invisible_captcha-0.9.2 lib/invisible_captcha.rb
invisible_captcha-0.9.1 lib/invisible_captcha.rb