Class: HTTPI::CookieStore

Inherits:
Object
  • Object
show all
Defined in:
lib/httpi/cookie_store.rb

Overview

HTTPI::CookieStore

Stores a unique list of cookies for future requests.

Examples

# Add one or more cookies to the store
cookie_store = HTTPI::CookieStore.new
cookie_store.add HTTPI::Cookie.new("token=choc-choc-chip; Path=/; HttpOnly")

# Fetch the names and values for the "Cookie" header
cookie_store.fetch  # => "token=choc-choc-chip"

Instance Method Summary collapse

Constructor Details

#initializeCookieStore

Returns a new instance of CookieStore.



17
18
19
# File 'lib/httpi/cookie_store.rb', line 17

def initialize
  @cookies = {}
end

Instance Method Details

#add(*cookies) ⇒ Object

Adds one or more cookies to the store.



22
23
24
25
26
# File 'lib/httpi/cookie_store.rb', line 22

def add(*cookies)
  cookies.each do |cookie|
    @cookies[cookie.name] = cookie.name_and_value
  end
end

#fetchObject

Returns the names and values for the “Cookie” header.



29
30
31
# File 'lib/httpi/cookie_store.rb', line 29

def fetch
  @cookies.values.join(";") unless @cookies.empty?
end