Class: OpenURL::DublinCore

Inherits:
ContextObjectEntity show all
Defined in:
lib/openurl/metadata_formats/dublin_core.rb

Instance Attribute Summary

Attributes inherited from ContextObjectEntity

#abbr, #format, #identifiers, #label, #metadata, #private_data, #reference

Instance Method Summary collapse

Methods inherited from ContextObjectEntity

#add_identifier, #delete_identifier, #empty?, #get_metadata, #identifier, new_from_format, normalize_id, #set_format, #set_private_data, #set_reference, #to_hash, #xml, #xml_for_ref_entity

Constructor Details

#initializeDublinCore

Returns a new instance of DublinCore.



13
14
15
16
17
18
19
20
21
22
# File 'lib/openurl/metadata_formats/dublin_core.rb', line 13

def initialize      
  super
  @metadata_keys = ['title','creator','subject','description','publisher',
    'contributor','date','type','format','identifier','source', 'language',
    'relation', 'coverage', 'rights'
    ]      
  @xml_ns = "info:ofi/fmt:xml:xsd:oai_dc"
  @kev_ns = "info:ofi/fmt:kev:mtx:dc"
  @oai_ns = "http://www.openarchives.org/OAI/2.0/oai_dc/"
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(metadata, value = nil) ⇒ Object

Raises:

  • (ArgumentError)


24
25
26
27
28
29
30
31
32
# File 'lib/openurl/metadata_formats/dublin_core.rb', line 24

def method_missing(, value=nil)
  meta = .to_s.sub(/=$/,'')
  raise ArgumentError, "#{meta.to_s} is not a valid #{self.class} metadata field." unless @metadata_keys.index(meta)
  if .to_s.match(/=$/)
    self.(meta, value)
  else
    return self.[meta]
  end      
end

Instance Method Details

#import_dc(dc) ⇒ Object

Raises:

  • (ArgumentError)


70
71
72
73
74
75
76
77
78
# File 'lib/openurl/metadata_formats/dublin_core.rb', line 70

def import_dc(dc)
  raise ArgumentError, "Argument must be a REXML::Document or String!" unless dc.is_a?(REXML::Document) or dc.is_a?(String)
  doc = dc
  doc = REXML::Document.new(dc) if doc.is_a?(String)
  doc.root.elements.each do | elem |
    self.(elem.name(), CGI.unescapeHTML(elem.children.to_s)) unless elem.children.empty?                                  
  end
  
end

#import_xml_metadata(node) ⇒ Object



59
60
61
62
63
64
65
66
67
68
# File 'lib/openurl/metadata_formats/dublin_core.rb', line 59

def (node)     
  mbv = REXML::XPath.first(node, "./ctx:metadata-by-val/ctx:metadata/fmt:dc", {"ctx"=>"info:ofi/fmt:xml:xsd:ctx", "fmt"=>@oai_ns})					              
  if mbv
    mbv.to_a.each do |m|          
      m.children.each do | value|            
        self.(m.name(), CGI.unescapeHTML(value.to_s))
      end
    end      
  end					
end

#kev(abbr) ⇒ Object

Outputs the entity as a KEV array



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/openurl/metadata_formats/dublin_core.rb', line 82

def kev(abbr)
  kevs = []
  
  @metadata.each do |key, vals|
    vals.each do | val |
      kevs << "#{abbr}.#{key}="+CGI.escape(val)
    end
  end
  
  kevs << "#{abbr}_val_fmt="+CGI.escape(@kev_ns)      
 
  
  if @reference["format"] 
    kevs << "#{abbr}_ref_fmt="+CGI.escape(@reference["format"])
    kevs << "#{abbr}_ref="+CGI.escape(@reference["location"])      
  end
  
  @identifiers.each do |id| 
      kevs << "#{abbr}_id="+CGI.escape(id)
  end
  
  kevs << "#{abbr}_dat="+CGI.escape(@private_data) if @private_data
                
  return kevs      
end

#serialize_metadata(elem, label) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/openurl/metadata_formats/dublin_core.rb', line 40

def (elem, label)
  meta = []
  fmt = elem.add_element("ctx:format")
  fmt.text = @xml_ns
   = elem.add_element("ctx:metadata")
  dc_container = .add_element("#{label}:dc")
  dc_container.add_namespace(label, @oai_ns)
  dc_container.add_namespace("dc", "http://purl.org/dc/elements/1.1/")
  dc_container.add_namespace("xsi", "http://www.w3.org/2001/XMLSchema-instance")
  dc_container.add_attribute("xsi:schemaLocation", "http://www.openarchives.org/OAI/2.0/oai_dc/ http://www.openarchives.org/OAI/2.0/oai_dc.xsd")
   
  @metadata.each do |key,vals|
    vals.each do | val |
      meta << dc_container.add_element("#{label}:#{key}")
      meta.last.text = val
    end
  end                 
end

#set_metadata(key, val) ⇒ Object



34
35
36
37
# File 'lib/openurl/metadata_formats/dublin_core.rb', line 34

def (key, val)
  @metadata[key] ||=[]    
  @metadata[key] << val unless @metadata[key].index(val)
end