class ROF::CompareRof

Attributes

bendo[R]
fedora[R]

Public Class Methods

fedora_vs_bendo(fedora_rof, bendo_rof, _output = nil, options = {}) click to toggle source

Compare two ROF objects; we'll call one fedora_rof and the other bendo_rof @return 0 if no errors; otherwise there are errors

# File lib/rof/compare_rof.rb, line 13
def self.fedora_vs_bendo(fedora_rof, bendo_rof, _output = nil, options = {})
  new(Array.wrap(fedora_rof)[0], Array.wrap(bendo_rof)[0], options).error_count
end
new(fedora, bendo, options = {}) click to toggle source
# File lib/rof/compare_rof.rb, line 17
def initialize(fedora, bendo, options = {})
  @fedora = Array.wrap(fedora).first
  @bendo = Array.wrap(bendo).first
  @skip_rels_ext_context = options.fetch(:skip_rels_ext_context) { false }
end

Public Instance Methods

compare_everything_else() click to toggle source

compare what remains

# File lib/rof/compare_rof.rb, line 95
def compare_everything_else
  error_count =0
  exclude_keys = ['rights', 'rels-ext', 'metadata', 'thumbnail-file']
  all_keys_to_check = (bendo.keys + fedora.keys - exclude_keys).uniq
  all_keys_to_check.each do |key|
    bendo_value = bendo.fetch(key, nil)
    fedora_value = fedora.fetch(key, nil)
    # Treat an empty hash and an empty array as equal
    next if bendo_value.empty? && fedora_value.empty?
    next if normalize_value(bendo_value) == normalize_value(fedora_value)
    error_count += 1
    break
  end
  error_count
end
compare_metadata() click to toggle source

convert metadata sections to RDF::graph and compater w/ rdf-isomorphic

# File lib/rof/compare_rof.rb, line 86
def compare_metadata
  error_count = 0
  bendo_rdf = jsonld_to_rdf(bendo.fetch('metadata', {}), ROF::RdfContext)
  fedora_rdf = jsonld_to_rdf(fedora.fetch('metadata', {}), ROF::RdfContext)
  error_count +=1 if ! bendo_rdf.isomorphic_with? fedora_rdf
  error_count
end
compare_rels_ext() click to toggle source

convert RELS-EXT sections to RDF::graph and compater w/ rdf-isomorphic

# File lib/rof/compare_rof.rb, line 62
def compare_rels_ext
  error_count = 0
  # Because Sipity's RELS-EXT context was out of whack, I need a switch to skip comparing
  # the @context of the rels-ext document
  bendo_rdf = jsonld_to_rdf(bendo.fetch('rels-ext', {}), ROF::RelsExtRefContext, @skip_rels_ext_context)
  fedora_rdf = jsonld_to_rdf(fedora.fetch('rels-ext', {}), ROF::RelsExtRefContext, @skip_rels_ext_context)
  error_count +=1 if ! bendo_rdf.isomorphic_with? fedora_rdf
  error_count
end
compare_rights() click to toggle source

do rights comparison return 0 if the same, >0 if different

# File lib/rof/compare_rof.rb, line 35
def compare_rights

  error_count =0

  # Use same comparison scheme on all rights
  [ 'read' , 'read-groups', 'edit', 'edit-groups', 'edit-users', 'embargo-date'].each do |attribute|
    error_count += rights_equal(attribute)
    break if error_count != 0
  end

  error_count
end
error_count() click to toggle source
# File lib/rof/compare_rof.rb, line 24
def error_count
  @error_count = 0
  @error_count += compare_rights
  @error_count += compare_rels_ext
  @error_count += compare_metadata
  @error_count += compare_everything_else
  @error_count
end

Private Instance Methods

jsonld_to_rdf(doc, default_context, skip_context = false) click to toggle source
# File lib/rof/compare_rof.rb, line 74
def jsonld_to_rdf(doc, default_context, skip_context = false)
  if skip_context
    doc.delete('@context')
  else
    doc["@context"] ||= default_context
  end
  RDF::Graph.new << JSON::LD::API.toRdf(doc)
end
normalize_value(values) click to toggle source

Because sometimes we have carriage returns and line breaks but we really don't care @todo Do we care about line breaks?

# File lib/rof/compare_rof.rb, line 115
def normalize_value(values)
  Array.wrap(values).map do |value|
    value.is_a?(String) ? value.gsub("\n", "") : value
  end
end
rights_equal(rights_attr) click to toggle source

compare array or element for equivalence

# File lib/rof/compare_rof.rb, line 51
def rights_equal(rights_attr)
  f_rights = Array.wrap(fedora.fetch('rights', {}).fetch(rights_attr, [])).sort
  b_rights = Array.wrap(bendo.fetch('rights', {}).fetch(rights_attr, [])).sort

  return 0 if f_rights == b_rights
  1
end