Commit ca63fd93380ae6afc7ef40705bf32a799d033bb2
1 parent
14b5363d
Exists in
theme-brasil-digital-from-staging
and in
9 other branches
virtuoso: refactoring dublin core metadata
Showing
2 changed files
with
70 additions
and
11 deletions
Show diff stats
plugins/virtuoso/lib/virtuoso_plugin/dublin_core_metadata.rb
| @@ -2,19 +2,27 @@ class VirtuosoPlugin::DublinCoreMetadata | @@ -2,19 +2,27 @@ class VirtuosoPlugin::DublinCoreMetadata | ||
| 2 | 2 | ||
| 3 | include OAI::XPath | 3 | include OAI::XPath |
| 4 | 4 | ||
| 5 | - attr_accessor :date, :title, :creator, :subject, :description, :date, :type, :identifier, :language, :rights, :format | 5 | + FIELDS = ['title', 'creator', 'subject', 'description', 'date', |
| 6 | + 'type', 'identifier', 'language', 'rights', 'format'] | ||
| 6 | 7 | ||
| 7 | def initialize(element) | 8 | def initialize(element) |
| 8 | - @title = xpath(element, './/dc:title') | ||
| 9 | - @creator = xpath(element, './/dc:creator') | ||
| 10 | - @subject = xpath_all(element, './/dc:subject').map(&:text) | ||
| 11 | - @description = xpath(element, './/dc:description') | ||
| 12 | - @date = xpath(element, './/dc:date') | ||
| 13 | - @type = xpath(element, './/dc:type') | ||
| 14 | - @identifier = xpath(element, './/dc:identifier') | ||
| 15 | - @language = xpath(element, './/dc:language') | ||
| 16 | - @rights = xpath_all(element, './/dc:rights').map(&:text) | ||
| 17 | - @format = xpath(element, './/dc:format') | 9 | + @element = element |
| 10 | + FIELDS.each do |field| | ||
| 11 | + self.class.send(:attr_accessor, field) | ||
| 12 | + self.send("#{field}=", extract_field("dc:#{field}")) | ||
| 13 | + end | ||
| 14 | + end | ||
| 15 | + | ||
| 16 | + def extract_field(name) | ||
| 17 | + value = xpath_all(@element, ".//#{name}") | ||
| 18 | + case value.size | ||
| 19 | + when 0 | ||
| 20 | + nil | ||
| 21 | + when 1 | ||
| 22 | + value.first.text | ||
| 23 | + else | ||
| 24 | + value.map(&:text) | ||
| 25 | + end | ||
| 18 | end | 26 | end |
| 19 | 27 | ||
| 20 | end | 28 | end |
| @@ -0,0 +1,51 @@ | @@ -0,0 +1,51 @@ | ||
| 1 | +require File.dirname(__FILE__) + '/../test_helper' | ||
| 2 | + | ||
| 3 | +class DublinCoreMetadataTest < ActiveSupport::TestCase | ||
| 4 | + | ||
| 5 | + should 'parse default dublin core fields' do | ||
| 6 | + dc = VirtuosoPlugin::DublinCoreMetadata.new(metadata) | ||
| 7 | + assert_equal "Title", dc.title | ||
| 8 | + assert_equal "Creator", dc.creator | ||
| 9 | + assert_equal ["Subject", "Other Subject"], dc.subject | ||
| 10 | + assert_equal "Description", dc.description | ||
| 11 | + assert_equal "2014", dc.date | ||
| 12 | + assert_equal "Type", dc.type | ||
| 13 | + assert_equal "Identifier", dc.identifier | ||
| 14 | + assert_equal "Language", dc.language | ||
| 15 | + assert_equal "Rights", dc.rights | ||
| 16 | + assert_equal "Format", dc.format | ||
| 17 | + end | ||
| 18 | + | ||
| 19 | + should 'extract fields' do | ||
| 20 | + dc = VirtuosoPlugin::DublinCoreMetadata.new(metadata) | ||
| 21 | + assert_equal "Title", dc.extract_field('dc:title') | ||
| 22 | + assert_equal "Creator", dc.extract_field('dc:creator') | ||
| 23 | + assert_equal ["Subject", "Other Subject"], dc.extract_field('dc:subject') | ||
| 24 | + end | ||
| 25 | + | ||
| 26 | + should 'return nil when field do not exists' do | ||
| 27 | + dc = VirtuosoPlugin::DublinCoreMetadata.new(metadata) | ||
| 28 | + assert_equal nil, dc.extract_field('dc:undefinedField') | ||
| 29 | + end | ||
| 30 | + | ||
| 31 | + def metadata | ||
| 32 | + REXML::Document.new(' | ||
| 33 | + <metadata> | ||
| 34 | + <oai_dc:dc xmlns:oai_dc="http://www.openarchives.org/OAI/2.0/oai_dc/" xmlns:dc="http://purl.org/dc/elements/1.1/"> | ||
| 35 | + <dc:title>Title</dc:title> | ||
| 36 | + <dc:creator>Creator</dc:creator> | ||
| 37 | + <dc:subject>Subject</dc:subject> | ||
| 38 | + <dc:subject>Other Subject</dc:subject> | ||
| 39 | + <dc:description>Description</dc:description> | ||
| 40 | + <dc:date>2014</dc:date> | ||
| 41 | + <dc:type>Type</dc:type> | ||
| 42 | + <dc:identifier>Identifier</dc:identifier> | ||
| 43 | + <dc:language>Language</dc:language> | ||
| 44 | + <dc:rights>Rights</dc:rights> | ||
| 45 | + <dc:format>Format</dc:format> | ||
| 46 | + </oai_dc:dc> | ||
| 47 | + </metadata> | ||
| 48 | + ') | ||
| 49 | + end | ||
| 50 | + | ||
| 51 | +end |