Class: Unwind::CookieHash

Inherits:
Hash
  • Object
show all
Defined in:
lib/unwind.rb

Overview

borrowed (stolen) from HTTParty with minor updates to handle all cookies existing in a single string

Constant Summary collapse

CLIENT_COOKIES =
%w{path expires domain path secure httponly}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details



164
165
166
167
168
169
170
171
# File 'lib/unwind.rb', line 164

def self.to_cookie_string(*cookie_strings)
  h = CookieHash.new
  cookie_strings.each do |cs|
    h.add_cookies(cs)
  end

  h.to_cookie_string
end

Instance Method Details

#add_cookies(value) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/unwind.rb', line 144

def add_cookies(value)
  case value
  when Hash
    merge!(value)
  when String
    value = value.gsub(/expires=[\w,\s\-\:]+;/i, '')
    value = value.gsub(/httponly[\,\;]*/i, '')
    value.split(/[;,]\s/).each do |cookie|
      array = cookie.split('=')
      self[array[0].strip.to_sym] = array[1]
    end
  else
    raise "add_cookies only takes a Hash or a String"
  end
end


160
161
162
# File 'lib/unwind.rb', line 160

def to_cookie_string
  delete_if { |k, v| CLIENT_COOKIES.include?(k.to_s.downcase) }.collect { |k, v| "#{k}=#{v}" }.join("; ")
end