/lib/httparty/cookie_hash.rb

http://github.com/jnunemaker/httparty · Ruby · 21 lines · 19 code · 2 blank · 0 comment · 1 complexity · f3f9227b2a586ceb78f03135f1ea93df MD5 · raw file

  1. class HTTParty::CookieHash < Hash #:nodoc:
  2. CLIENT_COOKIES = %w(path expires domain path secure httponly samesite)
  3. def add_cookies(data)
  4. case data
  5. when Hash
  6. merge!(data)
  7. when String
  8. data.split('; ').each do |cookie|
  9. key, value = cookie.split('=', 2)
  10. self[key.to_sym] = value if key
  11. end
  12. else
  13. raise "add_cookies only takes a Hash or a String"
  14. end
  15. end
  16. def to_cookie_string
  17. select { |k, v| !CLIENT_COOKIES.include?(k.to_s.downcase) }.collect { |k, v| "#{k}=#{v}" }.join("; ")
  18. end
  19. end