Class: RailsHttpLab::Bruno::Serializer

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_http_lab/bruno/serializer.rb

Overview

Round-trip-stable serializer for Bruno documents.

Format per block:

<name> {
  key: value
}

Raw blocks emit their ‘raw` content verbatim between ’and ’‘. Blocks are separated by a single blank line, matching Bruno’s output.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document) ⇒ Serializer

Returns a new instance of Serializer.



18
19
20
# File 'lib/rails_http_lab/bruno/serializer.rb', line 18

def initialize(document)
  @document = document
end

Class Method Details

.dump(document) ⇒ Object



14
15
16
# File 'lib/rails_http_lab/bruno/serializer.rb', line 14

def self.dump(document)
  new(document).dump
end

Instance Method Details

#dumpObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rails_http_lab/bruno/serializer.rb', line 22

def dump
  out = +""
  out << ("\n" * @document.leading_blank_lines)

  @document.blocks.each_with_index do |block, idx|
    out << "\n" if idx > 0
    out << render_block(block)
  end

  if @document.trailing_newline && !out.end_with?("\n")
    out << "\n"
  elsif !@document.trailing_newline && out.end_with?("\n")
    out.chomp!
  end

  out
end