Module: Plato::HeadersCodec

Extended by:
HeadersCodec
Included in:
HeadersCodec
Defined in:
lib/plato/headers_codec.rb

Defined Under Namespace

Modules: Sanitize

Instance Method Summary collapse

Instance Method Details

#deflate(hash) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/plato/headers_codec.rb', line 5

def deflate(hash)
  hash = stringify_keys(hash)
  body = hash.delete('body')

  [].tap do |buffer|
    buffer << hash.map do |key, value|
      "#{header_for(key)}: #{Sanitize.header(value)}"
    end.join("\n")

    buffer << "\n\n" << Sanitize.body(body) if body
    buffer << "\n" unless buffer.last =~ /\n\Z/
  end
end

#inflate(string) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/plato/headers_codec.rb', line 19

def inflate(string)
  {}.tap do |result|
    headers, body = string.split(/\n\n/, 2)

    headers.split("\n").each do |line|
      header, val = line.split(/:\s*/, 2)

      result.update hash_key_for(header) => deserialize_value(val)
    end

    result['body'] = body.chomp if body
  end
end