Sha256: 96683e9effbc561268db7e3b66d6ca9594c7d699b0a84867825099a30beb0c43

Contents?: true

Size: 1.09 KB

Versions: 12

Compression:

Stored size: 1.09 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))
    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

12 entries across 12 versions & 1 rubygems

Version Path
hydra-core-9.8.0 app/models/hydra/content_negotiation/fedora_uri_replacer.rb
hydra-core-9.7.1 app/models/hydra/content_negotiation/fedora_uri_replacer.rb
hydra-core-9.7.0 app/models/hydra/content_negotiation/fedora_uri_replacer.rb
hydra-core-9.6.0 app/models/hydra/content_negotiation/fedora_uri_replacer.rb
hydra-core-9.5.0 app/models/hydra/content_negotiation/fedora_uri_replacer.rb
hydra-core-9.4.1 app/models/hydra/content_negotiation/fedora_uri_replacer.rb
hydra-core-9.4.0 app/models/hydra/content_negotiation/fedora_uri_replacer.rb
hydra-core-9.3.0 app/models/hydra/content_negotiation/fedora_uri_replacer.rb
hydra-core-9.2.2 app/models/hydra/content_negotiation/fedora_uri_replacer.rb
hydra-core-9.2.1 app/models/hydra/content_negotiation/fedora_uri_replacer.rb
hydra-core-9.2.0 app/models/hydra/content_negotiation/fedora_uri_replacer.rb
hydra-core-9.2.0.rc1 app/models/hydra/content_negotiation/fedora_uri_replacer.rb