Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add view components for rendering google scholar meta tags for scraping. #104

Merged
merged 4 commits into from
Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,8 @@ GEM
rubocop-rspec (~> 2)
scss_lint (~> 0.55)
nio4r (2.5.8)
nokogiri (1.13.7-arm64-darwin)
racc (~> 1.4)
nokogiri (1.13.7-x86_64-darwin)
racc (~> 1.4)
nokogiri (1.13.7-x86_64-linux)
Expand Down Expand Up @@ -435,6 +437,7 @@ GEM
zeitwerk (2.5.4)

PLATFORMS
arm64-darwin-21
x86_64-darwin-20
x86_64-darwin-21
x86_64-linux
Expand Down
3 changes: 3 additions & 0 deletions app/components/custom_document_component.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
itemtype: @document.itemtype,
class: classes.flatten.join(' ') do %>
<%= header %>
<% if show? %>
<%= render GoogleScholarMetadataComponent.new(document: @document) %>
<% end %>
<% if body.present? %>
<%= body %>
<% else %>
Expand Down
7 changes: 7 additions & 0 deletions app/components/google_scholar_metadata_component.html.erb
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 %>
24 changes: 24 additions & 0 deletions app/components/google_scholar_metadata_component.rb
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
61 changes: 61 additions & 0 deletions spec/component/google_scholar_metadata_component_spec.rb
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
7 changes: 7 additions & 0 deletions spec/integration/home_page_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@
it 'shows Explore button to search' do
expect(page).to have_button 'Explore'
end

it 'does not display Google Scholar meta tags' do
expect(page).not_to have_css 'meta[@name="citation_title"]'
expect(page).not_to have_css 'meta[@name="citation_author"]'
expect(page).not_to have_css 'meta[@name="citation_publication_date"]'
expect(page).not_to have_css 'meta[@name="citation_pdf_url"]'
end
end

context 'when a user is logged in' do
Expand Down
3 changes: 3 additions & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
require 'capybara/rails'
# Add additional requires below this line. Rails is not loaded until this point!
require 'devise'
require 'view_component/test_helpers'

# Requires supporting ruby files with custom matchers and macros, etc, in
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
Expand Down Expand Up @@ -64,6 +65,8 @@
config.include Devise::Test::ControllerHelpers, type: :controller
config.include Devise::Test::IntegrationHelpers, type: :feature

config.include ViewComponent::TestHelpers, type: :component

# Filter lines from Rails gems in backtraces.
config.filter_rails_from_backtrace!
# arbitrary gems may also be filtered via:
Expand Down