Class: Cookie
- Inherits:
-
Object
- Object
- Cookie
- Defined in:
- lib/red_query/cookie.rb
Overview
Class Cookie
governs the writing and accessing of cookies in the browser.
A cookie is a key-value pair stored by your browser as text data. If you know a cookie’s key, you can read or overwrite its value, or reassign any of a number of parameters.
Instances of class Cookie
are temporary holders for browser-based cookie data. When you create a new Cookie
object using Cookie.new
or update an existing Cookie
object using Cookie#update
, class Cookie
writes the key-value pair and the cookie’s parameters to the browser’s cookie file. You can then read the value of the cookie immediately or during subsequent visits using Cookie.read
.
The following parameters can be set for a Cookie
object:
Required
- key
-
The unique (per domain) identifier by which you identify a cookie.
- value
-
The string of data associated with a given key.
Optional
- duration
-
The amount of time (in days) before the cookie should expire.
- domain
-
The domain to which the cookie should be sent.
- path
-
The path, relative to the domain, where the cookie is active.
- secure
-
If
true
, the browser will use SSL when sending the cookie.
The browser can hold up to 20 cookies from a single domain.
Constant Summary collapse
- OPTIONS =
{ :duration => nil, :domain => nil, :path => nil, :secure => false }
Instance Attribute Summary collapse
-
#document ⇒ Object
Returns the value of attribute document.
-
#domain ⇒ Object
Returns the value of attribute domain.
-
#duration ⇒ Object
Returns the value of attribute duration.
-
#key ⇒ Object
Returns the value of attribute key.
-
#path ⇒ Object
Returns the value of attribute path.
-
#secure ⇒ Object
Returns the value of attribute secure.
-
#value ⇒ Object
Returns the value of attribute value.
Class Method Summary collapse
-
.read(key) ⇒ Object
call-seq: Cookie.read(key) -> string.
-
.store(cookie) ⇒ Object
call-seq: Cookie.store(cookie) -> cookie.
Instance Method Summary collapse
-
#destroy ⇒ Object
call-seq: cookie.destroy -> true.
-
#initialize(key, value, options = {}) ⇒ Cookie
constructor
call-seq: Cookie.new(key, value, options = {}) -> cookie.
-
#inspect ⇒ Object
call-seq: cookie.inspect -> string.
-
#update(value, options = {}) ⇒ Object
call-seq: cookie.update(value, options = {}) -> cookie.
Constructor Details
#initialize(key, value, options = {}) ⇒ Cookie
call-seq:
Cookie.new(key, value, options = {}) -> cookie
Returns a new Cookie
object with the given parameters and stores the data in the browser as cookie data. If the browser already has a cookie that matches key, that cookie’s parameters will be overwritten.
Cookie.new(:user_jds, '2237115568') #=> #<Cookie: @key="user_jds" @value="2237115568">
Cookie.read(:user_jds) #=> '2237115568'
Cookie.new(:user_jds, '8557acb0') #=> #<Cookie: @key="user_jds" @value="8557acb0">
Cookie.read(:user_jds) #=> '8557acb0'
54 55 56 57 |
# File 'lib/red_query/cookie.rb', line 54 def initialize(key, value, = {}) self.key = key self.update(value, OPTIONS.merge()) end |
Instance Attribute Details
#document ⇒ Object
Returns the value of attribute document.
39 40 41 |
# File 'lib/red_query/cookie.rb', line 39 def document @document end |
#domain ⇒ Object
Returns the value of attribute domain.
39 40 41 |
# File 'lib/red_query/cookie.rb', line 39 def domain @domain end |
#duration ⇒ Object
Returns the value of attribute duration.
39 40 41 |
# File 'lib/red_query/cookie.rb', line 39 def duration @duration end |
#key ⇒ Object
Returns the value of attribute key.
39 40 41 |
# File 'lib/red_query/cookie.rb', line 39 def key @key end |
#path ⇒ Object
Returns the value of attribute path.
39 40 41 |
# File 'lib/red_query/cookie.rb', line 39 def path @path end |
#secure ⇒ Object
Returns the value of attribute secure.
39 40 41 |
# File 'lib/red_query/cookie.rb', line 39 def secure @secure end |
#value ⇒ Object
Returns the value of attribute value.
39 40 41 |
# File 'lib/red_query/cookie.rb', line 39 def value @value end |
Class Method Details
.read(key) ⇒ Object
call-seq:
Cookie.read(key) -> string
Returns the string value of the cookie named key, or nil
if no such cookie exists.
c = Cookie.new(:user_jds, '2237115568', :domain => '.example.com')
Cookie.read(:user_jds) #=> '2237115568'
This method can be used to test whether a cookie with the name key exists in the browser.
Cookie.new(:user_jds, '8557acb0') unless Cookie.read(:user_jds)
74 75 76 77 |
# File 'lib/red_query/cookie.rb', line 74 def self.read(key) value = `document.cookie.match('(?:^|;)\\s*' + #{Regexp.escape(key)}.__value__ + '=([^;]*)')` return value ? `$q(decodeURIComponent(value[1]))` : nil end |
.store(cookie) ⇒ Object
call-seq:
Cookie.store(cookie) -> cookie
Writes the given cookie to the browser, then returns cookie. This method is called internally by Cookie.new
and Cookie#update
.
85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/red_query/cookie.rb', line 85 def self.store() `var str = cookie.m$key().__value__ + '=' + encodeURIComponent(cookie.m$value().__value__)` `str += '; domain=' + cookie.m$domain().__value__` if .domain `str += '; path=' + cookie.m$path().__value__` if .path if .duration `date = new Date()` `date.setTime(date.getTime() + cookie.m$duration() * 86400000)` `str += '; expires=' + date.toGMTString()` end `str += '; secure'` if .secure `document.cookie = str` return end |
Instance Method Details
#destroy ⇒ Object
110 111 112 |
# File 'lib/red_query/cookie.rb', line 110 def destroy self.update('',:duration => -1) end |
#inspect ⇒ Object
123 124 125 |
# File 'lib/red_query/cookie.rb', line 123 def inspect "#<Cookie: @key=#{self.key.inspect} @value=#{self.value.inspect}>" end |
#update(value, options = {}) ⇒ Object
call-seq:
cookie.update(value, options = {}) -> cookie
Updates cookie with the given parameters, then writes the cookie data to the browser.
c = Cookie.new(:user_jds, '2237115568', :duration => 14)
Cookie.read(:user_jds) #=> '2237115568'
c.update('8557acb0') #=> #<Cookie: @key="user_jds" @value="8557acb0">
Cookie.read(:user_jds) #=> '8557acb0'
139 140 141 142 143 |
# File 'lib/red_query/cookie.rb', line 139 def update(value, = {}) self.value = value .each {|k,v| self.send("#{k}=",v) } Cookie.store(self) end |