Skip to content

Commit

Permalink
Resolves #566 by handling cases in which metadata can be rendered as …
Browse files Browse the repository at this point in the history
…XML (using CodeRay); Ensures that metadata retrieved using the HTTP can be offered by endpoints issuing redirect responses; Reintroducing core support for the MODS as a metadata type

Cleaning the markup in the metadata markup template
  • Loading branch information
jrgriffiniii committed Aug 23, 2017
1 parent aac81fc commit ed123ee
Show file tree
Hide file tree
Showing 15 changed files with 97 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//= require bootstrap/dropdown
//= require bootstrap/tab

!(function(global) {

Expand Down
1 change: 1 addition & 0 deletions app/assets/stylesheets/geoblacklight/_geoblacklight.scss
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
@import 'modules/metadata';
@import 'modules/metadata_content';
@import 'modules/metadata_missing';
@import 'modules/metadata_markup';
@import 'modules/results';
@import 'modules/geosearch';
@import 'modules/search_widgets';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* Rules for untransformed geospatial metadata markup
*
*/
#metadata-markup-container {
.alert {
margin-top: 20px;
}
}
7 changes: 5 additions & 2 deletions app/helpers/geoblacklight_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,11 @@ def render_facet_item_with_icon(field_name, item)
# @return [String]
def render_transformed_metadata(metadata)
render partial: 'catalog/metadata/content', locals: { content: metadata.transform.html_safe }
rescue Geoblacklight::MetadataTransformer::ParseError => e
Geoblacklight.logger.warn e.message
rescue Geoblacklight::MetadataTransformer::TransformError => transform_err
Geoblacklight.logger.warn transform_err.message
render partial: 'catalog/metadata/markup', locals: { content: metadata.to_xml }
rescue => err
Geoblacklight.logger.warn err.message
render partial: 'catalog/metadata/missing'
end

Expand Down
1 change: 0 additions & 1 deletion app/views/catalog/_metadata.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<% document ||= @document %>

<div class='metadata-view'>

<div class="container-fluid">
<ul class="nav nav-pills" role="tablist">
<% document.references.shown_metadata.each do |metadata| %>
Expand Down
8 changes: 8 additions & 0 deletions app/views/catalog/metadata/_markup.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<div id="metadata-markup-container" class="container-fluid">
<div class="alert alert-warning" role="alert">
<span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
<span class="sr-only">Error:</span>
<span>The metadata for this item cannot be transformed.</span>
</div>
<%= CodeRay.scan(content, :xml).div.html_safe %>
</div>
2 changes: 2 additions & 0 deletions geoblacklight.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ Gem::Specification.new do |spec|
spec.add_dependency 'font-awesome-rails'
spec.add_dependency 'config'
spec.add_dependency 'faraday'
spec.add_dependency 'faraday_middleware'
spec.add_dependency 'coderay'
spec.add_dependency 'geoblacklight-icons', '>= 0.2'
spec.add_dependency 'deprecation'
spec.add_dependency 'geo_combine', '>= 0.3'
Expand Down
1 change: 1 addition & 0 deletions lib/generators/geoblacklight/templates/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ INSTITUTION: 'Stanford'

# Metadata shown in tool panel
METADATA_SHOWN:
- 'mods'
- 'fgdc'
- 'iso19139'

Expand Down
2 changes: 2 additions & 0 deletions lib/geoblacklight/engine.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
require 'blacklight'
require 'leaflet-rails'
require 'font-awesome-rails'
require 'coderay'
require 'config'
require 'faraday'
require 'faraday_middleware'
require 'nokogiri'
require 'geoblacklight-icons'

Expand Down
8 changes: 6 additions & 2 deletions lib/geoblacklight/metadata/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class Base
delegate :type, to: :reference
delegate :to_html, to: :metadata
delegate :transform, to: :transformer
delegate :to_xml, to: :document

##
# Instantiates a Geoblacklight::Metadata object used for retrieving and
Expand Down Expand Up @@ -44,8 +45,11 @@ def endpoint
# @return [String, nil] metadata string or nil if there is a
# connection error
def retrieve_metadata
conn = Faraday.new(url: @reference.endpoint)
response = conn.get
connection = Faraday.new(url: @reference.endpoint) do |conn|
conn.use FaradayMiddleware::FollowRedirects
conn.adapter Faraday.default_adapter
end
response = connection.get
return response.body unless response.nil? || response.status == 404
Geoblacklight.logger.error "Could not reach #{@reference.endpoint}"
''
Expand Down
11 changes: 8 additions & 3 deletions lib/geoblacklight/metadata_transformer/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def initialize(metadata)
# @return [String] the transformed metadata in the HTML
def transform
cleaned_metadata.to_html
rescue StandardError => e
rescue => e
raise TransformError, e.message
end

Expand All @@ -29,8 +29,13 @@ def transform
# @return [Nokogiri::XML::Document] the Nokogiri XML Document for the cleaned HTML
def cleaned_metadata
transformed_doc = Nokogiri::XML(@metadata.to_html)
transformed_elem = transformed_doc.xpath('//body').children
transformed_elem || Nokogiri::XML
if transformed_doc.xpath('//body').children.empty?
fail TransformError,\
'Failed to extract the <body> child elements from the transformed metadata'
end
transformed_doc.xpath('//body').children
rescue => e
raise TransformError, e.message
end

##
Expand Down
17 changes: 14 additions & 3 deletions spec/features/metadata_panel_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,29 @@

feature 'Metadata tools' do
feature 'when metadata references are available', js: :true do
scenario 'shows up in tools' do
visit solr_document_path 'stanford-cg357zz0321'
scenario 'shows up as HTML' do
visit solr_document_path 'columbia-columbia-landinfo-global-aet'
expect(page).to have_css 'li.metadata a', text: 'Metadata'
click_link 'Metadata'
using_wait_time 15 do
within '.metadata-view' do
expect(page).to have_css '.pill-metadata', text: 'ISO 19139'
expect(page).to have_css '.pill-metadata', text: 'FGDC'
expect(page).to have_css 'dt', text: 'Identification Information'
expect(page).to have_css 'dt', text: 'Metadata Reference Information'
end
end
end
scenario 'shows up as XML' do
visit solr_document_path 'stanford-cg357zz0321'
expect(page).to have_css 'li.metadata a', text: 'Metadata'
click_link 'Metadata'
using_wait_time 15 do
within '.metadata-view' do
expect(page).to have_css '.pill-metadata', text: 'MODS'
expect(page).to have_css '.CodeRay'
end
end
end
end
feature 'when metadata references are not available' do
scenario 'is not in tools' do
Expand Down
12 changes: 12 additions & 0 deletions spec/helpers/geoblacklight_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,18 @@
end
end

context 'with valid XML data without an HTML transform' do
before do
allow(metadata).to receive(:transform).and_raise(Geoblacklight::MetadataTransformer::TransformError)
allow(metadata).to receive(:to_xml).and_return('<data></data>')
end

it 'renders the partial with metadata content' do
expect(helper).to receive(:render).with(partial: 'catalog/metadata/markup', locals: { content: '<data></data>' })
helper.render_transformed_metadata(metadata)
end
end

context 'without XML data' do
before do
allow(metadata).to receive(:transform).and_raise(Geoblacklight::MetadataTransformer::ParseError)
Expand Down
26 changes: 26 additions & 0 deletions spec/lib/geoblacklight/metadata_transformer/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,30 @@
expect { described_class.new(nil) }.to raise_error Geoblacklight::MetadataTransformer::EmptyMetadataError
end
end

context 'with metadata types without XSL Stylesheets' do
let(:metadata) { instance_double(GeoCombine::Metadata) }
subject { described_class.new(metadata) }
describe '#transform' do
before do
allow(metadata).to receive(:to_html).and_raise(NoMethodError, 'undefined method `to_html\'')
end
it 'raises a transform error' do
expect { subject.transform }.to raise_error Geoblacklight::MetadataTransformer::TransformError, /undefined method `to_html'/
end
end
end

context 'with metadata types with XSL Stylesheets but invalid HTML' do
let(:metadata) { instance_double(GeoCombine::Metadata) }
subject { described_class.new(metadata) }
describe '#transform' do
before do
allow(metadata).to receive(:to_html).and_return('<invalid-html></invalid-html>')
end
it 'raises a transform error' do
expect { subject.transform }.to raise_error Geoblacklight::MetadataTransformer::TransformError, 'Failed to extract the <body> child elements from the transformed metadata'
end
end
end
end
4 changes: 2 additions & 2 deletions spec/lib/geoblacklight/references_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,12 @@
end
describe 'shown_metadata_refs' do
it 'is a set of metadata references exposed by the configuration' do
expect(complex_shapefile.shown_metadata_refs.count).to eq 1
expect(complex_shapefile.shown_metadata_refs.count).to eq 2
end
end
describe 'shown_metadata' do
it 'is a set of metadata resources exposed by the configuration' do
expect(complex_shapefile.shown_metadata.count).to eq 1
expect(complex_shapefile.shown_metadata.count).to eq 2
expect(complex_shapefile.shown_metadata.first).to be_a Geoblacklight::Metadata::Base
end
end
Expand Down

0 comments on commit ed123ee

Please sign in to comment.