class OJXV::CrossrefMetadataFile

VALIDATING CROSSREF against the XSD Crossref bundle v0.3.1 from gitlab.com/crossref/schema/-/releases

Attributes

errors[RW]
file[RW]

Public Class Methods

new(filepath) click to toggle source
# File lib/ojxv/crossref_metadata_file.rb, line 11
def initialize(filepath)
  @errors = nil
  if File.exist?(filepath)
    @file = File.open(filepath)
  else
    raise ::OJXV::FileNotFound, "Can't find file: #{filepath}"
  end
end
supported_schema_versions() click to toggle source
# File lib/ojxv/crossref_metadata_file.rb, line 33
def self.supported_schema_versions
  ["4.8.0", "4.8.1", "5.1.0", "5.2.0", "5.3.0", "5.3.1"]
end

Public Instance Methods

valid_crossref?(crossref_version="5.3.1") click to toggle source
# File lib/ojxv/crossref_metadata_file.rb, line 20
def valid_crossref?(crossref_version="5.3.1")
  @errors = nil

  xsd = Nokogiri::XML::Schema(schema(crossref_version))
  doc = Nokogiri::XML(file.read)

  @errors = xsd.validate(doc)
  @errors.empty?

rescue Nokogiri::XML::SyntaxError => e
  raise ::OJXV::XMLParsingError, e.message
end

Private Instance Methods

schema(crossref_version) click to toggle source
# File lib/ojxv/crossref_metadata_file.rb, line 39
def schema(crossref_version)
  schema_path = File.join(File.dirname(__FILE__), "schemas", "crossref", "crossref#{crossref_version}.xsd")

  unless File.exist?(schema_path) && CrossrefMetadataFile.supported_schema_versions.include?(crossref_version)
    raise ::OJXV::UnsupportedCrossrefSchemaVersion, "Unsupported Crossref schema version: #{crossref_version}"
  end

  File.open(schema_path)
end