Module: XmlSpec::Helpers

Extended by:
Helpers
Included in:
Helpers, Matchers::BeXmlEql
Defined in:
lib/xml_spec/helpers.rb

Instance Method Summary collapse

Instance Method Details

#generate_normalized_xml(xml_or_hash) ⇒ Object

TODO consider removing ths method as we do not need 2 levels with XML



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/xml_spec/helpers.rb', line 24

def generate_normalized_xml( xml_or_hash )
  xml = nil
  if xml_or_hash.is_a?( Hash )
     hash = xml_or_hash
     raise 'error' if hash.keys.size != 1
     root = hash.keys.first
     xml = hash[root].to_xml( root: root )
  end

  normalize_xml( xml )
end

#load_xml(relative_path) ⇒ Object



36
37
38
39
40
41
# File 'lib/xml_spec/helpers.rb', line 36

def load_xml(relative_path)
  missing_json_directory! if XmlSpec.directory.nil?
  path = File.join(XmlSpec.directory, relative_path)
  missing_xml_file!(path) unless File.exist?(path)
  File.read(path)
end

#normalize_xml(xml, path = nil) ⇒ Object



17
18
19
20
21
# File 'lib/xml_spec/helpers.rb', line 17

def normalize_xml( xml, path=nil )
  Nokogiri.XML( xml ) do |config|
    config.default_xml.noblanks
  end.to_xml( indent: 2 ).chomp
end

#parse_xml(xml, path = nil) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/xml_spec/helpers.rb', line 8

def parse_xml( xml, path=nil )
  Nokogiri::XML( xml ) { |config| config.options = Nokogiri::XML::ParseOptions::STRICT }
  parser = Nori.new
  ruby = parser.parse( xml )
  value_at_json_path( ruby, path )
rescue Nokogiri::XML::SyntaxError=> e
  raise XmlSpec::XmlSyntaxError.new( e )
end