Class: CookieJar::Cookie

Inherits:
Object
  • Object
show all
Defined in:
lib/cookiejar/cookie.rb

Overview

Defines the parsing logic and data model of a HTTP Cookie. Note that the data values within the cookie may be different from the values described in the literal cookie declaration. Specifically, the ‘domain’ and ‘path’ values may be set to defaults based on the requested resource that resulted in the cookie being set.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#commentObject (readonly)

Comment (or location) describing cookie.



33
34
35
# File 'lib/cookiejar/cookie.rb', line 33

def comment
  @comment
end

#comment_urlObject (readonly)

Comment (or location) describing cookie.



33
34
35
# File 'lib/cookiejar/cookie.rb', line 33

def comment_url
  @comment_url
end

#created_atObject (readonly)

Returns the value of attribute created_at.



37
38
39
# File 'lib/cookiejar/cookie.rb', line 37

def created_at
  @created_at
end

#discardObject (readonly)

Discard



35
36
37
# File 'lib/cookiejar/cookie.rb', line 35

def discard
  @discard
end

#domainObject (readonly)

The domain and path of the cookie. These values will be set on all legal cookie objects, based on the requested URI if not set literally



18
19
20
# File 'lib/cookiejar/cookie.rb', line 18

def domain
  @domain
end

#http_onlyObject (readonly)

Popular browser extension to mark a cookie as invisible to code running within the browser, such as JavaScript



25
26
27
# File 'lib/cookiejar/cookie.rb', line 25

def http_only
  @http_only
end

#nameObject (readonly)

The mandatory name and value of the cookie



15
16
17
# File 'lib/cookiejar/cookie.rb', line 15

def name
  @name
end

#pathObject (readonly)

The domain and path of the cookie. These values will be set on all legal cookie objects, based on the requested URI if not set literally



18
19
20
# File 'lib/cookiejar/cookie.rb', line 18

def path
  @path
end

#portsObject (readonly)

Returns the value of attribute ports.



36
37
38
# File 'lib/cookiejar/cookie.rb', line 36

def ports
  @ports
end

#secureObject (readonly)

The secure flag is set to indicate that the cookie should only be sent securely. Nearly all implementations assume this to mean over SSL/TLS



22
23
24
# File 'lib/cookiejar/cookie.rb', line 22

def secure
  @secure
end

#valueObject (readonly)

The mandatory name and value of the cookie



15
16
17
# File 'lib/cookiejar/cookie.rb', line 15

def value
  @value
end

#versionObject (readonly)

Version indicator - version is 0 for netscape cookies, and 1 for RFC 2965 cookies



31
32
33
# File 'lib/cookiejar/cookie.rb', line 31

def version
  @version
end

Class Method Details

.compute_search_domains(request_uri) ⇒ Object



132
133
134
# File 'lib/cookiejar/cookie.rb', line 132

def self.compute_search_domains request_uri
  CookieValidation.compute_search_domains request_uri
end

Create a cookie based on an absolute URI and the string value of a ‘Set-Cookie’ header.



65
66
67
68
69
70
71
72
# File 'lib/cookiejar/cookie.rb', line 65

def self.from_set_cookie request_uri, set_cookie_value 
  args = CookieJar::CookieValidation.parse_set_cookie set_cookie_value
  args[:domain] = CookieJar::CookieValidation.determine_cookie_domain request_uri, args[:domain]
  args[:path] = CookieJar::CookieValidation.determine_cookie_path request_uri, args[:path]
  cookie = Cookie.new args
  CookieJar::CookieValidation.validate_cookie request_uri, cookie
  cookie
end

.json_create(o) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/cookiejar/cookie.rb', line 116

def self.json_create o
  params = o.inject({}) do |hash, (key, value)|
    hash[key.to_sym] = value
    hash
  end
  params[:version] ||= 0
  params[:created_at] = Time.parse params[:created_at]
  if params[:expiry].is_a? String
    params[:expires_at] = Time.parse params[:expiry]
  else
    params[:max_age] = params[:expiry]
  end
  params.delete :expiry

  self.new params
end

Instance Method Details

#expires_atObject



39
40
41
42
43
44
45
# File 'lib/cookiejar/cookie.rb', line 39

def expires_at
  if @expiry.nil? || @expiry.is_a?(Time)
    @expiry
  else
    @created_at + @expiry
  end
end

#is_expired?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/cookiejar/cookie.rb', line 55

def is_expired?
  expires_at != nil && Time.now > expires_at
end

#is_session?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/cookiejar/cookie.rb', line 59

def is_session?
  @expiry == nil
end

#max_ageObject



47
48
49
50
51
52
53
# File 'lib/cookiejar/cookie.rb', line 47

def max_age
  if @expiry.is_a? Time
    @expiry - @created_at
  else
    @expiry
  end
end

#should_send?(uri, script) ⇒ Boolean

Return true if (given a URI, a cookie object and other options) a cookie should be sent to a host. Note that this currently ignores domain.

The third option, ‘script’, indicates that cookies with the ‘http only’ extension should be ignored

Returns:

  • (Boolean)


83
84
85
86
87
88
89
90
91
# File 'lib/cookiejar/cookie.rb', line 83

def should_send? uri, script
  # cookie path must start with the uri, it must not be a secure cookie being sent over http,
  # and it must not be a http_only cookie sent to a script
  path_match   = uri.path.start_with? @path
  secure_match = !(@secure && uri.scheme == 'http') 
  script_match = !(script && @http_only)
  expiry_match = !is_expired?
  path_match && secure_match && script_match && expiry_match
end

#to_json(*a) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/cookiejar/cookie.rb', line 93

def to_json *a
  result = {
    :json_class => self.class.name,
    :name => @name,
    :value => @value,
    :domain => @domain,
    :path => @path,
    :created_at => @created_at
  }
  {
    :expiry => @expiry,
    :secure => (true if @secure),
    :http_only => (true if @http_only),
    :version => (@version if version != 0),
    :comment => @comment,
    :comment_url => @comment_url,
    :discard => (true if @discard),
    :ports => @ports
  }.each do |name, value|
    result[name] = value if value
  end
  result.to_json(*a)
end

#to_sObject



74
75
76
# File 'lib/cookiejar/cookie.rb', line 74

def to_s
  "#{name}=#{value}"
end