class AIXM::PayloadHash
Calculate OFMX-compliant payload hashes.
@example with XML fragment string
xml = '<xml><a></a></xml>' AIXM::PayloadHash.new(xml).to_uuid
@example with Nokogiri fragment
document = File.open("file.xml") { Nokogiri::XML(_1) } AIXM::PayloadHash.new(document).to_uuid
Constants
- IGNORED_ATTRIBUTES
Public Class Methods
new(fragment)
click to toggle source
@param fragment [Nokogiri::XML::DocumentFragment, Nokogiri::XML::Element, String] XML fragment
# File lib/aixm/payload_hash.rb 20 def initialize(fragment) 21 @fragment = case fragment 22 when Nokogiri::XML::DocumentFragment then fragment 23 when Nokogiri::XML::Element, String then Nokogiri::XML.fragment(fragment) 24 else fail ArgumentError 25 end 26 end
Public Instance Methods
to_uuid()
click to toggle source
@return [String] UUIDv3
# File lib/aixm/payload_hash.rb 29 def to_uuid 30 uuid_for payload_array 31 end
Private Instance Methods
payload_array()
click to toggle source
# File lib/aixm/payload_hash.rb 35 def payload_array 36 @fragment.css('*').each_with_object([]) do |element, array| 37 array << element.name.sub(/\A(\w+Uid)\w+/, '\1') # remove name extension 38 element.attributes.sort.each do |name, attribute| 39 array.push(name, attribute.value) unless IGNORED_ATTRIBUTES.include? name 40 end 41 array << element.child.text if element.children.one? && element.child.text? 42 array << '' if element.children.none? 43 end 44 end
uuid_for(array)
click to toggle source
# File lib/aixm/payload_hash.rb 46 def uuid_for(array) 47 ::Digest::MD5.hexdigest(array.flatten.map(&:to_s).join('|')).unpack("a8a4a4a4a12").join("-") 48 end