Class: Buzz::CookieJar

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

Defined Under Namespace

Classes: Cookie

Instance Method Summary collapse

Constructor Details

#initializeCookieJar

Returns a new instance of CookieJar.



10
11
12
13
# File 'lib/buzz/cookie_jar.rb', line 10

def initialize
  @cookies = {}
  @mutex = Mutex.new
end

Instance Method Details

#clearObject



45
46
47
# File 'lib/buzz/cookie_jar.rb', line 45

def clear
  @mutex.synchronize { @cookies.clear }
end


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/buzz/cookie_jar.rb', line 29

def cookie_header(uri)
  uri = uri.is_a?(URI) ? uri : URI.parse(uri)
  now = Time.now

  @mutex.synchronize do
    matching = @cookies.values.select do |cookie|
      next false if cookie.expires && cookie.expires < now
      next false if cookie.secure && uri.scheme != "https"
      path_matches?(uri.path, cookie.path)
    end

    return nil if matching.empty?
    matching.map { |c| "#{c.name}=#{c.value}" }.join("; ")
  end
end

#empty?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/buzz/cookie_jar.rb', line 49

def empty?
  @mutex.synchronize { @cookies.empty? }
end

#parse(set_cookie_header, request_uri) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/buzz/cookie_jar.rb', line 15

def parse(set_cookie_header, request_uri)
  return unless set_cookie_header

  headers = set_cookie_header.is_a?(Array) ? set_cookie_header : [set_cookie_header]
  uri = request_uri.is_a?(URI) ? request_uri : URI.parse(request_uri)

  @mutex.synchronize do
    headers.each do |header|
      cookie = parse_cookie(header, uri)
      @cookies[cookie.name] = cookie if cookie
    end
  end
end

#sizeObject



53
54
55
# File 'lib/buzz/cookie_jar.rb', line 53

def size
  @mutex.synchronize { @cookies.size }
end