Class: StructureButcher::Parser

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

Instance Method Summary collapse

Instance Method Details

#load_base64(filename) ⇒ Object



109
110
111
112
# File 'lib/structurebutcher.rb', line 109

def load_base64(filename)
    content = File.read(filename)
    return Base64.encode64(content)
end

#load_hocon(filename) ⇒ Object



105
106
107
# File 'lib/structurebutcher.rb', line 105

def load_hocon(filename)
    return recursive_stringify_keys(Hocon.load(filename))
end

#load_json(filename) ⇒ Object



92
93
94
95
# File 'lib/structurebutcher.rb', line 92

def load_json(filename)
    file = File.read(filename)
    return recursive_stringify_keys(JSON.parse(file))
end

#load_properties(filename) ⇒ Object



101
102
103
# File 'lib/structurebutcher.rb', line 101

def load_properties(filename)
    return recursive_stringify_keys(JavaProperties.load(filename))
end

#load_structure(filename, format) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/structurebutcher.rb', line 58

def load_structure(filename, format)
    case format
    when "json"
        return load_json(filename)
    when "yaml"
        return load_yaml(filename)
    when "properties"
        return load_properties(filename)
    when "hocon"
        return load_hocon(filename)
    when "base64"
        return load_base64(filename)
    else
        throw "Not implemented"
    end
end

#load_yaml(filename) ⇒ Object



97
98
99
# File 'lib/structurebutcher.rb', line 97

def load_yaml(filename)
    return recursive_stringify_keys(YAML.load_file(filename))
end

#recursive_stringify_keys(h) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/structurebutcher.rb', line 75

def recursive_stringify_keys(h)
    case h
    when Hash
        Hash[
            h.map do |k, v|
                [ k.respond_to?(:to_s) ? k.to_s : k,
                  recursive_stringify_keys(v)
                ]
            end
        ]
    when Enumerable
        h.map { |v| recursive_stringify_keys(v) }
    else
        h
    end
end