A cookie store for client side usage.
Enforces cookie validity rules
Returns just the cookies valid for a given URI
Handles expiration of cookies
Allows for persistence of cookie data (with or without session)
Create a new Jar from an array of Cookie objects. Expired cookies will still be added to the archive, and conflicting cookies will be overwritten by the last cookie in the array.
@param [Array<Cookie>] cookies array of cookie objects @return [CookieJar] a new CookieJar instance
# File lib/cookiejar/jar.rb, line 177 def self.from_a cookies jar = new cookies.each do |cookie| jar.add_cookie cookie end jar end
Create a new Jar from a JSON-backed hash
@param o [Hash] the expanded JSON object @return [CookieJar] a new CookieJar instance
# File lib/cookiejar/jar.rb, line 161 def self.json_create o if o.is_a? Hash o = o['cookies'] end cookies = o.inject [] do |result, cookie_json| result << (Cookie.json_create cookie_json) end self.from_a cookies end
Create a new empty Jar
# File lib/cookiejar/jar.rb, line 48 def initialize @domains = {} end
Return an array of all cookie objects in the jar
@return [Array<Cookie>] all cookies. Includes any expired cookies which have not yet been removed with #expire_cookies
# File lib/cookiejar/jar.rb, line 134 def to_a result = [] @domains.values.each do |paths| paths.values.each do |cookies| cookies.values.inject result, :<< end end result end
Return a JSON ‘object’ for the various data values. Allows for persistence of the cookie information
@param [Array] a options controlling output JSON text
(usually a State and a depth)
@return [String] JSON representation of object data
# File lib/cookiejar/jar.rb, line 150 def to_json *a { 'json_class' => self.class.name, 'cookies' => (to_a.to_json *a) }.to_json *a end
# File lib/cookiejar/jar.rb, line 295 def find_domain host @domains[host] || {} end
# File lib/cookiejar/jar.rb, line 278 def gather_header_values http_header_value, &block result = [] http_header_value if http_header_value.is_a? Array http_header_value.each do |value| result << block.call(value) end elsif http_header_value.is_a? String result << block.call(http_header_value) end result.compact end
# File lib/cookiejar/jar.rb, line 291 def to_uri request_uri (request_uri.is_a? URI)? request_uri : (URI.parse request_uri) end