Class: StructureButcher::Parser

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

Instance Method Summary collapse

Instance Method Details

#load_base64(filename) ⇒ Object



182
183
184
185
# File 'lib/structurebutcher.rb', line 182

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

#load_hocon(filename) ⇒ Object



171
172
173
174
175
176
177
178
179
180
# File 'lib/structurebutcher.rb', line 171

def load_hocon(filename)
    result = nil
    begin
        result =  recursive_stringify_keys(Hocon.load(filename))
    rescue
        msg = "Error parsing '" + filename + "': " + $!.message
        raise AbortException.new(msg)
    end
    return result
end

#load_json(filename) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
# File 'lib/structurebutcher.rb', line 137

def load_json(filename)
    file = File.read(filename)
    result = nil
    begin
        result = recursive_stringify_keys(JSON.parse(file))
    rescue
        msg = "Error parsing '" + filename + "': " + $!.message
        raise ArgumentError.new(msg)
    end
    return result
end

#load_properties(filename) ⇒ Object



160
161
162
163
164
165
166
167
168
169
# File 'lib/structurebutcher.rb', line 160

def load_properties(filename)
    result = nil
    begin
        result = recursive_stringify_keys(JavaProperties.load(filename))
    rescue
        msg = "Error parsing '" + filename + "': " + $!.message
        raise AbortException.new(msg)
    end
    return result
end

#load_structure(filename, format) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/structurebutcher.rb', line 97

def load_structure(filename, format)
    begin
        case format
        when "json"
            return load_json(filename)
        when "yaml"
            return load_yaml(filename)
        when "properties"
            return load_properties(filename)
        when "javaprops"
            return load_properties(filename)
        when "hocon"
            return load_hocon(filename)
        when "base64"
            return load_base64(filename)
        else
            raise "Not implemented"
        end
    rescue Exception => e
        abort(e.message)
    end
end

#load_yaml(filename) ⇒ Object



149
150
151
152
153
154
155
156
157
158
# File 'lib/structurebutcher.rb', line 149

def load_yaml(filename)
    result = nil
    begin
        result = recursive_stringify_keys(YAML.load_file(filename))
    rescue
        msg = "Error parsing '" + filename + "': " + $!.message
        raise AbortException.new(msg)
    end
    return result
end

#recursive_stringify_keys(h) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/structurebutcher.rb', line 120

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