Class: Inspec::Resources::JsonConfig
- Inherits:
-
Object
- Object
- Inspec::Resources::JsonConfig
- Includes:
- ObjectTraverser
- Defined in:
- lib/resources/json.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#params ⇒ 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`.
- #parse(content) ⇒ Object
- #to_s ⇒ Object
- #value(key) ⇒ Object
Methods included from ObjectTraverser
Constructor Details
#initialize(opts) ⇒ JsonConfig
Returns a new instance of JsonConfig.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/resources/json.rb', line 31 def initialize(opts) @opts = opts if opts.is_a?(Hash) if opts.key?(:content) @file_content = opts[:content] elsif opts.key?(:command) @command = inspec.command(opts[:command]) @file_content = @command.stdout end else @path = opts @file = inspec.file(@opts) @file_content = @file.content # check if file is available if !@file.file? skip_resource "Can't find file \"#{@path}\"" return @params = {} end # check if file is readable if @file_content.nil? && !@file.empty? skip_resource "Can't read file \"#{@path}\"" return @params = {} end end @params = parse(@file_content) 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’ } }
75 76 77 78 79 80 |
# File 'lib/resources/json.rb', line 75 def method_missing(*keys) # catch bahavior of rspec its implementation # @see https://github.com/rspec/rspec-its/blob/master/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
29 30 31 |
# File 'lib/resources/json.rb', line 29 def params @params end |
Instance Method Details
#parse(content) ⇒ Object
61 62 63 64 |
# File 'lib/resources/json.rb', line 61 def parse(content) require 'json' JSON.parse(content) end |
#to_s ⇒ Object
82 83 84 85 86 87 88 |
# File 'lib/resources/json.rb', line 82 def to_s if @opts.is_a?(Hash) && @opts.key?(:content) 'Json content' else "Json #{@path}" end end |
#value(key) ⇒ Object
66 67 68 |
# File 'lib/resources/json.rb', line 66 def value(key) extract_value(key, @params) end |