Module: Serializers::RDF

Included in:
Document
Defined in:
lib/serializers/rdf.rb

Overview

Convert a document to an RDF record

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Object) included(base)

Register this serializer in the Document list



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/serializers/rdf.rb', line 11

def self.included(base)
  base.register_serializer(
    :rdf, 'RDF/XML',
    ->(doc) { doc.to_rdf_xml.to_xml(indent: 2) },
    'http://www.w3.org/TR/rdf-syntax-grammar/'
  )
  base.register_serializer(
    :n3, 'RDF/N3',
    ->(doc) { doc.to_rdf_n3 },
    'http://www.w3.org/DesignIssues/Notation3.html'
  )
end

Instance Method Details

- (RDF::Graph) to_rdf

Returns this document as a RDF::Graph object

For the moment, we provide only metadata items for the basic Dublin Core elements, and for the Dublin Core “bibliographicCitation” element. We also encode an OpenURL reference (using the standard OpenURL namespace), in a second bibliographicCitation element. The precise way to encode journal articles in DC is in serious flux, but this should provide a reasonable solution.

Examples:

Convert this document to RDF-Turtle

RDF::Writer.for(:turtle).buffer do |writer|
  writer << doc.to_rdf
end


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/serializers/rdf.rb', line 41

def to_rdf
  graph = ::RDF::Graph.new
  doc = ::RDF::Node.new

  unless formatted_author_list.nil?
    formatted_author_list.each do |a|
      name = ''
      name << "#{a.von} " unless a.von.blank?
      name << "#{a.last}"
      name << " #{a.suffix}" unless a.suffix.blank?
      name << ", #{a.first}"
      graph << [doc, ::RDF::DC.creator, name]
    end
  end
  graph << [doc, ::RDF::DC.issued, year] unless year.blank?

  citation = "#{journal}" unless journal.blank?
  citation << " #{volume}" unless volume.blank?
  citation << ' ' if volume.blank?
  citation << "(#{number})" unless number.blank?
  citation << ", #{pages}" unless pages.blank?
  citation << ". (#{year})" unless year.blank?
  graph << [doc, ::RDF::DC.bibliographicCitation, citation]

  ourl = ::RDF::Literal.new(
    '&' + to_openurl_params,
    datatype: ::RDF::URI.new('info:ofi/fmt:kev:mtx:ctx')
  )
  graph << [doc, ::RDF::DC.bibliographicCitation, ourl]

  graph << [doc, ::RDF::DC.relation, journal] unless journal.blank?
  graph << [doc, ::RDF::DC.title, title] unless title.blank?
  graph << [doc, ::RDF::DC.type, 'Journal Article']
  graph << [doc, ::RDF::DC.identifier, "info:doi/#{doi}"] unless doi.blank?

  graph
end

- (String) to_rdf_n3

Note:

No tests for this method, as it is implemented by the RDF gem.

Returns this document as RDF+N3

:nocov:

Examples:

Download this document as a n3 file

controller.send_data doc.to_rdf_turtle, filename: 'export.n3',
                     disposition: 'attachment'


88
89
90
91
92
# File 'lib/serializers/rdf.rb', line 88

def to_rdf_n3
  ::RDF::Writer.for(:n3).buffer do |writer|
    writer << to_rdf
  end
end

- (Nokogiri::XML::Document) to_rdf_xml

Returns this document as RDF+XML

Examples:

Download this document as an XML file

controller.send_data doc.to_rdf_xml.to_xml, filename: 'export.xml',
                     disposition: 'attachment'


137
138
139
140
141
142
143
144
145
146
147
# File 'lib/serializers/rdf.rb', line 137

def to_rdf_xml
  doc = Nokogiri::XML::Document.new
  rdf = Nokogiri::XML::Node.new('rdf', doc)

  doc.add_child(rdf)
  rdf.default_namespace = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'
  rdf.add_namespace_definition('dc', 'http://purl.org/dc/terms/')
  rdf.add_child(to_rdf_xml_node(doc))

  doc
end

- (Nokogiri::XML::Node) to_rdf_xml_node(doc)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns this document as an rdf:Description element



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/serializers/rdf.rb', line 100

def to_rdf_xml_node(doc)
  graph = to_rdf

  desc = Nokogiri::XML::Node.new('Description', doc)

  to_rdf.each_statement do |statement|
    qname = statement.predicate.qname
    unless qname
      Rails.logger.warn "Cannot get qualified name for #{statement.predicate.to_s}, skipping predicate"
      next
    end

    unless statement.object.literal?
      Rails.logger.warn "Object #{statement.object.inspect} is not a literal, cannot parse"
      next
    end

    node = Nokogiri::XML::Node.new("#{qname[0]}:#{qname[1]}", doc)
    node.content = statement.object.value

    if statement.object.has_datatype?
      node['datatype'] = statement.object.datatype.to_s
    end

    desc.add_child(node)
  end

  desc
end