Class: JsonConfig
- Inherits:
-
Object
- Object
- JsonConfig
- Defined in:
- lib/resources/json.rb
Overview
Parses a json document Usage: describe json(‘policyfile.lock.json’) do
its('cookbook_locks.omnibus.version') { should eq('2.2.0') }
end
Direct Known Subclasses
Instance Attribute Summary collapse
-
#params ⇒ Object
readonly
make params readable.
Instance Method Summary collapse
- #extract_value(keys, value) ⇒ Object
-
#initialize(path) ⇒ JsonConfig
constructor
A new instance of JsonConfig.
-
#method_missing(name) ⇒ Object
Shorthand to retrieve a parameter name via ‘#its`.
- #parse(content) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(path) ⇒ JsonConfig
Returns a new instance of JsonConfig.
16 17 18 19 20 |
# File 'lib/resources/json.rb', line 16 def initialize(path) @path = path @file_content = inspec.file(@path).content @params = parse(@file_content) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name) ⇒ Object
Shorthand to retrieve a parameter name via ‘#its`. Example: describe json(’file’) { its(‘paramX’) { should eq ‘Y’ } }
57 58 59 |
# File 'lib/resources/json.rb', line 57 def method_missing(name) @params[name.to_s] end |
Instance Attribute Details
#params ⇒ Object (readonly)
make params readable
14 15 16 |
# File 'lib/resources/json.rb', line 14 def params @params end |
Instance Method Details
#extract_value(keys, value) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/resources/json.rb', line 27 def extract_value(keys, value) key = keys.shift return nil if key.nil? # check if key is a num, try to extract from array if key.to_i.to_s == key value = value[key.to_i] # if value is an array, iterate over each child elsif value.is_a?(Array) value = value.map { |i| extract_value([key], i) } # normal value extraction else value = value[key].nil? ? nil : value[key] end # check if further keys exist if !keys.first.nil? return extract_value(keys.clone, value) else return value end end |
#parse(content) ⇒ Object
22 23 24 25 |
# File 'lib/resources/json.rb', line 22 def parse(content) require 'json' JSON.parse(content) end |
#to_s ⇒ Object
61 62 63 |
# File 'lib/resources/json.rb', line 61 def to_s "Json #{@path}" end |