Class: Ronin::Support::Network::HTTP::Cookie
- Inherits:
-
Object
- Object
- Ronin::Support::Network::HTTP::Cookie
- Includes:
- Enumerable
- Defined in:
- lib/ronin/support/network/http/cookie.rb
Overview
Parses and generates Cookie
header values.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#params ⇒ Hash{String => String}
readonly
Parameters of the cookie.
Class Method Summary collapse
-
.escape(string) ⇒ String
Escapes the string so that it can be embedded in a
Cookie
orSet-Cookie
header. -
.parse(string) ⇒ Cookie
Parses a
Cookie
string. -
.unescape(string, encoding = Encoding::UTF_8) ⇒ String
Unescapes a string that came from a
Cookie
orSet-Cookie
header.
Instance Method Summary collapse
-
#[](name) ⇒ String?
Fetches a param from the cookie.
-
#[]=(name, value) ⇒ #to_s
Sets a param in the cookie.
-
#each {|name, value| ... } ⇒ Enumerator
Enumerates over the params in the cookie.
-
#empty? ⇒ Boolean
Determines if the cookie is empty.
-
#has_param?(name) ⇒ Boolean
Determines if the cookie has the param with the given name.
-
#initialize(params = {}) ⇒ Cookie
constructor
Initializes the
Cookie
header. -
#initialize_copy(other) ⇒ Object
Initializes a copy of a cookie.
-
#merge(params) ⇒ Cookie
Merges the cookie with other cookie params.
-
#merge!(params) ⇒ self
Merges other cookie params into the cookie.
-
#to_h ⇒ Hash{String => String}
Converts the cookie into a Hash of names and values.
-
#to_s ⇒ String
Converts the cookie back into a
Cookie
value.
Methods included from Enumerable
Constructor Details
#initialize(params = {}) ⇒ Cookie
Initializes the Cookie
header.
48 49 50 |
# File 'lib/ronin/support/network/http/cookie.rb', line 48 def initialize(params={}) @params = params end |
Instance Attribute Details
Class Method Details
.escape(string) ⇒ String
Escapes the string so that it can be embedded in a Cookie
or
Set-Cookie
header.
72 73 74 |
# File 'lib/ronin/support/network/http/cookie.rb', line 72 def self.escape(string) URI.encode_www_form_component(string) end |
.parse(string) ⇒ Cookie
Parses a Cookie
string.
101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/ronin/support/network/http/cookie.rb', line 101 def self.parse(string) params = {} string.split(/;\s+/) do |field| key, value = field.split('=',2) params[unescape(key)] = unescape(value) end return new(params) end |
Instance Method Details
#[](name) ⇒ String?
Fetches a param from the cookie.
134 135 136 |
# File 'lib/ronin/support/network/http/cookie.rb', line 134 def [](name) @params[name.to_s] end |
#[]=(name, value) ⇒ #to_s
Sets a param in the cookie.
150 151 152 153 154 155 156 157 158 159 |
# File 'lib/ronin/support/network/http/cookie.rb', line 150 def []=(name,value) name = name.to_s case value when nil then @params.delete(name) else @params[name] = value.to_s end return value end |
#each {|name, value| ... } ⇒ Enumerator
Enumerates over the params in the cookie.
177 178 179 |
# File 'lib/ronin/support/network/http/cookie.rb', line 177 def each(&block) @params.each(&block) end |
#empty? ⇒ Boolean
Determines if the cookie is empty.
215 216 217 |
# File 'lib/ronin/support/network/http/cookie.rb', line 215 def empty? @params.empty? end |
#has_param?(name) ⇒ Boolean
Determines if the cookie has the param with the given name.
121 122 123 |
# File 'lib/ronin/support/network/http/cookie.rb', line 121 def has_param?(name) @params.has_key?(name.to_s) end |
#initialize_copy(other) ⇒ Object
Initializes a copy of a cookie.
58 59 60 |
# File 'lib/ronin/support/network/http/cookie.rb', line 58 def initialize_copy(other) @params = other.params.dup end |
#merge(params) ⇒ Cookie
Merges the cookie with other cookie params.
206 207 208 |
# File 'lib/ronin/support/network/http/cookie.rb', line 206 def merge(params) clone.merge!(params) end |
#merge!(params) ⇒ self
Merges other cookie params into the cookie.
189 190 191 192 193 194 195 |
# File 'lib/ronin/support/network/http/cookie.rb', line 189 def merge!(params) params.each do |name,value| self[name] = value end return self end |
#to_h ⇒ Hash{String => String}
Converts the cookie into a Hash of names and values.
225 226 227 |
# File 'lib/ronin/support/network/http/cookie.rb', line 225 def to_h @params end |
#to_s ⇒ String
Converts the cookie back into a Cookie
value.
235 236 237 238 239 |
# File 'lib/ronin/support/network/http/cookie.rb', line 235 def to_s @params.map { |name,value| "#{self.class.escape(name)}=#{self.class.escape(value)}" }.join('; ') end |