Class: Formidable::Attempt

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

Class Method Summary collapse

Class Method Details

.parse(cookies, form, valid) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/formidable/attempt.rb', line 5

def parse(cookies, form, valid)
  hash = Digest::MD5.hexdigest("#{Config::api_key}.#{form}")

  cookie = cookies["formidable"]
  cookie_data = cookie ? Marshal.load(cookie) : {} rescue {}

  cookie_data[hash] ||= 1
  attempt = cookie_data[hash]

  unless valid
    cookie_data[hash] += 1
    save_cookie(cookies, cookie_data)
  else
    cookie_data.delete hash
    if cookie_data.empty?
      cookies.delete "formidable"
    else
      save_cookie(cookies, cookie_data)
    end
  end

  attempt
end