Class: Chef::Attribute::Validator::Check::LooksLike

Inherits:
Chef::Attribute::Validator::Check show all
Defined in:
lib/chef-attribute-validator/checks/looks_like.rb

Instance Attribute Summary

Attributes inherited from Chef::Attribute::Validator::Check

#check_arg, #path_spec, #rule_name

Instance Method Summary collapse

Methods inherited from Chef::Attribute::Validator::Check

#initialize, list_check_types, make

Constructor Details

This class inherits a constructor from Chef::Attribute::Validator::Check

Instance Method Details

#check(attrset) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/chef-attribute-validator/checks/looks_like.rb', line 24

def check(attrset)
  violations = []
  attrset.each do |path, value|
    if val_scalar?(value) then
      next if value.nil?
      case check_arg
      when 'ip'
        begin
          IPAddr.new(value)
        rescue
          violations.push Chef::Attribute::Validator::Violation.new(rule_name, path, "Value '#{value}' does not look like an IP address")
        end
      when 'url'
        begin
          URI(value)
        rescue
          violations.push Chef::Attribute::Validator::Violation.new(rule_name, path, "Value '#{value}' does not look like a URL")
        end
      end
    end
  end
  violations
end

#validate_check_argObject



13
14
15
16
17
18
19
20
21
22
# File 'lib/chef-attribute-validator/checks/looks_like.rb', line 13

def validate_check_arg
  expected = [
              'ip',
              'url',
             ]
       
  unless expected.include?(check_arg)
    raise "Bad 'looks_like' check argument '#{check_arg}' for rule '#{rule_name}' - expected one of #{expected.join(',')}"
  end
end