No commit activity in last 3 years
No release in over 3 years
The Ruby standard library defines YAML.safe_load and YAML.load_stream but there's no way to safely load a multi document stream. This Gem adds YAML.safe_load_stream.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 3.0
 Project Readme

YAMLSafeLoadStream

Build Status Gem Version Yard Docs

The Ruby standard library YAML/Psych module defines YAML.safe_load for safely loading YAML documents

There's also YAML.load_stream for loading multi-document streams.

The problem is, there's no YAML.safe_load_stream that would safely load a stream with multiple documents.

This gem adds a very bare bones implementation of that missing feature.

Usage

There is just one method: safe_load_stream(yaml_content, optional_filename_to_be_used_in_exception_messages)

When used with a block, it will yield each document separately:

YAMLSafeLoadStream.safe_load_stream("---\nhello: world\n\n---\nhello: universe\n") do |document|
  puts document.inspect
end

# outputs:
# { 'hello' => 'world' }
# { 'hello' => 'universe' }

When used without a block, it will return an Array containing documents in the stream:

docs = YAMLSafeLoadStream.safe_load_stream("---\nhello: world\n\n---\nhello: universe\n")
puts docs.inspect

# outputs:
# [{ 'hello' => 'world' }, { 'hello' => 'universe' }]

Directly

require 'yaml/safe_load_stream'

YAMLSafeLoadStream.safe_load_stream("---\nhello: world\n\n---\nhello: universe\n")

As a refinement

require 'yaml/safe_load_stream'
using YAMLSafeLoadStream

YAML.safe_load_stream("---\nhello: world\n\n---\nhello: universe\n")

Requiring the core-ext

require 'yaml/safe_load_stream/core-ext'
YAML.safe_load_stream("---\nhello: world\n\n---\nhello: universe\n")

Installation

Add this line to your application's Gemfile:

gem 'yaml-safe_load_stream'

And then execute:

$ bundle

Or install it yourself as:

$ gem install yaml-safe_load_stream

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/kontena/yaml-safe_load_stream