Class: Lookout::Rack::Cookie

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/lookout-rack-1.0/cookie.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(header, uri = nil, default_host = Lookout::Rack::DefaultHost) ⇒ Cookie

Returns a new instance of Cookie.



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/lookout-rack-1.0/cookie.rb', line 6

def initialize(header, uri = nil, default_host = Lookout::Rack::DefaultHost)
  @default_host = default_host
  uri ||= default_uri

  @cookie, options = header.split(/[;,] */n, 2)

  @name, @value = Rack::Utils.parse_query(@cookie, ';').to_a.first

  @options = Hash[Rack::Utils.parse_query(options, ';').map{ |k, v| [k.downcase, v] }]
  @options['domain'] ||= uri.host || default_host
  @options['path'] ||= uri.path.sub(%r{/[^/]*\z}, '')
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



54
55
56
# File 'lib/lookout-rack-1.0/cookie.rb', line 54

def name
  @name
end

Instance Method Details

#<=>(other) ⇒ Object



41
42
43
44
# File 'lib/lookout-rack-1.0/cookie.rb', line 41

def <=>(other)
  [name.downcase, path, domain.reverse] <=>
    [other.name.downcase, other.path, other.domain.reverse]
end

#empty?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/lookout-rack-1.0/cookie.rb', line 19

def empty?
  @value.nil? or @value.empty?
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/lookout-rack-1.0/cookie.rb', line 37

def eql?(other)
  self.class === other and self == other
end

#hashObject



46
47
48
# File 'lib/lookout-rack-1.0/cookie.rb', line 46

def hash
  name.downcase.hash ^ path.hash ^ domain.hash
end

#matches?(uri) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/lookout-rack-1.0/cookie.rb', line 33

def matches?(uri)
  valid? uri and not expired?
end

#to_sObject



50
51
52
# File 'lib/lookout-rack-1.0/cookie.rb', line 50

def to_s
  @cookie
end

#valid?(uri) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
27
28
29
30
31
# File 'lib/lookout-rack-1.0/cookie.rb', line 23

def valid?(uri)
  uri ||= default_uri
  uri.host ||= @default_host

  real_domain = domain.start_with?('.') ? domain[1..-1] : domain
  secure? uri and
    uri.host.downcase.end_with? real_domain.downcase and
    uri.path.start_with? path
end