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

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

Instance Method Summary collapse

Constructor Details

#initialize(cookies = nil) ⇒ Store

Returns a new instance of Store.


28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/httpx/plugins/cookies.rb', line 28

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

Instance Method Details

#[](uri) ⇒ Object


48
49
50
51
52
53
54
55
56
57
# File 'lib/httpx/plugins/cookies.rb', line 48

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


42
43
44
45
46
# File 'lib/httpx/plugins/cookies.rb', line 42

def set(origin, cookies)
  return unless cookies

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