Class: AIXM::PayloadHash::Mid

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

Overview

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.

Examples:

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

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 Also:

Instance Method Summary collapse

Constructor Details

#initialize(document) ⇒ Mid

Returns a new instance of Mid.

Parameters:

  • document (Nokogiri::XML::Document, String)

    XML document



75
76
77
78
79
80
81
# File 'lib/aixm/payload_hash.rb', line 75

def initialize(document)
  @document = case document
    when Nokogiri::XML::Document then document
    when String then Nokogiri::XML(document)
    else fail ArgumentError
  end
end

Instance Method Details

#check_midArray<String>

Check mid attributes on all *Uid elements

Returns:

  • (Array<String>)

    array of errors found



96
97
98
99
100
101
102
# File 'lib/aixm/payload_hash.rb', line 96

def check_mid
  uid_elements.each_with_object([]) do |element, errors|
    unless element['mid'] == (uuid = AIXM::PayloadHash.new(element).to_uuid)
      errors << "#{element.line}: ERROR: Element '#{element.name}': mid should be #{uuid}"
    end
  end
end

#insert_midself

Insert or update mid attributes on all *Uid elements

Returns:

  • (self)


86
87
88
89
90
91
# File 'lib/aixm/payload_hash.rb', line 86

def insert_mid
  uid_elements.each do |element|
    element['mid'] = AIXM::PayloadHash.new(element).to_uuid
  end
  self
end

#to_xmlString

Returns XML document as XML string.

Returns:

  • (String)

    XML document as XML string



105
106
107
# File 'lib/aixm/payload_hash.rb', line 105

def to_xml
  @document.to_xml
end