Class: CookieStore::HashStore

Inherits:
CookieStore show all
Defined in:
lib/cookie_store/hash_store.rb

Constant Summary

Constants inherited from CookieStore

MAX_COOKIES_PER_DOMAIN, MAX_COOKIES_TOTAL, MAX_COOKIE_LENGTH, VERSION

Instance Method Summary collapse

Methods inherited from CookieStore

#close_session, #cookie_header_for, #search_domains_for, #set_cookie

Constructor Details

#initializeHashStore

Returns a new instance of HashStore.



3
4
5
# File 'lib/cookie_store/hash_store.rb', line 3

def initialize
  @domains = {}
end

Instance Method Details

#add(cookie) ⇒ Object



7
8
9
10
11
12
# File 'lib/cookie_store/hash_store.rb', line 7

def add(cookie)
  #TODO: check for MAX_COOKIES_PER_DOMAIN && MAX_COOKIES_TOTAL, think remove the MAX_COOKIES_TOTAL tho
  @domains[cookie.domain] ||= {}
  @domains[cookie.domain][cookie.path] ||= {}
  @domains[cookie.domain][cookie.path][cookie.name] = cookie
end

#cookies_for(request_uri) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/cookie_store/hash_store.rb', line 14

def cookies_for(request_uri)
  request_uri = URI.parse(request_uri)
  trigger_gc = false
  request_cookies = []
  
  search_domains_for(request_uri.host).each do |domain|
    next unless @domains[domain]
    
    @domains[domain].each do |path, cookies|
      if request_uri.path.start_with?(path)
        cookies.each do |name, cookie|
          if cookie.expired?
            trigger_gc = true
          elsif cookie.port_match(request_uri.port) && (!cookie.secure || (cookie.secure && request_uri.scheme == 'https'))
            request_cookies << cookie
          end
        end
      end
    end
  end
  
  gc if trigger_gc
  
  request_cookies
end

#gc(close_session = false) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/cookie_store/hash_store.rb', line 40

def gc(close_session=false)
  @domains.delete_if do |domain, paths|
    paths.delete_if do |path, cookies|
      cookies.delete_if do |cookie_name, cookie|
        cookie.expired? || (close_session && cookie.session?)
      end
      cookies.empty?
    end
    paths.empty?
  end
end