Class: Merb::Test::Cookie

Inherits:
Object show all
Defined in:
lib/merb-core/test/helpers/cookie_jar.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw, default_host) ⇒ Cookie

:api: private



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/merb-core/test/helpers/cookie_jar.rb', line 11

def initialize(raw, default_host)
  # separate the name / value pair from the cookie options
  @name_value_raw, options = raw.split(/[;,] */n, 2)
  
  @name, @value = Merb::Parse.query(@name_value_raw, ';').to_a.first
  @options = Merb::Parse.query(options, ';')
  
  @options.delete_if { |k, v| !v || v.empty? }
  
  @options["domain"] ||= default_host
end

Instance Attribute Details

#nameObject (readonly)

:api: private



8
9
10
# File 'lib/merb-core/test/helpers/cookie_jar.rb', line 8

def name
  @name
end

#valueObject (readonly)

:api: private



8
9
10
# File 'lib/merb-core/test/helpers/cookie_jar.rb', line 8

def value
  @value
end

Instance Method Details

#<=>(other) ⇒ Object

:api: private



68
69
70
71
# File 'lib/merb-core/test/helpers/cookie_jar.rb', line 68

def <=>(other)
  # Orders the cookies from least specific to most
  [name, path, domain.reverse] <=> [other.name, other.path, other.domain.reverse]
end

#domainObject

:api: private



34
35
36
# File 'lib/merb-core/test/helpers/cookie_jar.rb', line 34

def domain
  @options["domain"]
end

#empty?Boolean

:api: private

Returns:

  • (Boolean)


29
30
31
# File 'lib/merb-core/test/helpers/cookie_jar.rb', line 29

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

#expired?Boolean

:api: private

Returns:

  • (Boolean)


49
50
51
# File 'lib/merb-core/test/helpers/cookie_jar.rb', line 49

def expired?
  expires && expires < Time.now
end

#expiresObject

:api: private



44
45
46
# File 'lib/merb-core/test/helpers/cookie_jar.rb', line 44

def expires
  Time.parse(@options["expires"]) if @options["expires"]
end

#matches?(uri) ⇒ Boolean

:api: private

Returns:

  • (Boolean)


63
64
65
# File 'lib/merb-core/test/helpers/cookie_jar.rb', line 63

def matches?(uri)
  ! expired? && valid?(uri)
end

#pathObject

:api: private



39
40
41
# File 'lib/merb-core/test/helpers/cookie_jar.rb', line 39

def path
  @options["path"] || "/"
end

#rawObject

:api: private



24
25
26
# File 'lib/merb-core/test/helpers/cookie_jar.rb', line 24

def raw
  @name_value_raw
end

#valid?(uri) ⇒ Boolean

:api: private

Returns:

  • (Boolean)


54
55
56
57
58
59
60
# File 'lib/merb-core/test/helpers/cookie_jar.rb', line 54

def valid?(uri)
  domain_ = domain.index('.') == 0 ? domain[1..-1] : domain
  uri_path = uri.path.blank? ? "/" : uri.path

  uri.host =~ Regexp.new("#{Regexp.escape(domain_)}$") &&
  uri_path =~ Regexp.new("^#{Regexp.escape(path)}")
end