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.
156 157 158 159 160 |
# File 'lib/patient_http/payload.rb', line 156 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.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/patient_http/payload.rb', line 56 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. A value that a text MIME type claims is text but that does not hold text is encoded as binary as well, because the serialized form must survive JSON encoding.
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/patient_http/payload.rb', line 39 def encode(value, mimetype) return nil if value.nil? if is_text_mimetype?(mimetype) text = text_value(value, charset(mimetype)) return encode_text(text) if text?(text) end [:binary, [value].pack("m0"), Encoding::BINARY.name] 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.
172 173 174 175 176 177 178 |
# File 'lib/patient_http/payload.rb', line 172 def as_json { "encoding" => encoding.to_s, "value" => encoded_value, "charset" => charset } end |
#value ⇒ String?
Returns the decoded value.
165 166 167 |
# File 'lib/patient_http/payload.rb', line 165 def value self.class.decode(encoded_value, encoding, charset) end |