|
|
@ -0,0 +1,47 @@ |
|
|
|
# frozen_string_literal: true |
|
|
|
|
|
|
|
require_relative 'header' |
|
|
|
|
|
|
|
module Hyde |
|
|
|
# Utility class for handling cookies |
|
|
|
class Cookie |
|
|
|
# @param data [String] raw cookie data |
|
|
|
def initialize(key, value, params = {}) |
|
|
|
@key = key |
|
|
|
@value = value |
|
|
|
@params = params |
|
|
|
end |
|
|
|
|
|
|
|
# Convert cookie to "Set-Cookie: " string representation. |
|
|
|
# @return [String] |
|
|
|
def to_s |
|
|
|
Hyde::Util.make_value(to_short, @params) |
|
|
|
end |
|
|
|
|
|
|
|
# Convert cookie to "Cookie: " string representation (no params) |
|
|
|
def to_short |
|
|
|
"#{key.to_s.strip}=#{value.to_s.strip}" |
|
|
|
end |
|
|
|
|
|
|
|
attr_accessor :key, :value |
|
|
|
attr_reader :params |
|
|
|
|
|
|
|
# Create cookie from a "Set-Cookie: " format |
|
|
|
# @param data [String] value part of "Set-Cookie: " header |
|
|
|
# @return [Cookie] |
|
|
|
def self.from_setcookie_string(data) |
|
|
|
kvpair, params = Hyde::Util.parse_value(data) |
|
|
|
key, value = kvpair.split("=").map(&:strip) |
|
|
|
Cookie.new(key, value, params) |
|
|
|
end |
|
|
|
|
|
|
|
# Create cookie(s) from a "Cookie: " format |
|
|
|
# @param data [String] value part of "Cookie: " header |
|
|
|
# @return [Array(Cookie)] |
|
|
|
def self.from_cookie_string(data) |
|
|
|
data.split(";").map do |cookiestr| |
|
|
|
Cookie.new(*cookiestr.split("=").map(&:strip)) |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
end |