Class: XMPR::XMP

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ XMP

Returns a new instance of XMP.



52
53
54
55
# File 'lib/xmpr.rb', line 52

def initialize(source)
  require "nokogiri"
  @xml = Nokogiri::XML(source)
end

Instance Attribute Details

#xmlObject (readonly)

Nokogiri document of parsed xml



50
51
52
# File 'lib/xmpr.rb', line 50

def xml
  @xml
end

Instance Method Details

#[](namespace_or_name, name = nil, **options) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/xmpr.rb', line 57

def [](namespace_or_name, name=nil, **options)
  if name
    namespace = namespace_or_name
  else
    namespace, name = DEFAULT_NAMESPACE, namespace_or_name
  end

  if NAMESPACES.has_key? namespace
    namespace = NAMESPACES[namespace]
  end

  embedded_attribute(namespace, name) ||
    standalone_attribute(namespace, name, **options)
end

#to_hashObject



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/xmpr.rb', line 72

def to_hash
  {}.tap do |hash|
    xml.at("//rdf:Description", NAMESPACES).attributes.each do |(_, attribute)|
      hash[attribute.namespace.href] ||= {}
      hash[attribute.namespace.href][attribute.name] = attribute.value
    end
    xml.xpath("//rdf:Description/*", NAMESPACES).each do |element|
      hash[element.namespace.href] ||= {}
      hash[element.namespace.href][element.name] = standalone_value(element, lang: nil)
    end
  end
end