-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add view components for rendering google scholar meta tags for scrapi…
…ng. (#104) * Add view components for rendering google scholar meta tags for scraping. * Adds tests for google scholar meta tags * Add google_scholar_metadata_component_spec.rb * Moved let statement to top level in google_scholar_metadata test
- Loading branch information
Showing
7 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<!-- Google Scholar metadata --> | ||
<meta name="citation_title" content="<%= @document[:title_tesi] %>"> | ||
<meta name="citation_author" content="<%= @document[:author_name_tesi] %>"> | ||
<meta name="citation_publication_date" content="<%= citation_publication_date %>"> | ||
<%- if citation_pdf_url %> | ||
<meta name="citation_pdf_url" content="<%= citation_pdf_url %>"> | ||
<%- end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# frozen_string_literal: true | ||
|
||
class GoogleScholarMetadataComponent < ViewComponent::Base | ||
attr_reader :document | ||
|
||
def initialize(document) | ||
super | ||
@document = document.fetch(:document) | ||
end | ||
|
||
def citation_publication_date | ||
document[:released_metadata_at_dtsi]&.to_date&.year | ||
end | ||
|
||
def citation_pdf_url | ||
return nil unless document.access_level.current_access_level == 'open_access' | ||
|
||
document.final_submissions.each do |key, value| | ||
return final_submission_file_url(key) if File.extname(value) == '.pdf' | ||
end | ||
|
||
nil | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe GoogleScholarMetadataComponent, type: :component do | ||
include Rails.application.routes.url_helpers | ||
|
||
subject(:component) { described_class.new(document: doc) } | ||
|
||
let(:doc) { SolrDocument.new(fake_solr_doc) } | ||
let(:html) { render_inline(component) } | ||
|
||
before do | ||
Blacklight.default_index.connection.add(doc) | ||
Blacklight.default_index.connection.commit | ||
end | ||
|
||
context 'with a final submission file pdf' do | ||
let(:fake_solr_doc) { FakeSolrDocument.new(access_level: 'open_access').doc } | ||
|
||
it 'renders all the meta tags' do | ||
expect(html.search('meta[@name="citation_title"]') | ||
.first['content']).to eq(doc[:title_tesi]) | ||
expect(html.search('meta[@name="citation_author"]') | ||
.first['content']).to eq(doc[:author_name_tesi]) | ||
expect(html.search('meta[@name="citation_publication_date"]') | ||
.first['content']).to eq(doc[:released_metadata_at_dtsi].to_date.year.to_s) | ||
expect(html.search('meta[@name="citation_pdf_url"]').first['content']).to eq( | ||
final_submission_file_url(doc.final_submissions.first.first, host: 'http://test.host') | ||
) | ||
end | ||
end | ||
|
||
context 'when the final submission file is not a pdf' do | ||
let(:fake_solr_doc) { FakeSolrDocument.new(access_level: 'open_access', file_names: ['word_doc.docx']).doc } | ||
|
||
it 'renders all the meta tags' do | ||
expect(html.search('meta[@name="citation_title"]') | ||
.first['content']).to eq(doc[:title_tesi]) | ||
expect(html.search('meta[@name="citation_author"]') | ||
.first['content']).to eq(doc[:author_name_tesi]) | ||
expect(html.search('meta[@name="citation_publication_date"]') | ||
.first['content']).to eq(doc[:released_metadata_at_dtsi].to_date.year.to_s) | ||
expect(html.search('meta[@name="citation_pdf_url"]')).to be_empty | ||
end | ||
end | ||
|
||
context 'when the final submission is not open_access' do | ||
let(:fake_solr_doc) { FakeSolrDocument.new(access_level: 'restricted_to_institution').doc } | ||
|
||
it 'renders all the meta tags' do | ||
expect(html.search('meta[@name="citation_title"]') | ||
.first['content']).to eq(doc[:title_tesi]) | ||
expect(html.search('meta[@name="citation_author"]') | ||
.first['content']).to eq(doc[:author_name_tesi]) | ||
expect(html.search('meta[@name="citation_publication_date"]') | ||
.first['content']).to eq(doc[:released_metadata_at_dtsi].to_date.year.to_s) | ||
expect(html.search('meta[@name="citation_pdf_url"]')).to be_empty | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters