From 3a90f00bf123269510566b6dc917e99dab2938f5 Mon Sep 17 00:00:00 2001 From: Yetrina Battad Date: Wed, 26 Jun 2024 08:43:57 +1000 Subject: [PATCH 1/6] feat: implement service call --- app/services/catalogue_services_client.rb | 15 +++++++ .../catalogue_services/no_related_docs.json | 3 ++ .../catalogue_services/related_docs.json | 36 +++++++++++++++ .../catalogue_services_client_spec.rb | 45 +++++++++++++++++++ 4 files changed, 99 insertions(+) create mode 100644 spec/files/catalogue_services/no_related_docs.json create mode 100644 spec/files/catalogue_services/related_docs.json diff --git a/app/services/catalogue_services_client.rb b/app/services/catalogue_services_client.rb index eb1bf035..cbcbaaf2 100644 --- a/app/services/catalogue_services_client.rb +++ b/app/services/catalogue_services_client.rb @@ -76,6 +76,21 @@ def get_access_conditions(instance_id:, holdings_id:) item ? item["note"] : nil end + def related_docs(objid:) + conn = setup_connection + + res = conn.get("/catalogue-services/ead/relateddocs/#{objid}") + if res.status == 200 + if res.body.present? + res.body["docs"].presence + end + else + Rails.logger.error "Failed to retrieve related documents for #{objid}" + end + rescue => e + Rails.logger.error "related_docs - Failed to connect catalogue-service: #{e.message}" + end + private def setup_connection diff --git a/spec/files/catalogue_services/no_related_docs.json b/spec/files/catalogue_services/no_related_docs.json new file mode 100644 index 00000000..6f9406d4 --- /dev/null +++ b/spec/files/catalogue_services/no_related_docs.json @@ -0,0 +1,3 @@ +{ + "docs": [] +} diff --git a/spec/files/catalogue_services/related_docs.json b/spec/files/catalogue_services/related_docs.json new file mode 100644 index 00000000..79ecd226 --- /dev/null +++ b/spec/files/catalogue_services/related_docs.json @@ -0,0 +1,36 @@ +{ + "docs": [ + { + "id": "nla.obj-367760674", + "copyRole": "fas", + "fileName": "Finding aid supplementary", + "externalComments": "Appendix C - Items in Series 12.1", + "fileType": "pdf", + "fileSize": 124305 + }, + { + "id": "nla.obj-367758458", + "copyRole": "fas", + "fileName": "Finding aid supplementary", + "externalComments": "Appendix B - Selected items in Subseries 5.2", + "fileType": "pdf", + "fileSize": 113986 + }, + { + "id": "nla.obj-367758206", + "copyRole": "fas", + "fileName": "Finding aid supplementary", + "externalComments": "Appendix A - Concordance (Box List)", + "fileType": "pdf", + "fileSize": 401572 + }, + { + "id": "nla.obj-367766877", + "copyRole": "fas", + "fileName": "Finding aid supplementary", + "externalComments": "Appendix D - Items in Series 17", + "fileType": "pdf", + "fileSize": 200793 + } + ] +} diff --git a/spec/services/catalogue_services_client_spec.rb b/spec/services/catalogue_services_client_spec.rb index 174501ae..2c25cada 100644 --- a/spec/services/catalogue_services_client_spec.rb +++ b/spec/services/catalogue_services_client_spec.rb @@ -57,4 +57,49 @@ expect(service.get_access_conditions(instance_id: "08aed703-3648-54d0-80ef-fddb3c635731", holdings_id: "d6c97d9e-dfe6-5faa-9f0b-020b2bddbf8c")).to be_nil end end + + describe "#related_docs" do + context "when there are related records" do + before do + cat_response = IO.read("spec/files/catalogue_services/related_docs.json") + WebMock.stub_request(:get, /catservices.test\/catalogue-services\/ead\/relateddocs\/nla.obj-123/) + .to_return(status: 200, body: cat_response, headers: {"Content-Type" => "application/json"}) + end + + let(:related_docs) { service.related_docs(objid: "nla.obj-123") } + + it "returns the related records" do + expect(related_docs).not_to be_nil + expect(related_docs.size).to eq 4 + expect(related_docs.first["id"]).to eq "nla.obj-367760674" + expect(related_docs.first["copyRole"]).to eq "fas" + expect(related_docs.first["fileName"]).to eq "Finding aid supplementary" + expect(related_docs.first["fileType"]).to eq "pdf" + expect(related_docs.first["fileSize"]).to eq 124305 + end + end + + context "when there are no related records" do + before do + cat_response = IO.read("spec/files/catalogue_services/no_related_docs.json") + WebMock.stub_request(:get, /catservices.test\/catalogue-services\/ead\/relateddocs\/nla.obj-123/) + .to_return(status: 200, body: cat_response, headers: {"Content-Type" => "application/json"}) + end + + it "returns an empty response" do + expect(service.related_docs(objid: "nla.obj-123")).to be_nil + end + end + + context "when failed to connect to catalogue-service" do + before do + WebMock.stub_request(:get, /catservices.test\/catalogue-services\/ead\/relateddocs\/nla.obj-123/) + .to_return(status: 400, body: "", headers: {"Content-Type" => "application/json"}) + end + + it "returns an empty response" do + expect(service.related_docs(objid: "nla.obj-123")).not_to be_nil + end + end + end end From 32e7a24d18f8e0c79ea1dbe5f73421ad91ef9188 Mon Sep 17 00:00:00 2001 From: Yetrina Battad Date: Wed, 26 Jun 2024 11:40:17 +1000 Subject: [PATCH 2/6] feat: implement rendering of related document list --- .../related_documents_component.html.erb | 16 +++++++++ app/components/related_documents_component.rb | 13 +++++++ app/controllers/catalog_controller.rb | 12 ++++++- app/helpers/field_helper.rb | 7 ++++ app/helpers/file_size_helper.rb | 15 ++++++++ app/models/related_document.rb | 34 +++++++++++++++++++ app/models/solr_document.rb | 1 + app/services/catalogue_services_client.rb | 13 ++++--- config/locales/arclight.en.yml | 2 +- config/locales/en.yml | 4 ++- .../related_documents_component_spec.rb | 15 ++++++++ .../catalogue_services_client_spec.rb | 6 ++-- 12 files changed, 127 insertions(+), 11 deletions(-) create mode 100644 app/components/related_documents_component.html.erb create mode 100644 app/components/related_documents_component.rb create mode 100644 app/helpers/field_helper.rb create mode 100644 app/helpers/file_size_helper.rb create mode 100644 app/models/related_document.rb create mode 100644 spec/components/related_documents_component_spec.rb diff --git a/app/components/related_documents_component.html.erb b/app/components/related_documents_component.html.erb new file mode 100644 index 00000000..6fb8c0b7 --- /dev/null +++ b/app/components/related_documents_component.html.erb @@ -0,0 +1,16 @@ +
    + <% related_docs.each do |doc| %> + <% cache doc do %> +
  1. + + + <%= doc.link_text %> + + + <% if doc.externalComments.present? %> +

    <%= doc.externalComments %>

    + <% end %> +
  2. + <% end %> + <% end %> +
diff --git a/app/components/related_documents_component.rb b/app/components/related_documents_component.rb new file mode 100644 index 00000000..79df819b --- /dev/null +++ b/app/components/related_documents_component.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +class RelatedDocumentsComponent < Blacklight::Component + attr_reader :related_docs + + def initialize(related_docs:) + @related_docs = related_docs + end + + def render? + related_docs.present? + end +end diff --git a/app/controllers/catalog_controller.rb b/app/controllers/catalog_controller.rb index 69dc4752..eff18d88 100644 --- a/app/controllers/catalog_controller.rb +++ b/app/controllers/catalog_controller.rb @@ -381,7 +381,11 @@ class CatalogController < ApplicationController values: ->(__field_config, document, _context) { document.extract_notes_by_header("box_list") } # Collection Show Page - Related Section - config.add_related_field "relatedmaterial", field: "relatedmaterial_html_tesm", helper_method: :render_html_tags + config.add_related_field "relatedmaterial", + label: I18n.t("blacklight.search.fields.relatedmaterial"), + helper_method: :render_related_docs, + values: ->(__field_config, document, _context) { CatalogueServicesClient.new.related_docs(pid: "nla.obj-#{document.pid}") }, + if: ->(_controller, _config, document) { document.pid.present? } config.add_related_field "separatedmaterial", field: "separatedmaterial_html_tesm", helper_method: :render_html_tags config.add_related_field "otherfindaid", field: "otherfindaid_html_tesm", helper_method: :render_html_tags config.add_related_field "altformavail", field: "altformavail_html_tesm", helper_method: :render_html_tags @@ -481,4 +485,10 @@ class CatalogController < ApplicationController # Group header values config.add_group_header_field "abstract_or_scope", accessor: true, truncate: true, helper_method: :render_html_tags end + + private + + def has_pid? + document.pid.present? + end end diff --git a/app/helpers/field_helper.rb b/app/helpers/field_helper.rb new file mode 100644 index 00000000..6f022a5f --- /dev/null +++ b/app/helpers/field_helper.rb @@ -0,0 +1,7 @@ +module FieldHelper + def render_related_docs(document:, field:, config:, value:, context:) + if document.pid.present? + render RelatedDocumentsComponent.new(related_docs: value) + end + end +end diff --git a/app/helpers/file_size_helper.rb b/app/helpers/file_size_helper.rb new file mode 100644 index 00000000..86d10af9 --- /dev/null +++ b/app/helpers/file_size_helper.rb @@ -0,0 +1,15 @@ +module FileSizeHelper + def format_filesize(size) + if size.present? + if size.to_i < 1024 + "#{size} B" + elsif size.to_i < 1048576 # 1024 * 1024 + "#{(size.to_f / 1024).round(2)} KB" + elsif size.to_i < 1073741824 # 1024 * 1024 * 1024 + "#{(size.to_f / 1048576).round(2)} MB" + else + "#{(size.to_f / 1073741824).round(2)} GB" + end + end + end +end diff --git a/app/models/related_document.rb b/app/models/related_document.rb new file mode 100644 index 00000000..f275fe0f --- /dev/null +++ b/app/models/related_document.rb @@ -0,0 +1,34 @@ +# frozen_string_literal: true + +class RelatedDocument < OpenStruct + def initializer(attributes) + super(to_ostruct(attributes)) + end + + def to_ostruct(object) + case object + when Hash + OpenStruct.new(object.map { |key, value| [key.to_sym, value] }.to_h) + when Array + object.map! { |i| to_ostruct(i) } + else # a primitive value + object + end + end + + def link_text + if copyRole == "fas" + "#{I18n.t("related_docs.link_text_fas")} ( Type: #{fileType}, Size: #{ApplicationController.helpers.format_filesize(fileSize)})" + else + "#{I18n.t("related_docs.link_text")} ( Type: #{fileType}, Size: #{ApplicationController.helpers.format_filesize.call(fileSize)})" + end + end + + def download_url + if copyRole == "fas" + "https://nla.gov.au/#{pid}/relateddocs/download?copyRole=#{copyRole}©Id=#{id}" + else + "https://nla.gov.au/#{pid}/relateddocs/download?copyRole=#{copyRole}" + end + end +end diff --git a/app/models/solr_document.rb b/app/models/solr_document.rb index 22d8e473..c62d9302 100644 --- a/app/models/solr_document.rb +++ b/app/models/solr_document.rb @@ -12,6 +12,7 @@ class SolrDocument attribute :physdesc_dimensions, :string, "physdesc_dimensions_ssi" attribute :physdesc_facet, :string, "physdesc_facet_ssi" attribute :notes, :array, "note_json_ssm" + attribute :pid, :string, "ead_ssi" # **** BEWARE! **** # Arclight aliases "level_ssm" and "unitid_ssm" fields to String type attributes in the diff --git a/app/services/catalogue_services_client.rb b/app/services/catalogue_services_client.rb index cbcbaaf2..2eed82a2 100644 --- a/app/services/catalogue_services_client.rb +++ b/app/services/catalogue_services_client.rb @@ -76,16 +76,19 @@ def get_access_conditions(instance_id:, holdings_id:) item ? item["note"] : nil end - def related_docs(objid:) + def related_docs(pid:) conn = setup_connection - res = conn.get("/catalogue-services/ead/relateddocs/#{objid}") + res = conn.get("/catalogue-services/ead/relateddocs/#{pid}") if res.status == 200 - if res.body.present? - res.body["docs"].presence + if res.body.present? && res.body["docs"].present? + res.body["docs"].map do |attrs| + attrs[:pid] = pid # insert the pid since we need it to construct the download URL + RelatedDocument.new(attrs) + end end else - Rails.logger.error "Failed to retrieve related documents for #{objid}" + Rails.logger.error "Failed to retrieve related documents for #{pid}" end rescue => e Rails.logger.error "related_docs - Failed to connect catalogue-service: #{e.message}" diff --git a/config/locales/arclight.en.yml b/config/locales/arclight.en.yml index 552c00ce..fb837a98 100644 --- a/config/locales/arclight.en.yml +++ b/config/locales/arclight.en.yml @@ -25,7 +25,7 @@ en: descrules: Rules or conventions extentsinfo: Extent information - relatedmaterial: Related Material + relatedmaterial: Related Documentation separatedmaterial: Separated Material otherfindaid: Other Finding Aids altformavail: Existence and Location of Copies diff --git a/config/locales/en.yml b/config/locales/en.yml index 8186a2d2..bd905b2a 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -55,4 +55,6 @@ en: container_list: "Container List" box_list: "Box List" bibref: "Bibliographic Reference(s)" - + related_docs: + link_text_fas: "Supplementary finding aid" + link_text: "Transcript" diff --git a/spec/components/related_documents_component_spec.rb b/spec/components/related_documents_component_spec.rb new file mode 100644 index 00000000..fe1c2dbe --- /dev/null +++ b/spec/components/related_documents_component_spec.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +require "rails_helper" + +RSpec.describe RelatedDocumentsComponent, type: :component do + pending "add some examples to (or delete) #{__FILE__}" + + # it "renders something useful" do + # expect( + # render_inline(described_class.new(attr: "value")) { "Hello, components!" }.css("p").to_html + # ).to include( + # "Hello, components!" + # ) + # end +end diff --git a/spec/services/catalogue_services_client_spec.rb b/spec/services/catalogue_services_client_spec.rb index 2c25cada..43be73f2 100644 --- a/spec/services/catalogue_services_client_spec.rb +++ b/spec/services/catalogue_services_client_spec.rb @@ -66,7 +66,7 @@ .to_return(status: 200, body: cat_response, headers: {"Content-Type" => "application/json"}) end - let(:related_docs) { service.related_docs(objid: "nla.obj-123") } + let(:related_docs) { service.related_docs(pid: "nla.obj-123") } it "returns the related records" do expect(related_docs).not_to be_nil @@ -87,7 +87,7 @@ end it "returns an empty response" do - expect(service.related_docs(objid: "nla.obj-123")).to be_nil + expect(service.related_docs(pid: "nla.obj-123")).to be_nil end end @@ -98,7 +98,7 @@ end it "returns an empty response" do - expect(service.related_docs(objid: "nla.obj-123")).not_to be_nil + expect(service.related_docs(pid: "nla.obj-123")).not_to be_nil end end end From 79fde270a89922ff5a617ed9885d5d8d96686577 Mon Sep 17 00:00:00 2001 From: Yetrina Battad Date: Wed, 26 Jun 2024 11:52:48 +1000 Subject: [PATCH 3/6] feat: cache related docs request response --- app/services/catalogue_services_client.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/services/catalogue_services_client.rb b/app/services/catalogue_services_client.rb index 2eed82a2..f00cff6f 100644 --- a/app/services/catalogue_services_client.rb +++ b/app/services/catalogue_services_client.rb @@ -79,7 +79,7 @@ def get_access_conditions(instance_id:, holdings_id:) def related_docs(pid:) conn = setup_connection - res = conn.get("/catalogue-services/ead/relateddocs/#{pid}") + res = Rails.cache.fetch([pid, :related_docs], expires_in: 10.minutes) { conn.get("/catalogue-services/ead/relateddocs/#{pid}") } if res.status == 200 if res.body.present? && res.body["docs"].present? res.body["docs"].map do |attrs| From 76830e3a945ef6db040fa03fce294f3e0b81c4e0 Mon Sep 17 00:00:00 2001 From: Yetrina Battad Date: Wed, 26 Jun 2024 11:53:51 +1000 Subject: [PATCH 4/6] feat: modify related docs download URL --- app/models/related_document.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/related_document.rb b/app/models/related_document.rb index f275fe0f..41600e74 100644 --- a/app/models/related_document.rb +++ b/app/models/related_document.rb @@ -18,7 +18,7 @@ def to_ostruct(object) def link_text if copyRole == "fas" - "#{I18n.t("related_docs.link_text_fas")} ( Type: #{fileType}, Size: #{ApplicationController.helpers.format_filesize(fileSize)})" + "#{I18n.t("related_docs.link_text_fas")} (Type: #{fileType}, Size: #{ApplicationController.helpers.format_filesize(fileSize)})" else "#{I18n.t("related_docs.link_text")} ( Type: #{fileType}, Size: #{ApplicationController.helpers.format_filesize.call(fileSize)})" end From d954e41eec0282f35b52fcc2cd19caf0fbe6d8fd Mon Sep 17 00:00:00 2001 From: Yetrina Battad Date: Wed, 26 Jun 2024 11:54:27 +1000 Subject: [PATCH 5/6] refactor: restyle related documents link --- app/components/related_documents_component.html.erb | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/app/components/related_documents_component.html.erb b/app/components/related_documents_component.html.erb index 6fb8c0b7..e32df00d 100644 --- a/app/components/related_documents_component.html.erb +++ b/app/components/related_documents_component.html.erb @@ -2,11 +2,7 @@ <% related_docs.each do |doc| %> <% cache doc do %>
  • - - - <%= doc.link_text %> - - + <%= doc.link_text %> <% if doc.externalComments.present? %>

    <%= doc.externalComments %>

    <% end %> From 929e1b80c17a82de147616649fb569f83622467d Mon Sep 17 00:00:00 2001 From: Yetrina Battad Date: Wed, 26 Jun 2024 14:14:07 +1000 Subject: [PATCH 6/6] test: add tests for related documents --- .env.test | 10 ++- .../related_documents_component.html.erb | 2 +- app/helpers/file_size_helper.rb | 2 + app/models/related_document.rb | 17 +---- app/services/catalogue_services_client.rb | 2 + config/environments/test.rb | 11 +-- .../related_documents_component_spec.rb | 53 +++++++++++--- spec/features/related_documents_spec.rb | 56 +++++++++++++++ spec/helpers/file_size_helper_spec.rb | 31 +++++++++ spec/models/related_document_spec.rb | 69 +++++++++++++++++++ .../catalogue_services_client_spec.rb | 15 +++- spec/support/webmock.rb | 9 +++ 12 files changed, 233 insertions(+), 44 deletions(-) create mode 100644 spec/features/related_documents_spec.rb create mode 100644 spec/helpers/file_size_helper_spec.rb create mode 100644 spec/models/related_document_spec.rb diff --git a/.env.test b/.env.test index ea7f31bf..341fa390 100644 --- a/.env.test +++ b/.env.test @@ -15,12 +15,10 @@ GLOBAL_MESSAGE_URL=http://test.nla.gov.au/catalogue-message/1234 BOOTSNAP_CACHE_DIR=./tmp # Requesting -CATALOGUE_SERVICES_API_BASE_URL="define CATALOGUE_SERVICES_API_BASE_URL" -CATALOGUE_SERVICES_CLIENT="define CATALOGUE_SERVICES_CLIENT" -CATALOGUE_SERVICES_SECRET="define CATALOGUE_SERVICES_SECRET" -CATALOGUE_SERVICES_REALM="define CATALOGUE_SERVICES_REALM" +CATALOGUE_SERVICES_API_BASE_URL=http://catservices.test +CATALOGUE_SERVICES_CLIENT="catalogue-services" +CATALOGUE_SERVICES_SECRET="254241c8-1e99-4855-a0ae-52b04702c3e5" +CATALOGUE_SERVICES_REALM="example-realm" # turn of functionality during FOLIO update FOLIO_UPDATE_IN_PROGRESS= - -CATALOGUE_SERVICES_API_BASE_URL=http://catservices.test diff --git a/app/components/related_documents_component.html.erb b/app/components/related_documents_component.html.erb index e32df00d..e907b4dc 100644 --- a/app/components/related_documents_component.html.erb +++ b/app/components/related_documents_component.html.erb @@ -1,4 +1,4 @@ -
      +