Class: HTTP::CookieJar::HashStore

Inherits:
AbstractStore show all
Defined in:
lib/http/cookie_jar/hash_store.rb

Constant Summary collapse

GC_THRESHOLD =
HTTP::Cookie::MAX_COOKIES_TOTAL / 20

Instance Method Summary collapse

Methods inherited from AbstractStore

class_to_symbol, implementation, inherited

Constructor Details

#initialize(options = nil) ⇒ HashStore

Returns a new instance of HashStore.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/http/cookie_jar/hash_store.rb', line 17

def initialize(options = nil)
  super

  @jar = {}
  # {
  #   hostname => {
  #     path => {
  #       name => cookie,
  #       ...
  #     },
  #     ...
  #   },
  #   ...
  # }

  @gc_index = 0
end

Instance Method Details

#add(cookie) ⇒ Object



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

def add(cookie)
  path_cookies = ((@jar[cookie.domain_name.hostname] ||= {})[cookie.path] ||= {})

  if cookie.expired?
    path_cookies.delete(cookie.name)
  else
    path_cookies[cookie.name] = cookie
    cleanup if (@gc_index += 1) >= GC_THRESHOLD
  end

  self
end

#cleanup(session = false) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/http/cookie_jar/hash_store.rb', line 97

def cleanup(session = false)
  all_cookies = []

  @jar.each { |domain, paths|
    domain_cookies = []

    paths.each { |path, hash|
      hash.delete_if { |name, cookie|
        if cookie.expired? || (session && cookie.session?)
          true
        else
          domain_cookies << cookie
          false
        end
      }
    }

    if (debt = domain_cookies.size - HTTP::Cookie::MAX_COOKIES_PER_DOMAIN) > 0
      domain_cookies.sort_by!(&:created_at)
      domain_cookies.slice!(0, debt).each { |cookie|
        add(cookie.expire)
      }
    end

    all_cookies.concat(domain_cookies)
  }

  if (debt = all_cookies.size - HTTP::Cookie::MAX_COOKIES_TOTAL) > 0
    all_cookies.sort_by!(&:created_at)
    all_cookies.slice!(0, debt).each { |cookie|
      add(cookie.expire)
    }
  end

  @jar.delete_if { |domain, paths|
    paths.delete_if { |path, hash|
      hash.empty?
    }
    paths.empty?
  }

  @gc_index = 0
end

#clearObject



88
89
90
91
# File 'lib/http/cookie_jar/hash_store.rb', line 88

def clear
  @jar.clear
  self
end

#default_optionsObject



13
14
15
# File 'lib/http/cookie_jar/hash_store.rb', line 13

def default_options
  {}
end

#each(uri = nil) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/http/cookie_jar/hash_store.rb', line 52

def each(uri = nil)
  if uri
    thost = DomainName.new(uri.host)
    tpath = HTTP::Cookie.normalize_path(uri.path)
    @jar.each { |domain, paths|
      next unless thost.cookie_domain?(domain)
      paths.each { |path, hash|
        next unless tpath.start_with?(path)
        hash.delete_if { |name, cookie|
          if cookie.expired?
            true
          else
            cookie.accessed_at = Time.now
            yield cookie
            false
          end
        }
      }
    }
  else
    @jar.each { |domain, paths|
      paths.each { |path, hash|
        hash.delete_if { |name, cookie|
          if cookie.expired?
            true
          else
            yield cookie
            false
          end
        }
      }
    }
  end
  self
end

#empty?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/http/cookie_jar/hash_store.rb', line 93

def empty?
  @jar.empty?
end

#initialize_copy(other) ⇒ Object



35
36
37
# File 'lib/http/cookie_jar/hash_store.rb', line 35

def initialize_copy(other)
  @jar = Marshal.load(Marshal.dump(other.instance_variable_get(:@jar)))
end