Module: HTTY::CookiesUtil

Defined in:
lib/htty/cookies_util.rb

Overview

Provides support for marshaling HTTP cookies to and from strings.

Constant Summary collapse

'='
COOKIES_DELIMITER =
'; '

Class Method Summary collapse

Class Method Details

.cookies_from_string(cookies_string) ⇒ Object

Returns the specified cookies_string HTTP header value deserialized to an array of cookies.



8
9
10
11
12
13
14
15
# File 'lib/htty/cookies_util.rb', line 8

def self.cookies_from_string(cookies_string)
  return [] unless cookies_string
  cookies_string.split(COOKIES_DELIMITER).collect do |name_value_string|
    name_and_value = name_value_string.split(COOKIE_NAME_VALUE_DELIMITER, 2)
    name_and_value << nil if (name_and_value.length < 2)
    name_and_value
  end
end

.cookies_to_string(cookies) ⇒ Object

Returns the specified array of cookies serialized to an HTTP header value. Returns nil if cookies is nil or empty or if it contains only nil cookie values.



20
21
22
23
24
25
26
27
# File 'lib/htty/cookies_util.rb', line 20

def self.cookies_to_string(cookies)
  cookies = Array(cookies)
  return nil if cookies.empty?

  cookies.collect { |name, value|
    [name, value].compact.join COOKIE_NAME_VALUE_DELIMITER
  }.join COOKIES_DELIMITER
end