Class: PacketGen::Header::HTTP::Headers Abstract
- Inherits:
-
Object
- Object
- PacketGen::Header::HTTP::Headers
- Includes:
- Types::Fieldable
- Defined in:
- lib/packetgen/header/http/headers.rb
Overview
This class is abstract.
Base class for HTTP headers.
Instance Attribute Summary collapse
-
#data ⇒ Hash?
(also: #to_h)
readonly
Underlying Headers data (or nil).
Instance Method Summary collapse
-
#[](header) ⇒ String
Get header value from its name.
-
#from_human(data) ⇒ self
Read human-readable data to populate header data.
-
#given? ⇒ Boolean
Check if any headers were given.
-
#header?(header) ⇒ Boolean
(also: #has_header?)
Say if
self
includeheader
header. -
#initialize ⇒ Headers
constructor
A new instance of Headers.
-
#read(s_or_h) ⇒ self
Populate object from a string or directly from a hash.
-
#to_human ⇒ Hash
Get a human readable string.
-
#to_s ⇒ String
Get binary string.
Methods included from Types::Fieldable
#format_inspect, #sz, #type_name
Constructor Details
#initialize ⇒ Headers
Returns a new instance of Headers.
22 23 24 |
# File 'lib/packetgen/header/http/headers.rb', line 22 def initialize @data = {} end |
Instance Attribute Details
#data ⇒ Hash? (readonly) Also known as: to_h
Underlying Headers data (or nil).
19 20 21 |
# File 'lib/packetgen/header/http/headers.rb', line 19 def data @data end |
Instance Method Details
#[](header) ⇒ String
Get header value from its name
47 48 49 |
# File 'lib/packetgen/header/http/headers.rb', line 47 def [](header) data[header] end |
#from_human(data) ⇒ self
Read human-readable data to populate header data.
80 81 82 |
# File 'lib/packetgen/header/http/headers.rb', line 80 def from_human(data) read(data) end |
#given? ⇒ Boolean
Check if any headers were given.
86 87 88 89 90 |
# File 'lib/packetgen/header/http/headers.rb', line 86 def given? return true unless @data.nil? || @data.empty? false end |
#header?(header) ⇒ Boolean Also known as: has_header?
Say if self
include header
header
54 55 56 |
# File 'lib/packetgen/header/http/headers.rb', line 54 def header?(header) data.key?(header) end |
#read(s_or_h) ⇒ self
Populate object from a string or directly from a hash.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/packetgen/header/http/headers.rb', line 29 def read(s_or_h) case s_or_h when String @data = s_or_h.split("\n").map do |h| next unless h.include?(':') k, v = h.split(':', 2) [k, v.strip] end.compact.to_h when Hash @data = s_or_h end self end |
#to_human ⇒ Hash
Get a human readable string.
73 74 75 |
# File 'lib/packetgen/header/http/headers.rb', line 73 def to_human @data end |
#to_s ⇒ String
Get binary string.
61 62 63 64 65 66 67 68 69 |
# File 'lib/packetgen/header/http/headers.rb', line 61 def to_s return "\r\n" if @data.nil? || @data.empty? d = [] @data.map do |k, v| d << "#{k}: #{v}" end d.join("\r\n") << "\r\n\r\n" end |