Sha256: 290149428fd3a5e53753cac28191ac1ec18fcc2335fbc31c17bcb3794acc51f8

Contents?: true

Size: 1.39 KB

Versions: 30

Compression:

Stored size: 1.39 KB

Contents

module Hydra::ContentNegotiation
  # Replaces Fedora URIs in a graph with a Hydra-configured alternative.
  class FedoraUriReplacer
    # @param [String] fedora_base_uri the internal Fedora base uri
    # @param [RDF::Graph] graph the original graph that needs URIs replaced
    # @param [#call] replacer a function that is called with id and graph and returns a string representation of the new uri.
    def initialize(fedora_base_uri, graph, replacer)
      @fedora_base_uri = fedora_base_uri
      @graph = graph
      @replacer = replacer
    end

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

    private

    attr_reader :fedora_base_uri, :graph, :replacer

    def replace_uri(uri)
      id = ActiveFedora::Base.uri_to_id(uri)
      RDF::URI(replacer.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

30 entries across 30 versions & 1 rubygems

Version Path
hydra-core-13.0.0 app/models/hydra/content_negotiation/fedora_uri_replacer.rb
hydra-core-12.1.0 app/models/hydra/content_negotiation/fedora_uri_replacer.rb
hydra-core-12.0.2 app/models/hydra/content_negotiation/fedora_uri_replacer.rb
hydra-core-11.0.7 app/models/hydra/content_negotiation/fedora_uri_replacer.rb
hydra-core-12.0.1 app/models/hydra/content_negotiation/fedora_uri_replacer.rb
hydra-core-11.0.6 app/models/hydra/content_negotiation/fedora_uri_replacer.rb
hydra-core-11.0.5 app/models/hydra/content_negotiation/fedora_uri_replacer.rb
hydra-core-11.0.4 app/models/hydra/content_negotiation/fedora_uri_replacer.rb
hydra-core-12.0.0 app/models/hydra/content_negotiation/fedora_uri_replacer.rb
hydra-core-11.0.1 app/models/hydra/content_negotiation/fedora_uri_replacer.rb
hydra-core-11.0.0 app/models/hydra/content_negotiation/fedora_uri_replacer.rb
hydra-core-11.0.0.rc2 app/models/hydra/content_negotiation/fedora_uri_replacer.rb
hydra-core-10.7.0 app/models/hydra/content_negotiation/fedora_uri_replacer.rb
hydra-core-10.6.2 app/models/hydra/content_negotiation/fedora_uri_replacer.rb
hydra-core-10.6.1 app/models/hydra/content_negotiation/fedora_uri_replacer.rb
hydra-core-10.6.0 app/models/hydra/content_negotiation/fedora_uri_replacer.rb
hydra-core-10.5.1 app/models/hydra/content_negotiation/fedora_uri_replacer.rb
hydra-core-11.0.0.rc1 app/models/hydra/content_negotiation/fedora_uri_replacer.rb
hydra-core-10.5.0 app/models/hydra/content_negotiation/fedora_uri_replacer.rb
hydra-core-10.4.0 app/models/hydra/content_negotiation/fedora_uri_replacer.rb