Class: PacketGen::Header::HTTP::Headers Abstract
- Inherits:
-
Object
- Object
- PacketGen::Header::HTTP::Headers
- 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
-
#from_human(data) ⇒ self
Read human-readable data to populate header data.
-
#given? ⇒ Boolean
Check if any headers were given.
-
#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.
Constructor Details
#initialize ⇒ Headers
Returns a new instance of Headers.
20 21 22 |
# File 'lib/packetgen/header/http/headers.rb', line 20 def initialize @data = nil end |
Instance Attribute Details
#data ⇒ Hash? (readonly) Also known as: to_h
Underlying Headers data (or nil).
17 18 19 |
# File 'lib/packetgen/header/http/headers.rb', line 17 def data @data end |
Instance Method Details
#from_human(data) ⇒ self
Read human-readable data to populate header data.
63 64 65 |
# File 'lib/packetgen/header/http/headers.rb', line 63 def from_human(data) read(data) end |
#given? ⇒ Boolean
Check if any headers were given.
69 70 71 72 73 |
# File 'lib/packetgen/header/http/headers.rb', line 69 def given? return true unless @data.nil? || @data.empty? false end |
#read(s_or_h) ⇒ self
Populate object from a string or directly from a hash.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/packetgen/header/http/headers.rb', line 27 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.reject(&:nil?).to_h when Hash @data = s_or_h end self end |
#to_human ⇒ Hash
Get a human readable string.
56 57 58 |
# File 'lib/packetgen/header/http/headers.rb', line 56 def to_human @data end |
#to_s ⇒ String
Get binary string.
44 45 46 47 48 49 50 51 52 |
# File 'lib/packetgen/header/http/headers.rb', line 44 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 |