Class: LoadFile::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/load_file/parser.rb

Constant Summary collapse

ParserError =
Class.new(StandardError)

Class Method Summary collapse

Class Method Details

.json(content) ⇒ Object


19
20
21
22
23
24
25
26
27
# File 'lib/load_file/parser.rb', line 19

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

.present?(string) ⇒ Boolean

Returns:

  • (Boolean)

29
30
31
# File 'lib/load_file/parser.rb', line 29

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

.yaml(content) ⇒ Object


9
10
11
12
13
14
15
16
17
# File 'lib/load_file/parser.rb', line 9

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