Class: Cookie
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, :document => Document }
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, = {}) ->
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'
52 53 54 55 |
# File 'lib/source/redshift/cookie.rb', line 52 def initialize(key, value, = {}) self.key = key self.update(value, OPTIONS.merge()) end |
Instance Attribute Details
#document ⇒ Object
Returns the value of attribute document.
37 38 39 |
# File 'lib/source/redshift/cookie.rb', line 37 def document @document end |
#domain ⇒ Object
Returns the value of attribute domain.
37 38 39 |
# File 'lib/source/redshift/cookie.rb', line 37 def domain @domain end |
#duration ⇒ Object
Returns the value of attribute duration.
37 38 39 |
# File 'lib/source/redshift/cookie.rb', line 37 def duration @duration end |
#key ⇒ Object
Returns the value of attribute key.
37 38 39 |
# File 'lib/source/redshift/cookie.rb', line 37 def key @key end |
#path ⇒ Object
Returns the value of attribute path.
37 38 39 |
# File 'lib/source/redshift/cookie.rb', line 37 def path @path end |
#secure ⇒ Object
Returns the value of attribute secure.
37 38 39 |
# File 'lib/source/redshift/cookie.rb', line 37 def secure @secure end |
#value ⇒ Object
Returns the value of attribute value.
37 38 39 |
# File 'lib/source/redshift/cookie.rb', line 37 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)
72 73 74 75 |
# File 'lib/source/redshift/cookie.rb', line 72 def self.read(key) value = `#{OPTIONS[:document].native}.cookie.match('(?:^|;)\\s*' + #{Regexp.escape(key)}.__value__ + '=([^;]*)')` return value ? `$q(decodeURIComponent(value[1]))` : nil end |
.store(cookie) ⇒ Object
call-seq:
Cookie.store() ->
Writes the given cookie to the browser, then returns cookie. This method is called internally by Cookie.new and Cookie#update.
83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/source/redshift/cookie.rb', line 83 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 `#{cookie.document.native}.cookie = str` return end |
Instance Method Details
#destroy ⇒ Object
call-seq:
.destroy -> true
Expires cookie, then returns true.
c = Cookie.new(:user_jds, '2237115568', :duration => 14)
c.destroy #=> true
Cookie.read(:user_jds) #=> nil
108 109 110 |
# File 'lib/source/redshift/cookie.rb', line 108 def destroy self.update('',:duration => -1) end |
#inspect ⇒ Object
call-seq:
.inspect -> string
Returns a string representing cookie and its key-value data.
c = Cookie.new(:user_jds, '2237115568', :duration => 14)
c.inspect #=> #<Cookie: @key="user_jds" @value="2237115568">
121 122 123 |
# File 'lib/source/redshift/cookie.rb', line 121 def inspect "#<Cookie: @key=#{self.key.inspect} @value=#{self.value.inspect}>" end |
#update(value, options = {}) ⇒ Object
call-seq:
.update(value, = {}) ->
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'
137 138 139 140 141 |
# File 'lib/source/redshift/cookie.rb', line 137 def update(value, = {}) self.value = value .each {|k,v| self.send("#{k}=",v) } Cookie.store(self) end |