class Qa::Services::RDFAuthorityParser

Class to parse downloaded content from rdf sources such as Library of Congress and Lexvo to create LocalAuthorityEntries for a named LocalAuthority.

Example calls: RDFAuthorityParser.import_rdf('subjects',['/tmp/authoritiessubjects.nt']) RDFAuthorityParser.import_rdf('languages',['/tmp/lexvo_2013-02-09.rdf'],

format: 'rdfxml',
predicate: RDF::URI("http://www.w3.org/2008/05/skos#prefLabel"))

Public Class Methods

import_rdf(name, sources, opts = {}) click to toggle source

import the rdf from the sources and store them as entries for the named LocalAuthority

@param [String] name The name of the authority to update @param [Array] sources An array of file names to process @param [Hash] opts options for processing @option [opts] :format format of the rdf to parse

 see RDF::Reader for valid options
defaults to :ntripples

@option [opts] :predicate label predicate to use for

 LocalAuthorityEntry.label, which
 must match a field in the source file(s)
defaults to http://www.w3.org/2004/02/skos/core#prefLabel
# File lib/qa/services/rdf_authority_parser.rb, line 29
def import_rdf(name, sources, opts = {})
  authority = Qa::LocalAuthority.find_or_create_by(name: name)
  format = opts.fetch(:format, :ntriples)
  predicate = opts.fetch(:predicate, ::RDF::Vocab::SKOS.prefLabel)
  sources.each do |uri|
    import_source(authority, format, predicate, uri)
  end
end

Private Class Methods

import_source(authority, format, predicate, uri) click to toggle source
# File lib/qa/services/rdf_authority_parser.rb, line 40
def import_source(authority, format, predicate, uri)
  ::RDF::Reader.open(uri, format: format) do |reader|
    reader.each_statement do |statement|
      parse_statement(statement, predicate, authority)
    end
  end
end
parse_statement(statement, predicate, authority) click to toggle source
# File lib/qa/services/rdf_authority_parser.rb, line 48
def parse_statement(statement, predicate, authority)
  return unless statement.predicate == predicate
  Qa::LocalAuthorityEntry.create(local_authority: authority,
                                 label: statement.object.to_s,
                                 uri: statement.subject.to_s)
rescue ActiveRecord::RecordNotUnique
  Rails.logger.warn("Duplicate record: #{statement.subject}")
end