Skip to content

Commit

Permalink
Expose an API for reading data
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoyne committed Jun 14, 2016
1 parent 5ea33fe commit a7266a7
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 5 deletions.
23 changes: 23 additions & 0 deletions app/connections/clean_connection.rb
@@ -0,0 +1,23 @@
# This stands in for ActiveFedora::CleanConnection. It behaves the same way,
# but it doesn't clear the has_model assertion
class CleanConnection < SimpleDelegator
def get(*args)
result = __getobj__.get(*args) do |req|
prefer_headers = Ldp::PreferHeaders.new(req.headers["Prefer"])
prefer_headers.omit = prefer_headers.omit | omit_uris
req.headers["Prefer"] = prefer_headers.to_s
end
result
end

private

def omit_uris
[
::RDF::Vocab::Fcrepo4.ServerManaged,
::RDF::Vocab::LDP.PreferContainment,
::RDF::Vocab::LDP.PreferEmptyContainer,
::RDF::Vocab::LDP.PreferMembership
]
end
end
Expand Up @@ -63,6 +63,7 @@ def show
render :show, status: :ok
end
additional_response_formats(wants)
document_export_formats(wants)
end
end

Expand Down Expand Up @@ -179,6 +180,24 @@ def contextual_path(presenter, parent_presenter)
::CurationConcerns::ContextualPath.new(presenter, parent_presenter).show
end

def document_export_formats(format)
format.any do
format_name = params.fetch(:format, '').to_sym
raise ActionController::UnknownFormat unless presenter.export_formats.include? format_name
render_document_export_format format_name
end
end

##
# Render the document export formats for a response
# First, try to render an appropriate template (e.g. index.endnote.erb)
# If that fails, just concatenate the document export responses with a newline.
def render_document_export_format(format_name)
render
rescue ActionView::MissingTemplate
render text: presenter.export_as(format_name), layout: false
end

private

def curation_concern_from_search_results
Expand Down
Expand Up @@ -148,5 +148,14 @@ def date_field(field_name)
Rails.logger.info "Unable to parse date: #{field.first.inspect} for #{self['id']}"
end
end

# This overrides the connection provided by Hydra::ContentNegotiation so we
# can get the model too.
module ConnectionWithModel
def connection
# TODO: clean the fedora added triples out.
@connection ||= CleanConnection.new(ActiveFedora.fedora.connection)
end
end
end
end
2 changes: 1 addition & 1 deletion app/presenters/curation_concerns/work_show_presenter.rb
Expand Up @@ -16,7 +16,7 @@ class WorkShowPresenter
self.work_presenter_class = self

# Methods used by blacklight helpers
delegate :has?, :first, :fetch, to: :solr_document
delegate :has?, :first, :fetch, :export_formats, :export_as, to: :solr_document

# @param [SolrDocument] solr_document
# @param [Ability] current_ability
Expand Down
14 changes: 14 additions & 0 deletions lib/curation_concerns.rb
Expand Up @@ -6,4 +6,18 @@
require 'kaminari_route_prefix'

module CurationConcerns
# This method is called once for each statement in the graph.
def self.id_to_resource_uri
lambda do |id, graph|
result = graph.query([nil, ActiveFedora::RDF::Fcrepo::Model.hasModel, nil]).first
route_key = result.object.to_s.constantize.model_name.singular_route_key
routes = Rails.application.routes.url_helpers
builder = ActionDispatch::Routing::PolymorphicRoutes::HelperMethodBuilder
builder.polymorphic_method routes, route_key, nil, :url, id: id, host: hostname
end
end

def self.hostname
config.hostname
end
end
6 changes: 6 additions & 0 deletions lib/curation_concerns/configuration.rb
Expand Up @@ -143,6 +143,12 @@ def ingest_queue_name
@ingest_queue_name ||= :default
end

# Hostname is used for the externally visible URI when RDF is requested
attr_writer :hostname
def hostname
@hostname ||= 'localhost'
end

callback.enable :after_create_concern, :after_create_fileset,
:after_update_content, :after_revert_content,
:after_update_metadata, :after_import_local_file_success,
Expand Down
1 change: 1 addition & 0 deletions lib/curation_concerns/engine.rb
Expand Up @@ -50,6 +50,7 @@ class Engine < ::Rails::Engine
ActiveFedora::Noid.config.template = c.noid_template
ActiveFedora::Noid.config.statefile = c.minter_statefile
end
Hydra.config.id_to_resource_uri = CurationConcerns.id_to_resource_uri
end
end
end
6 changes: 6 additions & 0 deletions lib/generators/curation_concerns/install_generator.rb
Expand Up @@ -80,6 +80,12 @@ def inject_solr_document_behavior
"\n # Adds CurationConcerns behaviors to the SolrDocument.\n" \
" include CurationConcerns::SolrDocumentBehavior\n"
end

insert_into_file file_path, after: /use_extension\(\s*Hydra::ContentNegotiation\s*\)/ do
"\n\n # Overrides the connection provided by Hydra::ContentNegotiation so we" \
"\n # can get the model too." \
"\n use_extension(ConnectionWithModel)"
end
else
puts " \e[31mFailure\e[0m CurationConcerns requires a SolrDocument object. This generators assumes that the model is defined in the file #{file_path}, which does not exist."
end
Expand Down
Expand Up @@ -56,6 +56,9 @@
# Leaving it blank will set the start date to when ever the file was uploaded by
# NOTE: if you have always sent analytics to GA for downloads and page views leave this commented out
# config.analytic_start_date = DateTime.new(2014,9,10)

# Hostname is used for the externally visible URI when RDF is requested
# config.hostname = 'localhost'
end

Date::DATE_FORMATS[:standard] = '%m/%d/%Y'
Expand Up @@ -13,6 +13,7 @@
get :show, id: a_work
expect(response).to be_success
end

context "with a parent work" do
render_views
it "renders a breadcrumb" do
Expand All @@ -35,10 +36,20 @@

context 'someone elses public work' do
let(:a_work) { create(:public_generic_work) }
it 'shows me the page' do
expect(controller). to receive(:additional_response_formats).with(ActionController::MimeResponds::Collector)
get :show, id: a_work
expect(response).to be_success
context "html" do
it 'shows me the page' do
expect(controller). to receive(:additional_response_formats).with(ActionController::MimeResponds::Collector)
get :show, id: a_work
expect(response).to be_success
end
end
context "ttl" do
it 'renders an turtle file' do
get :show, id: a_work, format: :ttl
expect(response).to be_successful
expect(response.body).to start_with "\n<http://localhost/concern/generic_works/#{a_work.id}>"
expect(response.body).to match %r{<http://purl\.org/dc/terms/title> "Test title";}
end
end
end

Expand Down

0 comments on commit a7266a7

Please sign in to comment.