Class: XMP

Inherits:
Object
  • Object
show all
Defined in:
lib/xmp.rb,
lib/xmp/version.rb,
lib/xmp/silencer.rb,
lib/xmp/namespace.rb

Overview

XMP XML parser

Example

xmp = XMP.new(File.read('xmp.xml'))
xmp.dc.title           # => "Amazing Photo"
xmp.photoshop.Category # => "summer"
xmp.photoshop.SupplementalCategories # => ["morning", "sea"]

Defined Under Namespace

Modules: Silencer Classes: Namespace

Constant Summary collapse

VERSION =
"0.2.2"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xml) ⇒ XMP

accepts valid XMP XML



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/xmp.rb', line 19

def initialize(xml)
  doc = Nokogiri::XML(xml)
  @xml = doc.root

  available_namespaces = doc.collect_namespaces
  # let nokogiri know about all namespaces
  available_namespaces.each do |ns, url|
    @xml.add_namespace_definition ns, url
  end

  # collect namespace names
  @namespaces = available_namespaces.collect do |ns, _|
    ns =~ /^xmlns:(.+)/
    $1
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(namespace, *args) ⇒ Object

returns Namespace object if namespace exists, otherwise tries to call a method



41
42
43
44
45
46
47
# File 'lib/xmp.rb', line 41

def method_missing(namespace, *args)
  if has_namespace?(namespace)
    Namespace.new(self, namespace)
  else
    super
  end
end

Instance Attribute Details

#namespacesObject (readonly)

available namespace names



16
17
18
# File 'lib/xmp.rb', line 16

def namespaces
  @namespaces
end

#xmlObject (readonly)

underlying XML content



14
15
16
# File 'lib/xmp.rb', line 14

def xml
  @xml
end

Class Method Details

.parse(doc) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/xmp.rb', line 53

def self.parse(doc)
  case doc.class.to_s
  when 'EXIFR::JPEG'
    if xmp_chunk = doc.app1s.find { |a| a =~ %r|\Ahttp://ns.adobe.com/xap/1.0/| }
      xmp_data = xmp_chunk.split("\000")[1]
      XMP.new(xmp_data)
    end
  else
    raise "Document not supported:\n#{doc.inspect}"
  end
end

Instance Method Details

#inspectObject



36
37
38
# File 'lib/xmp.rb', line 36

def inspect
  "#<XMP:@namespaces=#{@namespaces.inspect}>"
end

#respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/xmp.rb', line 49

def respond_to?(method)
  has_namespace?(method) or super
end