Class: Inspec::Resources::JsonConfig
- Inherits:
-
Object
- Object
- Inspec::Resources::JsonConfig
- Includes:
- FileReader, ObjectTraverser
- Defined in:
- lib/inspec/resources/json.rb
Direct Known Subclasses
CassandradbConf, CsvConfig, IniConfig, MongodbConf, Opa, TomlConfig, XmlConfig, YamlConfig
Instance Attribute Summary collapse
-
#params ⇒ Object
readonly
make params readable.
-
#raw_content ⇒ Object
readonly
make params readable.
Instance Method Summary collapse
-
#initialize(opts) ⇒ JsonConfig
constructor
A new instance of JsonConfig.
-
#method_missing(*keys) ⇒ Object
Shorthand to retrieve a parameter name via ‘#its`.
- #resource_id ⇒ Object
- #to_s ⇒ Object
- #value(key) ⇒ Object
Methods included from FileReader
Methods included from ObjectTraverser
Constructor Details
#initialize(opts) ⇒ JsonConfig
Returns a new instance of JsonConfig.
30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/inspec/resources/json.rb', line 30 def initialize(opts) # pre-initialize @params to an empty hash. In the event that reading/parsing the data # throws an exception, this allows the resource to still be called outside of a # describe/test and not throw errors when a caller attempts to fetch a value from the params. @params = {} # load the raw content from the source, and then parse it @raw_content = load_raw_content(opts) @params = parse(@raw_content) # If the JSON content is enumerable, make this object enumerable too extend EnumerableDelegation if @params.respond_to?(:each) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(*keys) ⇒ Object
Shorthand to retrieve a parameter name via ‘#its`. Example: describe json(’file’) { its(‘paramX’) { should eq ‘Y’ } }
49 50 51 52 53 54 |
# File 'lib/inspec/resources/json.rb', line 49 def method_missing(*keys) # catch bahavior of rspec its implementation # @see https://github.com/rspec/rspec-its/blob/v1.2.0/lib/rspec/its.rb#L110 keys.shift if keys.is_a?(Array) && keys[0] == :[] value(keys) end |
Instance Attribute Details
#params ⇒ Object (readonly)
make params readable
28 29 30 |
# File 'lib/inspec/resources/json.rb', line 28 def params @params end |
#raw_content ⇒ Object (readonly)
make params readable
28 29 30 |
# File 'lib/inspec/resources/json.rb', line 28 def raw_content @raw_content end |
Instance Method Details
#resource_id ⇒ Object
62 63 64 |
# File 'lib/inspec/resources/json.rb', line 62 def resource_id @resource_name_supplement || "#{resource_base_name}'s content" end |
#to_s ⇒ Object
66 67 68 |
# File 'lib/inspec/resources/json.rb', line 66 def to_s "#{resource_base_name} #{@resource_name_supplement || "content"}" end |
#value(key) ⇒ Object
56 57 58 59 60 |
# File 'lib/inspec/resources/json.rb', line 56 def value(key) # uses ObjectTraverser.extract_value to walk the hash looking for the key, # which may be an Array of keys for a nested Hash. extract_value(key, params) end |