Class: SIRI::XSD::Validator
- Inherits:
-
Object
- Object
- SIRI::XSD::Validator
- Defined in:
- lib/siri/xsd/validator.rb
Class Method Summary collapse
Instance Method Summary collapse
- #errors ⇒ Object
- #open_input(input) ⇒ Object
- #valid? ⇒ Boolean
- #validate(input, filename: nil) ⇒ Object
- #validate_file(file) ⇒ Object
Class Method Details
.load_schema ⇒ Object
15 16 17 18 19 20 21 |
# File 'lib/siri/xsd/validator.rb', line 15 def self.load_schema Dir.chdir schema_dir do File.open schema_file, "r" do |file| Nokogiri::XML::Schema file end end end |
.schema ⇒ Object
23 24 25 |
# File 'lib/siri/xsd/validator.rb', line 23 def self.schema @schema ||= load_schema end |
.schema_dir ⇒ Object
7 8 9 |
# File 'lib/siri/xsd/validator.rb', line 7 def self.schema_dir File.("../../../../vendor/xsd",__FILE__) end |
.schema_file ⇒ Object
11 12 13 |
# File 'lib/siri/xsd/validator.rb', line 11 def self.schema_file File.join(schema_dir, 'soap.xsd') end |
Instance Method Details
#errors ⇒ Object
56 57 58 |
# File 'lib/siri/xsd/validator.rb', line 56 def errors @errrors ||= [] end |
#open_input(input) ⇒ Object
42 43 44 45 46 47 48 49 50 |
# File 'lib/siri/xsd/validator.rb', line 42 def open_input(input) if input.respond_to?(:read) yield input else File.open(input,'r') do |file| yield file end end end |
#valid? ⇒ Boolean
52 53 54 |
# File 'lib/siri/xsd/validator.rb', line 52 def valid? errors.empty? end |
#validate(input, filename: nil) ⇒ Object
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/siri/xsd/validator.rb', line 31 def validate(input, filename: nil) print "Inspect #{filename}" if filename open_input(input) do |io| document = Nokogiri::XML::Document.parse io, filename document_errors = self.class.schema.validate(document) puts ": #{document_errors.count} error(s)" if filename errors.concat document_errors end end |
#validate_file(file) ⇒ Object
27 28 29 |
# File 'lib/siri/xsd/validator.rb', line 27 def validate_file(file) validate file, filename: file end |