Class: Hyde::Cookie
- Inherits:
-
Object
- Object
- Hyde::Cookie
- Defined in:
- lib/hyde/util/cookie.rb
Overview
Utility class for handling cookies
Instance Attribute Summary collapse
-
#key ⇒ Object
Returns the value of attribute key.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#value ⇒ Object
Returns the value of attribute value.
Class Method Summary collapse
-
.from_cookie_string(data) ⇒ Array(Cookie)
Create cookie(s) from a "Cookie: " format.
-
.from_setcookie_string(data) ⇒ Cookie
Create cookie from a "Set-Cookie: " format.
Instance Method Summary collapse
-
#initialize(key, value, params = {}) ⇒ Cookie
constructor
A new instance of Cookie.
-
#to_s ⇒ String
Convert cookie to "Set-Cookie: " string representation.
-
#to_short ⇒ Object
Convert cookie to "Cookie: " string representation (no params).
Constructor Details
#initialize(key, value, params = {}) ⇒ Cookie
Returns a new instance of Cookie.
10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/hyde/util/cookie.rb', line 10 def initialize(key, value, params = {}) unless key.match? Hyde::Util::HeaderRegexp::COOKIE_KEY raise Hyde::ParsingError, "invalid cookie key: #{key}" end unless value.match? Hyde::Util::HeaderRegexp::COOKIE_VALUE raise Hyde::ParsingError, "invalid cookie value: #{value}" end @key = key @value = value @params = params end |
Instance Attribute Details
#key ⇒ Object
Returns the value of attribute key.
35 36 37 |
# File 'lib/hyde/util/cookie.rb', line 35 def key @key end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
36 37 38 |
# File 'lib/hyde/util/cookie.rb', line 36 def params @params end |
#value ⇒ Object
Returns the value of attribute value.
35 36 37 |
# File 'lib/hyde/util/cookie.rb', line 35 def value @value end |
Class Method Details
.from_cookie_string(data) ⇒ Array(Cookie)
Create cookie(s) from a "Cookie: " format
53 54 55 56 57 |
# File 'lib/hyde/util/cookie.rb', line 53 def self.(data) data.split(";").map do || Cookie.new(*.split("=").map(&:strip)) end end |
.from_setcookie_string(data) ⇒ Cookie
Create cookie from a "Set-Cookie: " format
41 42 43 44 45 46 47 48 |
# File 'lib/hyde/util/cookie.rb', line 41 def self.(data) kvpair, params = Hyde::Util::ParserCommon.parse_value( data, regexp: Hyde::Util::HeaderRegexp::COOKIE_PARAM ) key, value = kvpair.split("=").map(&:strip) Cookie.new(key, value, params) end |
Instance Method Details
#to_s ⇒ String
Convert cookie to "Set-Cookie: " string representation.
26 27 28 |
# File 'lib/hyde/util/cookie.rb', line 26 def to_s Hyde::Util.make_value(to_short, @params) end |
#to_short ⇒ Object
Convert cookie to "Cookie: " string representation (no params)
31 32 33 |
# File 'lib/hyde/util/cookie.rb', line 31 def to_short "#{key.to_s.strip}=#{value.to_s.strip}" end |