|
1 | 1 | require 'rails/html/sanitizer' |
| 2 | +require 'json/ld' |
| 3 | +require 'rdf' |
| 4 | +require 'rdf/rdfxml' |
| 5 | +require 'builder' |
2 | 6 |
|
3 | 7 | class Material < ApplicationRecord |
4 | 8 | include PublicActivity::Common |
@@ -186,4 +190,58 @@ def duplicate |
186 | 190 | def archived? |
187 | 191 | status == 'archived' |
188 | 192 | end |
| 193 | + |
| 194 | + def to_rdf |
| 195 | + jsonld_str = to_bioschemas[0].to_json |
| 196 | + |
| 197 | + graph = RDF::Graph.new |
| 198 | + JSON::LD::Reader.new(jsonld_str) do |reader| |
| 199 | + reader.each_statement { |stmt| graph << stmt } |
| 200 | + end |
| 201 | + |
| 202 | + rdfxml_str = graph.dump(:rdfxml, prefixes: { sdo: 'http://schema.org/', dc: 'http://purl.org/dc/terms/' }) |
| 203 | + rdfxml_str.sub(/\A<\?xml.*?\?>\s*/, '') # remove XML declaration because this is used inside OAI-PMH response |
| 204 | + end |
| 205 | + |
| 206 | + def to_oai_dc |
| 207 | + xml = ::Builder::XmlMarkup.new |
| 208 | + xml.tag!('oai_dc:dc', |
| 209 | + 'xmlns:oai_dc' => 'http://www.openarchives.org/OAI/2.0/oai_dc/', |
| 210 | + 'xmlns:dc' => 'http://purl.org/dc/elements/1.1/', |
| 211 | + 'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance', |
| 212 | + 'xsi:schemaLocation' => 'http://www.openarchives.org/OAI/2.0/oai_dc/ http://www.openarchives.org/OAI/2.0/oai_dc.xsd') do |
| 213 | + xml.tag!('dc:title', title) |
| 214 | + xml.tag!('dc:description', description) |
| 215 | + authors.each { |a| xml.tag!('dc:creator', a) } |
| 216 | + contributors.each { |a| xml.tag!('dc:contributor', a) } |
| 217 | + xml.tag!('dc:publisher', content_provider.title) if content_provider |
| 218 | + |
| 219 | + xml.tag!('dc:format', 'text/html') |
| 220 | + xml.tag!('dc:language', 'en') |
| 221 | + xml.tag!('dc:rights', licence) if licence.present? |
| 222 | + |
| 223 | + [date_published, date_created, date_modified].compact.each do |d| |
| 224 | + xml.tag!('dc:date', d.iso8601) |
| 225 | + end |
| 226 | + |
| 227 | + if doi.present? |
| 228 | + doi_iri = doi.start_with?('http://', 'https://') ? doi : "https://doi.org/#{doi}" |
| 229 | + xml.tag!('dc:identifier', doi_iri) |
| 230 | + else |
| 231 | + xml.tag!('dc:identifier', url) |
| 232 | + end |
| 233 | + |
| 234 | + (keywords + scientific_topics.map(&:uri) + operations.map(&:uri)).each do |s| |
| 235 | + xml.tag!('dc:subject', s) |
| 236 | + end |
| 237 | + |
| 238 | + xml.tag!('dc:type', 'http://purl.org/dc/dcmitype/Text') |
| 239 | + xml.tag!('dc:type', 'https://schema.org/LearningResource') |
| 240 | + resource_type.each { |t| xml.tag!('dc:type', t) } |
| 241 | + |
| 242 | + xml.tag!('dc:relation', "#{TeSS::Config.base_url}#{Rails.application.routes.url_helpers.material_path(self)}") |
| 243 | + xml.tag!('dc:relation', content_provider.url) if content_provider&.url |
| 244 | + end |
| 245 | + xml.target! |
| 246 | + end |
189 | 247 | end |
0 commit comments