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] || <<-MESSAGE Please try again. This usually happens because an automated script attempted to submit this form. MESSAGE self.fields = opts[:fields].inject({}) do |hash, field_name| hash[field_name] = @@test_mode ? "test-#{field_name}" : Digest::MD5.hexdigest( [field_name, spinner, secret].join('-') ) hash end self.values = HashWithIndifferentAccess.new self.error = "No params provided" if opts[:params] && (opts[:params][:spinner] || opts[:params][:timestamp]) process(opts[:params]) end end |
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 @message 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 @timestamp 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. #{}" elsif params[:spinner] != spinner self.error = "Error: Invalid spinner. #{}" elsif fields.keys.detect {|name| params[name] && params[name] =~ /\S/} self.error = <<-ERROR Error: Hidden form fields were submitted that should not have been. #{} ERROR false else self.error = "" fields.each do |name, encrypted_name| self.values[name] = params[encrypted_name] if params.include? encrypted_name end end end |
#valid? ⇒ Boolean
61 62 63 |
# File 'lib/negative_captcha.rb', line 61 def valid? error.blank? end |