Module: Yamljson

Defined in:
lib/yamljson.rb,
lib/yamljson/version.rb

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.json2yaml(filename) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/yamljson.rb', line 25

def self.json2yaml(filename)
  begin
    JSON.parse(read_file(filename)).to_yaml
  rescue JSON::ParserError
    raise "syntax error in #{filename}"
  end
end

.read_file(filename) ⇒ Object

read whole file into a string, raise nice errors if files missing



8
9
10
11
12
13
14
15
# File 'lib/yamljson.rb', line 8

def self.read_file(filename)
  if File.exists?(filename)
    data = File.readlines(filename)
  else
    raise "File not found: #{filename}"
  end
  data.join("\n")
end

.yaml2json(filename) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/yamljson.rb', line 17

def self.yaml2json(filename)
  begin
    YAML.load(read_file(filename)).to_json
  rescue Psych::SyntaxError
    raise "syntax error in #{filename}"
  end
end