Class: Hyde::Cookie

Inherits:
Object
  • Object
show all
Defined in:
lib/hyde/util/cookie.rb

Overview

Utility class for handling cookies

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, value, params = {}) ⇒ Cookie

Returns a new instance of Cookie.

Parameters:

  • data (String)

    raw cookie data



9
10
11
12
13
# File 'lib/hyde/util/cookie.rb', line 9

def initialize(key, value, params = {})
  @key = key
  @value = value
  @params = params
end

Instance Attribute Details

#keyObject

Returns the value of attribute key.



26
27
28
# File 'lib/hyde/util/cookie.rb', line 26

def key
  @key
end

#paramsObject (readonly)

Returns the value of attribute params.



27
28
29
# File 'lib/hyde/util/cookie.rb', line 27

def params
  @params
end

#valueObject

Returns the value of attribute value.



26
27
28
# File 'lib/hyde/util/cookie.rb', line 26

def value
  @value
end

Class Method Details

Create cookie(s) from a "Cookie: " format

Parameters:

  • data (String)

    value part of "Cookie: " header

Returns:



41
42
43
44
45
# File 'lib/hyde/util/cookie.rb', line 41

def self.from_cookie_string(data)
  data.split(";").map do |cookiestr|
    Cookie.new(*cookiestr.split("=").map(&:strip))
  end
end

.from_setcookie_string(data) ⇒ Cookie

Create cookie from a "Set-Cookie: " format

Parameters:

  • data (String)

    value part of "Set-Cookie: " header

Returns:



32
33
34
35
36
# File 'lib/hyde/util/cookie.rb', line 32

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

Instance Method Details

#to_sString

Convert cookie to "Set-Cookie: " string representation.

Returns:

  • (String)


17
18
19
# File 'lib/hyde/util/cookie.rb', line 17

def to_s
  Hyde::Util.make_value(to_short, @params)
end

#to_shortObject

Convert cookie to "Cookie: " string representation (no params)



22
23
24
# File 'lib/hyde/util/cookie.rb', line 22

def to_short
  "#{key.to_s.strip}=#{value.to_s.strip}"
end