Class: Cookie

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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, options = {})
  self.key = key
  self.update(value, OPTIONS.merge(options))
end

Instance Attribute Details

#documentObject

Returns the value of attribute document.



39
40
41
# File 'lib/red_query/cookie.rb', line 39

def document
  @document
end

#domainObject

Returns the value of attribute domain.



39
40
41
# File 'lib/red_query/cookie.rb', line 39

def domain
  @domain
end

#durationObject

Returns the value of attribute duration.



39
40
41
# File 'lib/red_query/cookie.rb', line 39

def duration
  @duration
end

#keyObject

Returns the value of attribute key.



39
40
41
# File 'lib/red_query/cookie.rb', line 39

def key
  @key
end

#pathObject

Returns the value of attribute path.



39
40
41
# File 'lib/red_query/cookie.rb', line 39

def path
  @path
end

#secureObject

Returns the value of attribute secure.



39
40
41
# File 'lib/red_query/cookie.rb', line 39

def secure
  @secure
end

#valueObject

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(cookie)
  `var str = cookie.m$key().__value__ + '=' + encodeURIComponent(cookie.m$value().__value__)`
  `str += '; domain=' + cookie.m$domain().__value__` if cookie.domain
  `str += '; path='   + cookie.m$path().__value__`   if cookie.path
  if cookie.duration
    `date = new Date()`
    `date.setTime(date.getTime() + cookie.m$duration() * 86400000)`
    `str += '; expires=' + date.toGMTString()`
  end
  `str += '; secure'` if cookie.secure
  
  `document.cookie = str`
  return cookie
end

Instance Method Details

#destroyObject

call-seq:

cookie.destroy -> true

Expires cookie, then returns true.

c = Cookie.new(:user_jds, '2237115568', :duration => 14)

c.destroy                 #=> true
Cookie.read(:user_jds)    #=> nil


110
111
112
# File 'lib/red_query/cookie.rb', line 110

def destroy
  self.update('',:duration => -1)
end

#inspectObject

call-seq:

cookie.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">


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, options = {})
  self.value = value
  options.each {|k,v| self.send("#{k}=",v) }
  Cookie.store(self)
end