Class: HTTPX::Plugins::Cookies::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/httpx/plugins/cookies.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cookies = nil) ⇒ Store

Returns a new instance of Store.



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/httpx/plugins/cookies.rb', line 36

def initialize(cookies = nil)
  @store = Hash.new { |hash, origin| hash[origin] = HTTP::CookieJar.new }
  return unless cookies

  cookies = cookies.split(/ *; */) if cookies.is_a?(String)
  @default_cookies = cookies.map do |cookie, v|
    if cookie.is_a?(HTTP::Cookie)
      cookie
    else
      HTTP::Cookie.new(cookie.to_s, v.to_s)
    end
  end
end

Class Method Details

.new(cookies = nil) ⇒ Object



30
31
32
33
34
# File 'lib/httpx/plugins/cookies.rb', line 30

def self.new(cookies = nil)
  return cookies if cookies.is_a?(self)

  super
end

Instance Method Details

#==(other) ⇒ Object



67
68
69
# File 'lib/httpx/plugins/cookies.rb', line 67

def ==(other)
  @store == other.instance_variable_get(:@store)
end

#[](uri) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/httpx/plugins/cookies.rb', line 56

def [](uri)
  store = @store[uri.origin]
  @default_cookies.each do |cookie|
    c = cookie.dup
    c.domain ||= uri.authority
    c.path ||= uri.path
    store.add(c)
  end if @default_cookies
  store
end

#set(origin, cookies) ⇒ Object



50
51
52
53
54
# File 'lib/httpx/plugins/cookies.rb', line 50

def set(origin, cookies)
  return unless cookies

  @store[origin].parse(cookies, origin)
end