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



177
178
179
180
181
182
183
184
# File 'lib/unwind.rb', line 177

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



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/unwind.rb', line 157

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


173
174
175
# File 'lib/unwind.rb', line 173

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