Class: Rack::Test::CookieJar

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/test/cookie_jar.rb

Instance Method Summary collapse

Constructor Details

#initialize(cookies = [], default_host = DEFAULT_HOST) ⇒ CookieJar

:api: private



99
100
101
102
103
# File 'lib/rack/test/cookie_jar.rb', line 99

def initialize(cookies = [], default_host = DEFAULT_HOST)
  @default_host = default_host
  @cookies = cookies
  @cookies.sort!
end

Instance Method Details

#<<(new_cookie) ⇒ Object



125
126
127
128
129
130
131
132
# File 'lib/rack/test/cookie_jar.rb', line 125

def <<(new_cookie)
  @cookies.reject! do |existing_cookie|
    new_cookie.replaces?(existing_cookie)
  end

  @cookies << new_cookie
  @cookies.sort!
end

#[](name) ⇒ Object



105
106
107
108
109
# File 'lib/rack/test/cookie_jar.rb', line 105

def [](name)
  cookies = hash_for(nil)
  # TODO: Should be case insensitive
  cookies[name] && cookies[name].value
end

#[]=(name, value) ⇒ Object



111
112
113
114
# File 'lib/rack/test/cookie_jar.rb', line 111

def []=(name, value)
  # TODO: needs proper escaping
  merge("#{name}=#{value}")
end

#for(uri) ⇒ Object

:api: private



135
136
137
# File 'lib/rack/test/cookie_jar.rb', line 135

def for(uri)
  hash_for(uri).values.map { |c| c.raw }.join(';')
end

#merge(raw_cookies, uri = nil) ⇒ Object



116
117
118
119
120
121
122
123
# File 'lib/rack/test/cookie_jar.rb', line 116

def merge(raw_cookies, uri = nil)
  return unless raw_cookies

  Array(raw_cookies).join("\n").split("\n").each do |raw_cookie|
    cookie = Cookie.new(raw_cookie, uri, @default_host)
    self << cookie if cookie.valid?(uri)
  end
end

#to_hashObject



139
140
141
142
143
144
145
146
147
# File 'lib/rack/test/cookie_jar.rb', line 139

def to_hash
  cookies = {}

  hash_for(nil).each do |name, cookie|
    cookies[name] = cookie.value
  end

  return cookies
end