Class: AIXM::Executables::Ckmid

Inherits:
Object
  • Object
show all
Defined in:
lib/aixm/executables.rb

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ Ckmid

Returns a new instance of Ckmid.



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/aixm/executables.rb', line 46

def initialize(**options)
  @options = options
  OptionParser.new do |o|
    o.banner = <<~END
      Check mid attributes of OFMX files.
      Usage: #{File.basename($0)} files
    END
    o.on('-s', '--[no-]skip-validation', 'skip XML schema validation (default: false)') { @options[:skip_validation] = _1 }
    o.on('-A', '--about', 'show author/license information and exit') { AIXM::Executables.about }
    o.on('-V', '--version', 'show version and exit') { AIXM::Executables.version }
  end.parse!
  @files = ARGV
end

Instance Method Details

#runObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/aixm/executables.rb', line 60

def run
  exit(
    @files.reduce(true) do |success, file|
      fail "cannot read #{file}" unless file && File.readable?(file)
      fail "#{file} is not OFMX" unless file.match?(/\.ofmx$/)
      AIXM.ofmx!
      document = File.open(file) { Nokogiri::XML(_1) }
      errors = []
      unless @options[:skip_validation]
        errors += Nokogiri::XML::Schema(File.open(AIXM.schema(:xsd))).validate(document)
      end
      errors += AIXM::PayloadHash::Mid.new(document).check_mid
      fail (["#{file} is not valid..."] + errors).join("\n") if errors.any?
      success && true
    rescue RuntimeError => error
      puts "ERROR: #{error.message}"
      false
    end
  )
end