From 0d976c10989b2124487bff5d59c2294027c4fb2e Mon Sep 17 00:00:00 2001 From: Justin Coyne Date: Tue, 16 Apr 2024 10:15:28 -0500 Subject: [PATCH] Show purl endpoint This will allow purl to know whether an item should be crawlable or not --- README.md | 32 +++++++++++++++++++- app/controllers/application_controller.rb | 2 +- app/controllers/v1/purls_controller.rb | 6 ++++ config/routes.rb | 2 +- spec/factories/purl.rb | 2 +- spec/factories/release_tag.rb | 4 +++ spec/rails_helper.rb | 2 +- spec/requests/v1/purls_controller_spec.rb | 13 ++++++++ spec/requests/v1/released_controller_spec.rb | 2 +- 9 files changed, 59 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 71a55c08..6e220f50 100644 --- a/README.md +++ b/README.md @@ -95,7 +95,37 @@ rake rubocop ### Purls -#### `/purls/:druid` +#### GET `/purls/:druid` + +`GET /purls/:druid` + +##### Summary + +Display a single purl + +##### Description + +The GET `/purls/:druid` endpoint provides the ability to display a PURL document. This endpoint is used by [purl](https://github.com/sul-dlss/purl/) to know if an item should be in the sitemap + +##### Parameters + +| Name | Located In | Description | Required | Schema | Default | +| --------- | ---------- | ------------------------------------------ | -------- | ------------------------------- | ------- | +| `druid` | url | Druid of a specific PURL | Yes | string eg(`druid:cc1111dd2222`) | null | +| `version` | header | Version of the API request eg(`version=1`) | No | integer | 1 | + +##### Example Response + +```json +{ + "druid": "druid:dd111ee2222", + "latest_change": "2014-01-01T00:00:00Z", + "true_targets": ["PURL sitemap"], + "collections": ["druid:oo000oo0001"] +} +``` + +#### POST `/purls/:druid` `POST /purls/:druid` diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 80f4c0d9..971fa399 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -16,7 +16,7 @@ def per_page_params def druid_param druid = params.require(:druid) - druid = "druid:#{druid}" unless druid.include?('druid:') + druid = "druid:#{druid}" unless druid.start_with?('druid:') druid end end diff --git a/app/controllers/v1/purls_controller.rb b/app/controllers/v1/purls_controller.rb index 96fd0d1c..390bdaae 100644 --- a/app/controllers/v1/purls_controller.rb +++ b/app/controllers/v1/purls_controller.rb @@ -1,5 +1,11 @@ module V1 class PurlsController < ApplicationController + # Show the public json for the object. Used by purl to know if this object should be indexed by crawlers. + def show + purl = Purl.find_by(druid: druid_param) + render json: purl.as_public_json + end + ## # Update the database purl record from the passed in cocina def update diff --git a/config/routes.rb b/config/routes.rb index 00197e0e..18626650 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -9,7 +9,7 @@ resources :released, only: :show - resources :purls, only: [:destroy], param: :druid do + resources :purls, only: [:destroy, :show], param: :druid do member do post '/', action: 'update' end diff --git a/spec/factories/purl.rb b/spec/factories/purl.rb index a544dcd3..90f975bf 100644 --- a/spec/factories/purl.rb +++ b/spec/factories/purl.rb @@ -1,7 +1,7 @@ FactoryBot.define do factory :purl do sequence :druid do |n| - "druid:zz#{n.to_s * 3}yy#{n.to_s * 4}" + "druid:zz#{format('%03d', n)}yy0000" end published_at { 1.day.ago } end diff --git a/spec/factories/release_tag.rb b/spec/factories/release_tag.rb index 6c012cf0..68285c5d 100644 --- a/spec/factories/release_tag.rb +++ b/spec/factories/release_tag.rb @@ -3,5 +3,9 @@ purl name { 'Searchworks' } release_type { true } + + trait :sitemap do + name { 'PURL sitemap' } + end end end diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 8749bf1f..b10a0231 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -45,7 +45,7 @@ # https://relishapp.com/rspec/rspec-rails/docs config.infer_spec_type_from_file_location! - config.fixture_paths = ["#{::Rails.root}/test/fixtures"] + config.fixture_paths = [Rails.root.join('test/fixtures')] config.use_transactional_fixtures = true diff --git a/spec/requests/v1/purls_controller_spec.rb b/spec/requests/v1/purls_controller_spec.rb index 988572fc..43e378ef 100644 --- a/spec/requests/v1/purls_controller_spec.rb +++ b/spec/requests/v1/purls_controller_spec.rb @@ -1,6 +1,19 @@ require 'rails_helper' RSpec.describe V1::PurlsController do + describe 'GET show' do + let!(:release_tag) { create(:release_tag, :sitemap) } + let(:purl_object) { release_tag.purl } + let(:druid) { purl_object.druid } + + it 'displays the purl data' do + get "/purls/#{druid}" + + expect(response).to have_http_status(:ok) + expect(response.parsed_body).to include("true_targets" => ["PURL sitemap", "SearchWorksPreview", "ContentSearch"]) + end + end + describe 'POST update' do context 'with cocina json' do before do diff --git a/spec/requests/v1/released_controller_spec.rb b/spec/requests/v1/released_controller_spec.rb index 7c2bcaf7..94a9076b 100644 --- a/spec/requests/v1/released_controller_spec.rb +++ b/spec/requests/v1/released_controller_spec.rb @@ -2,7 +2,7 @@ RSpec.describe V1::ReleasedController do describe '#show' do - let!(:release_tag) { create(:release_tag, name: "PURL sitemap") } + let!(:release_tag) { create(:release_tag, :sitemap) } let(:purl) { release_tag.purl } it 'returns list of druids' do