Class: WsdlValidator
- Inherits:
-
Object
- Object
- WsdlValidator
- Defined in:
- lib/wsdl_validator/wsdl_validator.rb,
lib/wsdl_validator/error.rb,
lib/wsdl_validator/version.rb
Overview
Helps to validate xml against schemas contained in a WSDL
Defined Under Namespace
Classes: Error
Constant Summary collapse
- VERSION =
'0.1.8'.freeze
Instance Attribute Summary collapse
-
#basic_auth ⇒ Object
Returns the value of attribute basic_auth.
-
#doc ⇒ Object
Returns the value of attribute doc.
-
#schemas ⇒ Object
[Nokogiri::XML::Schema].
-
#show_schemas ⇒ Object
- String
-
Schemas found within WSDL.
Instance Method Summary collapse
-
#errors_for(xml) ⇒ Array
Returns a list of syntax errors.
-
#extract_root_from_soap(envelope) ⇒ Nokogiri::XML::Document
Gets the namespaces from the SOAP Envelope & Body element, adds them to the root element underneath the body and returns that element.
-
#initialize(wsdl_url) ⇒ WsdlValidator
constructor
Parse WSDL storing authentication and all schemas from it.
-
#valid?(xml) ⇒ Boolean, Exception
(also: #validate)
Whether xml is valid according to WSDL of class.
Constructor Details
#initialize(wsdl_url) ⇒ WsdlValidator
Parse WSDL storing authentication and all schemas from it
15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/wsdl_validator/wsdl_validator.rb', line 15 def initialize(wsdl_url) self.basic_auth = extract_auth(wsdl_url) self.doc = Wasabi.document wsdl_url self.show_schemas = parse_wsdl_schemas self.schemas = Nokogiri::XML::Schema(show_schemas) rescue Wasabi::Resolver::HTTPError => resolver raise WsdlValidator::Error, "Unauthorized for basic auth #{basic_auth}" if resolver.response.code == 401 raise resolver rescue Nokogiri::XML::SyntaxError => syntax_error puts "Error for " + show_schemas raise syntax_error end |
Instance Attribute Details
#basic_auth ⇒ Object
Returns the value of attribute basic_auth.
10 11 12 |
# File 'lib/wsdl_validator/wsdl_validator.rb', line 10 def basic_auth @basic_auth end |
#doc ⇒ Object
Returns the value of attribute doc.
11 12 13 |
# File 'lib/wsdl_validator/wsdl_validator.rb', line 11 def doc @doc end |
#schemas ⇒ Object
- Nokogiri::XML::Schema
9 10 11 |
# File 'lib/wsdl_validator/wsdl_validator.rb', line 9 def schemas @schemas end |
#show_schemas ⇒ Object
- String
-
Schemas found within WSDL
7 8 9 |
# File 'lib/wsdl_validator/wsdl_validator.rb', line 7 def show_schemas @show_schemas end |
Instance Method Details
#errors_for(xml) ⇒ Array
Returns a list of syntax errors. Empty list indicates valid xml
43 44 45 46 47 48 49 |
# File 'lib/wsdl_validator/wsdl_validator.rb', line 43 def errors_for(xml) raise "Incorrect type #{xml.class}" unless [String, Nokogiri::XML::Document, Nokogiri::XML::NodeSet].include? xml.class xml_under_test = Nokogiri::XML(xml.to_s) soap_envelope = xml_under_test.children.find { |e| e.name == 'Envelope' } xml_under_test = extract_root_from_soap(soap_envelope) if soap_envelope schemas.validate(xml_under_test) end |
#extract_root_from_soap(envelope) ⇒ Nokogiri::XML::Document
This is not the ideal approach. Ideally Nokogiri parser would be able to understand SOAP xsd as well
Gets the namespaces from the SOAP Envelope & Body element, adds them to the root element underneath the body and returns that element.
32 33 34 35 36 37 38 |
# File 'lib/wsdl_validator/wsdl_validator.rb', line 32 def extract_root_from_soap(envelope) body = envelope.children.find { |child| child.name == 'Body' } root_element = body.children.reject { |child| child.is_a?(Nokogiri::XML::Text) }.first envelope.namespaces.each { |namespace, value| root_element[namespace] = value } body.namespaces.each { |namespace, value| root_element[namespace] = value } Nokogiri::XML root_element.to_s # Convert to Xml Document end |
#valid?(xml) ⇒ Boolean, Exception Also known as: validate
Returns Whether xml is valid according to WSDL of class.
53 54 55 56 57 |
# File 'lib/wsdl_validator/wsdl_validator.rb', line 53 def valid?(xml) validator = errors_for xml raise WsdlValidator::Error, validator.join unless validator.empty? true end |