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


72
73
74
75
76
77
78
# File 'lib/aixm/payload_hash.rb', line 72

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


93
94
95
96
97
98
99
# File 'lib/aixm/payload_hash.rb', line 93

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)

83
84
85
86
87
88
# File 'lib/aixm/payload_hash.rb', line 83

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


102
103
104
# File 'lib/aixm/payload_hash.rb', line 102

def to_xml
  @document.to_xml
end