class AIXM::PayloadHash::Mid

Insert OFMX-compliant payload hashes as mid attributes into an XML document.

Keep in mind: If you pass a Nokogiri::XML::Document, the mid attributes are added into this document. In order to leave the original document untouched, you have to `dup` it.

@example with XML string

string = '<OFMX-Snapshot><Ahp><AhpUid></AhpUid></Ahp></OFMX-Snapshot>'
converter = AIXM::PayloadHash::Mid.new(string)
converter.insert_mid.to_xml   # returns XML as String

@example with Nokogiri document

document = File.open("file.ofmx") { Nokogiri::XML(_1) }
converter = AIXM::PayloadHash::Mid.new(document)
converter.insert_mid.to_xml   # returns XML as String
document.to_xml               # returns XML as String as well

@see gitlab.com/openflightmaps/ofmx/wikis/Features#mid

Public Class Methods

new(document) click to toggle source

@param document [Nokogiri::XML::Document, String] XML document

   # File lib/aixm/payload_hash.rb
72 def initialize(document)
73   @document = case document
74     when Nokogiri::XML::Document then document
75     when String then Nokogiri::XML(document)
76     else fail ArgumentError
77   end
78 end

Public Instance Methods

check_mid() click to toggle source

Check mid attributes on all *Uid elements

@return [Array<String>] array of errors found

   # File lib/aixm/payload_hash.rb
93 def check_mid
94   uid_elements.each_with_object([]) do |element, errors|
95     unless element['mid'] == (uuid = AIXM::PayloadHash.new(element).to_uuid)
96       errors << "#{element.line}: ERROR: Element '#{element.name}': mid should be #{uuid}"
97     end
98   end
99 end
insert_mid() click to toggle source

Insert or update mid attributes on all *Uid elements

@return [self]

   # File lib/aixm/payload_hash.rb
83 def insert_mid
84   uid_elements.each do |element|
85     element['mid'] = AIXM::PayloadHash.new(element).to_uuid
86   end
87   self
88 end
to_xml() click to toggle source

@return [String] XML document as XML string

    # File lib/aixm/payload_hash.rb
102 def to_xml
103   @document.to_xml
104 end

Private Instance Methods

uid_elements() click to toggle source
    # File lib/aixm/payload_hash.rb
108 def uid_elements
109   @document.xpath('//*[contains(local-name(), "Uid")]')
110 end