Class: NegativeCaptcha

Inherits:
Object
  • Object
show all
Defined in:
lib/negative_captcha.rb

Constant Summary collapse

@@test_mode =
false

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ NegativeCaptcha

Returns a new instance of NegativeCaptcha.



19
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
# File 'lib/negative_captcha.rb', line 19

def initialize(opts)
  self.secret = opts[:secret] ||
    Digest::MD5.hexdigest("this_is_a_secret_key")

  if opts.has_key?(:params)
    self.timestamp = opts[:params][:timestamp] || Time.now.to_i
  else
    self.timestamp = Time.now.to_i
  end

  self.spinner = Digest::MD5.hexdigest(
    ([timestamp, secret] + Array(opts[:spinner])).join('-')
  )

  self.message = 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

#errorObject

Returns the value of attribute error.



6
7
8
# File 'lib/negative_captcha.rb', line 6

def error
  @error
end

#fieldsObject

Returns the value of attribute fields.



6
7
8
# File 'lib/negative_captcha.rb', line 6

def fields
  @fields
end

#messageObject

Returns the value of attribute message.



6
7
8
# File 'lib/negative_captcha.rb', line 6

def message
  @message
end

#secretObject

Returns the value of attribute secret.



6
7
8
# File 'lib/negative_captcha.rb', line 6

def secret
  @secret
end

#spinnerObject

Returns the value of attribute spinner.



6
7
8
# File 'lib/negative_captcha.rb', line 6

def spinner
  @spinner
end

#timestampObject

Returns the value of attribute timestamp.



6
7
8
# File 'lib/negative_captcha.rb', line 6

def timestamp
  @timestamp
end

#valuesObject

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



15
16
17
# File 'lib/negative_captcha.rb', line 15

def self.test_mode=(value)
  class_variable_set(:@@test_mode, value)
end

Instance Method Details

#[](name) ⇒ Object



54
55
56
# File 'lib/negative_captcha.rb', line 54

def [](name)
  fields[name]
end

#process(params) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/negative_captcha.rb', line 62

def process(params)
  timestamp_age = (Time.now.to_i - params[:timestamp].to_i).abs

  if params[:timestamp].nil? || timestamp_age > 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
Error: Hidden form fields were submitted that should not have been. #{message}
    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

Returns:

  • (Boolean)


58
59
60
# File 'lib/negative_captcha.rb', line 58

def valid?
  error.nil? || error == "" || error.empty?
end