Class: NegativeCaptcha
- Inherits:
-
Object
- Object
- NegativeCaptcha
- Defined in:
- lib/negative_captcha.rb
Constant Summary collapse
- @@test_mode =
false
Instance Attribute Summary collapse
-
#css ⇒ Object
Returns the value of attribute css.
-
#error ⇒ Object
Returns the value of attribute error.
-
#fields ⇒ Object
Returns the value of attribute fields.
-
#message ⇒ Object
Returns the value of attribute message.
-
#secret ⇒ Object
Returns the value of attribute secret.
-
#spinner ⇒ Object
Returns the value of attribute spinner.
-
#timestamp ⇒ Object
Returns the value of attribute timestamp.
-
#values ⇒ Object
Returns the value of attribute values.
Class Method Summary collapse
Instance Method Summary collapse
- #[](name) ⇒ Object
-
#initialize(opts) ⇒ NegativeCaptcha
constructor
A new instance of NegativeCaptcha.
- #process(params) ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(opts) ⇒ NegativeCaptcha
Returns a new instance of NegativeCaptcha.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/negative_captcha.rb', line 20 def initialize(opts) self.secret = opts[:secret] || Digest::MD5.hexdigest("this_is_a_secret_key") if opts.has_key?(:params) self. = opts[:params][:timestamp] || Time.now.to_i else self. = Time.now.to_i end self.spinner = Digest::MD5.hexdigest( ([, secret] + Array(opts[:spinner])).join('-') ) self.css = opts[:css] || "position: absolute; left: -2000px;" self. = opts[:message] || "Please try again.\nThis usually happens because an automated script attempted to submit this form.\n MESSAGE\n\n self.fields = opts[:fields].inject({}) do |hash, field_name|\n hash[field_name] = @@test_mode ? \"test-\#{field_name}\" : Digest::MD5.hexdigest(\n [field_name, spinner, secret].join('-')\n )\n\n hash\n end\n\n self.values = HashWithIndifferentAccess.new\n self.error = \"No params provided\"\n\n if opts[:params] && (opts[:params][:spinner] || opts[:params][:timestamp])\n process(opts[:params])\n end\nend\n" |
Instance Attribute Details
#css ⇒ Object
Returns the value of attribute css.
6 7 8 |
# File 'lib/negative_captcha.rb', line 6 def css @css end |
#error ⇒ Object
Returns the value of attribute error.
6 7 8 |
# File 'lib/negative_captcha.rb', line 6 def error @error end |
#fields ⇒ Object
Returns the value of attribute fields.
6 7 8 |
# File 'lib/negative_captcha.rb', line 6 def fields @fields end |
#message ⇒ Object
Returns the value of attribute message.
6 7 8 |
# File 'lib/negative_captcha.rb', line 6 def end |
#secret ⇒ Object
Returns the value of attribute secret.
6 7 8 |
# File 'lib/negative_captcha.rb', line 6 def secret @secret end |
#spinner ⇒ Object
Returns the value of attribute spinner.
6 7 8 |
# File 'lib/negative_captcha.rb', line 6 def spinner @spinner end |
#timestamp ⇒ Object
Returns the value of attribute timestamp.
6 7 8 |
# File 'lib/negative_captcha.rb', line 6 def end |
#values ⇒ Object
Returns the value of attribute values.
6 7 8 |
# File 'lib/negative_captcha.rb', line 6 def values @values end |
Class Method Details
.test_mode=(value) ⇒ Object
16 17 18 |
# File 'lib/negative_captcha.rb', line 16 def self.test_mode=(value) class_variable_set(:@@test_mode, value) end |
Instance Method Details
#[](name) ⇒ Object
57 58 59 |
# File 'lib/negative_captcha.rb', line 57 def [](name) fields[name] end |
#process(params) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/negative_captcha.rb', line 65 def process(params) = (Time.now.to_i - params[:timestamp].to_i).abs if params[:timestamp].nil? || > 86400 self.error = "Error: Invalid timestamp. #{message}" elsif params[:spinner] != spinner self.error = "Error: Invalid spinner. #{message}" elsif fields.keys.detect {|name| params[name] && params[name] =~ /\S/} self.error = "Error: Hidden form fields were submitted that should not have been. \#{message}\n ERROR\n\n false\n else\n self.error = \"\"\n\n fields.each do |name, encrypted_name|\n self.values[name] = params[encrypted_name] if params.include? encrypted_name\n end\n end\nend\n" |
#valid? ⇒ Boolean
61 62 63 |
# File 'lib/negative_captcha.rb', line 61 def valid? error.blank? end |