Sha256: 8b4c483994f7a834c5bc348e47ccab32a232bc6185c8c58fec3640995328383b

Contents?: true

Size: 590 Bytes

Versions: 2

Compression:

Stored size: 590 Bytes

Contents

# frozen_string_literal: true
require "yaml"
require "json"

module LoadFile
  class Parser
    ParserError = Class.new(StandardError)

    def self.yaml(content)
      if present?(content)
        YAML.safe_load(content, [Regexp, Symbol])
      else
        {}
      end
    rescue Psych::SyntaxError
      raise ParserError
    end

    def self.json(content)
      if present?(content)
        JSON.parse(content)
      else
        {}
      end
    rescue JSON::ParserError
      raise ParserError
    end

    def self.present?(string)
      string && !string.empty?
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
load_file-1.1.0 lib/load_file/parser.rb
load_file-1.0.0 lib/load_file/parser.rb