Sha256: 87692f805fd594652848440423e8e487236e8a77fe507a5430acc2049789fc35

Contents?: true

Size: 1.1 KB

Versions: 8

Compression:

Stored size: 1.1 KB

Contents

module Hydra::ContentNegotiation
  # Replaces Fedora URIs in a graph with a Hydra-configured alternative.
  class FedoraUriReplacer
    def initialize(fedora_base_uri, graph)
      @fedora_base_uri = fedora_base_uri
      @graph = graph
    end

    def run
      RDF::Graph.new.insert(*replaced_objects)
    end

    private

    attr_reader :fedora_base_uri, :graph

    def replace_uri(uri)
      id = ActiveFedora::Base.uri_to_id(uri)
      RDF::URI(Hydra.config.id_to_resource_uri.call(id, graph))
    end

    def replaced_objects
      replaced_subjects.map do |statement|
        if fedora_uri?(statement.object)
          RDF::Statement.from([statement.subject, statement.predicate, replace_uri(statement.object)])
        else
          statement
        end
      end
    end

    def fedora_uri?(subject)
      subject.to_s.start_with?(fedora_base_uri.to_s)
    end

    def replaced_subjects
      graph.each_statement.to_a.map do |s|
        if fedora_uri?(s.subject)
          RDF::Statement.from([replace_uri(s.subject), s.predicate, s.object])
        else
          s
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
hydra-core-10.0.0 app/models/hydra/content_negotiation/fedora_uri_replacer.rb
hydra-core-10.0.0.beta4 app/models/hydra/content_negotiation/fedora_uri_replacer.rb
hydra-core-10.0.0.beta3 app/models/hydra/content_negotiation/fedora_uri_replacer.rb
hydra-core-10.0.0.beta2 app/models/hydra/content_negotiation/fedora_uri_replacer.rb
hydra-core-10.0.0.beta1 app/models/hydra/content_negotiation/fedora_uri_replacer.rb
hydra-core-9.10.0 app/models/hydra/content_negotiation/fedora_uri_replacer.rb
hydra-core-9.9.0 app/models/hydra/content_negotiation/fedora_uri_replacer.rb
hydra-core-9.8.1 app/models/hydra/content_negotiation/fedora_uri_replacer.rb