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)



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

#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



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.

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



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.

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



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

#valueString?

Returns the decoded value.

Returns:

  • (String, nil)

    the decoded data



143
144
145
# File 'lib/patient_http/payload.rb', line 143

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