Class: Pincers::Http::CookieJar

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

Constant Summary collapse

BAD_VALUE_CHARS =

RFC 6265 - 4.1.1

/([\x00-\x20\x7F",;\\])/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_cookies = nil) ⇒ CookieJar

Returns a new instance of CookieJar.



11
12
13
# File 'lib/pincers/http/cookie_jar.rb', line 11

def initialize(_cookies=nil)
  @cookies = _cookies || []
end

Instance Attribute Details

#cookiesObject (readonly)

Returns the value of attribute cookies.



9
10
11
# File 'lib/pincers/http/cookie_jar.rb', line 9

def cookies
  @cookies
end

Instance Method Details

#copyObject



15
16
17
# File 'lib/pincers/http/cookie_jar.rb', line 15

def copy
  self.class.new @cookies.clone
end

#for_origin(_uri) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/pincers/http/cookie_jar.rb', line 51

def for_origin(_uri)
  # RFC 6265 5.4.1
  @cookies.select do |c|
    # TODO: add scheme and host only checks
    domains_match c.domain, _uri.host and paths_match c.path, _uri.path
  end
end

#for_origin_as_header(_uri) ⇒ Object



59
60
61
# File 'lib/pincers/http/cookie_jar.rb', line 59

def for_origin_as_header(_uri)
  for_origin(_uri).map { |c| "#{c.name}=#{quote(c.value)}" }.join('; ')
end

#get(_url, _name) ⇒ Object



19
20
21
# File 'lib/pincers/http/cookie_jar.rb', line 19

def get(_url, _name)
  for_origin(Utils.parse_uri(_url)).find { |c| c.name == _name }
end

#set(_cookie) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/pincers/http/cookie_jar.rb', line 23

def set(_cookie)
  if _cookie.name.nil? or _cookie.value.nil? or _cookie.domain.nil? or _cookie.path.nil?
    raise ArgumentError, 'Invalid cookie'
  end

  @cookies.each_with_index do |cookie, i|
    if equivalent(cookie, _cookie)
      @cookies[i] = _cookie
      return _cookie
    end
  end

  @cookies << _cookie
  _cookie
end

#set_from_header(_uri, _header) ⇒ Object



45
46
47
48
49
# File 'lib/pincers/http/cookie_jar.rb', line 45

def set_from_header(_uri, _header)
  _header.split(/, (?=\w+=)/).map do |raw_cookie|
    set_raw _uri, raw_cookie.strip
  end
end

#set_raw(_request_uri, _raw) ⇒ Object



39
40
41
42
43
# File 'lib/pincers/http/cookie_jar.rb', line 39

def set_raw(_request_uri, _raw)
  cookie = decode_cookie _request_uri, _raw
  set cookie unless cookie.nil?
  cookie
end