Skip to content

Commit

Permalink
Fixes #19 - add filter validation
Browse files Browse the repository at this point in the history
  * Previously there was not a way to validate if a filter was
    legit or not.  This adds a new method that exposes the jgrep
    validation interface to facterdb.

    Additionally, it also refactors the get_facts method by adding
    an intermediary method to generate the filter string.

    I have also added a new error class for throwing a better error
    message to the user.

    Lastly, I added some method documention for all the methods I touched.
  • Loading branch information
logicminds committed Oct 9, 2020
1 parent d5cd0b9 commit 6816271
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
41 changes: 31 additions & 10 deletions lib/facterdb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
require 'jgrep'

module FacterDB
module Errors
class InvalidFilter < RuntimeError; end
end

# @return [String] - returns a giant incomprehensible string of concatenated json data
def self.database
Expand Down Expand Up @@ -92,18 +95,36 @@ def self.get_os_facts(facter_version='*', filter=[])
get_facts(filter_str)
end

def self.get_facts(filter=nil)
if filter.is_a?(Array)
filter_str = '(' + filter.map { |f| f.map { |k,v | "#{k}=#{v}" }.join(' and ') }.join(') or (') + ')'
elsif filter.is_a?(Hash)
filter_str = filter.map { |k,v | "#{k}=#{v}" }.join(' and ')
elsif filter.is_a?(String)
filter_str = filter
elsif filter == nil
filter_str = ''
# @return [String] - the string filter
# @param filter [Object] - The filter to convert to jgrep string
def generate_filter_str(filter=nil)
case filter
when ::Array
'(' + filter.map { |f| f.map { |k,v | "#{k}=#{v}" }.join(' and ') }.join(') or (') + ')'
when ::Hash
filter.map { |k,v | "#{k}=#{v}" }.join(' and ')
when ::String
filter
when ::NilClass
''
else
raise 'filter must be either an Array a Hash or a String'
raise Errors::InvalidFilter, "filter must be either an Array a Hash, String, or nil, received #{filter.class}"
end
end

# @return [Boolean] - true if the filter is valid against the jgrep filter validator
# @param filter [Object] - The filter to convert to jgrep string
def self.valid_filters?(filters)
filter_str = generate_filter_str(filters)
JGrep.validate_filters(filter_str).nil?
rescue RuntimeError
false
end

# @return [Array] - array of hashes of facts
# @param filter [Object] - The filter to convert to jgrep string
def self.get_facts(filter=nil)
filter_str = generate_filter_str(filter)
JGrep.jgrep(database, filter_str).map { |hash| Hash[hash.map{ |k, v| [k.to_sym, v] }] }
end
end
10 changes: 10 additions & 0 deletions spec/facterdb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,16 @@
end
end

describe '.valid_filters?' do
it 'invalid and false' do
expect( FacterDB.valid_filters?('and')).to be_falsey
end

it 'valid and true' do
expect( FacterDB.valid_filters?('foo')).to be_truthy
end
end

describe '.get_facts' do
subject(:result) { FacterDB.get_facts(filter) }

Expand Down

0 comments on commit 6816271

Please sign in to comment.