class Inspec::Resources::JsonConfig

Attributes

params[R]

make params readable

raw_content[R]

make params readable

Public Class Methods

new(opts) click to toggle source
# 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

Public Instance Methods

method_missing(*keys) click to toggle source

Shorthand to retrieve a parameter name via `#its`. Example: describe json('file') { its('paramX') { should eq 'Y' } }

@param [String] name name of the field to retrieve @return [Object] the value stored at this position

# 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
to_s() click to toggle source
# File lib/inspec/resources/json.rb, line 62
def to_s
  "#{resource_base_name} #{@resource_name_supplement || "content"}"
end
value(key) click to toggle source
# 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

Private Instance Methods

load_raw_content(opts) click to toggle source
# File lib/inspec/resources/json.rb, line 75
def load_raw_content(opts)
  # if the opts isn't a hash, we assume it's a path to a file
  unless opts.is_a?(Hash)
    @resource_name_supplement = opts
    return load_raw_from_file(opts)
  end

  if opts.key?(:command)
    @resource_name_supplement = "from command: #{opts[:command]}"
    load_raw_from_command(opts[:command])
  elsif opts.key?(:content)
    opts[:content]
  else
    raise Inspec::Exceptions::ResourceFailed, "No JSON content; must specify a file, command, or raw JSON content"
  end
end
load_raw_from_command(command) click to toggle source
# File lib/inspec/resources/json.rb, line 96
def load_raw_from_command(command)
  result = inspec.command(command)

  return result.stdout unless result.stdout.empty?

  msg = if result.stderr.empty?
          "No JSON output, STDERR was empty"
        else
          "No JSON output, STDERR:\n #{result.stderr}"
        end

  raise Inspec::Exceptions::ResourceFailed, msg
end
load_raw_from_file(path) click to toggle source
# File lib/inspec/resources/json.rb, line 92
def load_raw_from_file(path)
  read_file_content(path)
end
parse(content) click to toggle source
# File lib/inspec/resources/json.rb, line 68
def parse(content)
  require "json" unless defined?(JSON)
  JSON.parse(content)
rescue => e
  raise Inspec::Exceptions::ResourceFailed, "Unable to parse JSON: #{e.message}"
end
resource_base_name() click to toggle source

for resources the subclass JsonConfig, this allows specification of the resource base name in each subclass so we can build a good to_s method

# File lib/inspec/resources/json.rb, line 112
def resource_base_name
  "JSON"
end