Class: PatientHttp::Payload
- Inherits:
-
Object
- Object
- PatientHttp::Payload
- Defined in:
- lib/patient_http/payload.rb
Overview
Handles encoding and decoding of HTTP response bodies for storage.
This class provides compression and encoding strategies for different content types to optimize storage and transmission of response data.
Instance Attribute Summary collapse
-
#charset ⇒ String?
readonly
The character set (if applicable).
-
#encoded_value ⇒ String
readonly
The encoded data.
-
#encoding ⇒ Symbol
readonly
The encoding type.
Class Method Summary collapse
-
.decode(encoded_value, encoding, charset) ⇒ String?
Decodes an encoded value based on its encoding type.
-
.encode(value, mimetype) ⇒ Array(Symbol, String, String)?
Encodes a value based on its MIME type.
-
.load(hash) ⇒ Payload?
Reconstructs a Payload from a hash representation.
Instance Method Summary collapse
-
#as_json ⇒ Hash
(also: #dump)
Converts to a hash representation for serialization.
-
#initialize(encoding, encoded_value, charset) ⇒ Payload
constructor
Initializes a new Payload.
-
#value ⇒ String?
Returns the decoded value.
Constructor Details
#initialize(encoding, encoded_value, charset) ⇒ Payload
Initializes a new Payload.
134 135 136 137 138 |
# File 'lib/patient_http/payload.rb', line 134 def initialize(encoding, encoded_value, charset) @encoded_value = encoded_value @encoding = encoding @charset = charset end |
Instance Attribute Details
#charset ⇒ String? (readonly)
Returns the character set (if applicable).
16 17 18 |
# File 'lib/patient_http/payload.rb', line 16 def charset @charset end |
#encoded_value ⇒ String (readonly)
Returns the encoded data.
13 14 15 |
# File 'lib/patient_http/payload.rb', line 13 def encoded_value @encoded_value end |
#encoding ⇒ Symbol (readonly)
Returns the encoding type.
10 11 12 |
# File 'lib/patient_http/payload.rb', line 10 def encoding @encoding end |
Class Method Details
.decode(encoded_value, encoding, charset) ⇒ String?
Decodes an encoded value based on its encoding type.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/patient_http/payload.rb', line 64 def decode(encoded_value, encoding, charset) return nil if encoded_value.nil? decoded_value = case encoding when :text encoded_value when :binary encoded_value.unpack1("m") when :gzipped Zlib.gunzip(encoded_value.unpack1("m")) end force_encoding(decoded_value, charset) end |
.encode(value, mimetype) ⇒ Array(Symbol, String, String)?
Encodes a value based on its MIME type.
For text-based content types, applies gzip compression if beneficial. For binary content, uses Base64 encoding.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/patient_http/payload.rb', line 37 def encode(value, mimetype) return nil if value.nil? if is_text_mimetype?(mimetype) value = text_value(value, charset(mimetype)) if value.bytesize < 4096 [:text, value, value.encoding.name] else gzipped = Zlib.gzip(value) if gzipped.bytesize < value.bytesize [:gzipped, [gzipped].pack("m0"), value.encoding.name] else [:text, value, value.encoding.name] end end else [:binary, [value].pack("m0"), Encoding::BINARY.name] end end |
.load(hash) ⇒ Payload?
Reconstructs a Payload from a hash representation.
23 24 25 26 27 |
# File 'lib/patient_http/payload.rb', line 23 def load(hash) return nil if hash.nil? || hash["value"].nil? new(hash["encoding"].to_sym, hash["value"], hash["charset"]) end |
Instance Method Details
#as_json ⇒ Hash Also known as: dump
Converts to a hash representation for serialization.
150 151 152 153 154 155 156 |
# File 'lib/patient_http/payload.rb', line 150 def as_json { "encoding" => encoding.to_s, "value" => encoded_value, "charset" => charset } end |
#value ⇒ String?
Returns the decoded value.
143 144 145 |
# File 'lib/patient_http/payload.rb', line 143 def value self.class.decode(encoded_value, encoding, charset) end |