Class: PatientHttp::Payload

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(encoding, encoded_value, charset) ⇒ Payload

Initializes a new Payload.

Parameters:

  • encoding (Symbol)

    the encoding type

  • encoded_value (String)

    the encoded data

  • charset (String, nil)

    the character set (if applicable)



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

#charsetString? (readonly)

Returns the character set (if applicable).

Returns:

  • (String, nil)

    the character set (if applicable)



16
17
18
# File 'lib/patient_http/payload.rb', line 16

def charset
  @charset
end

#encoded_valueString (readonly)

Returns the encoded data.

Returns:

  • (String)

    the encoded data



13
14
15
# File 'lib/patient_http/payload.rb', line 13

def encoded_value
  @encoded_value
end

#encodingSymbol (readonly)

Returns the encoding type.

Returns:

  • (Symbol)

    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.

Parameters:

  • encoded_value (String)

    the encoded data

  • encoding (Symbol)

    the encoding type (:text, :binary, :gzipped)

  • charset (String, nil)

    the character set (if applicable)

Returns:

  • (String, nil)

    the decoded value or nil if encoded_value is nil



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.

Parameters:

  • value (String)

    the value to encode

  • mimetype (String, nil)

    the MIME type of the content

Returns:

  • (Array(Symbol, String, String), nil)

    [encoding, encoded_value, charset] or nil if value is nil



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.

Parameters:

  • hash (Hash, nil)

    hash with "encoding" and "value" keys

Returns:

  • (Payload, nil)

    reconstructed payload or nil if hash is invalid



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_jsonHash Also known as: dump

Converts to a hash representation for serialization.

Returns:

  • (Hash)

    hash with "encoding" and "value" keys



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

#valueString?

Returns the decoded value.

Returns:

  • (String, nil)

    the decoded data



165
166
167
# File 'lib/patient_http/payload.rb', line 165

def value
  self.class.decode(encoded_value, encoding, charset)
end