Class: Rack::Client::Parser::YAML

Inherits:
Base
  • Object
show all
Defined in:
lib/rack/client/parser/yaml.rb

Constant Summary

Constants inherited from Base

Base::CONTENT_TYPE

Instance Method Summary collapse

Methods inherited from Base

content_type, lookup, type_table

Instance Method Details

#decode(body) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rack/client/parser/yaml.rb', line 20

def decode(body)
  BodyCollection.new do |collection|
    begin
      io = if body.respond_to? :to_path
             File.open(body.to_path, 'r')
           else
             io = StringIO.new

             body.each do |part|
               io << part
             end

             io.rewind
             io
           end

      ::YAML.load_documents(io) do |object|
        collection << object
      end

      collection.finish
    ensure
      io.close if io.respond_to? :close
      body.close if body.respond_to? :close
    end
  end
end

#encode(input) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/rack/client/parser/yaml.rb', line 10

def encode(input)
  output = StringIO.new

  input.each do |object|
    ::YAML.dump(object, output)
  end

  output
end