Skip to content

Commit

Permalink
Add an RSS feed for works, to be used by discovery for indexing (#418)
Browse files Browse the repository at this point in the history
* Add an RSS feed for works, to be used by discovery for indexing

* Only approved works should appear in RSS feed
  • Loading branch information
bess authored Sep 19, 2022
1 parent 61b7746 commit d825e22
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 0 deletions.
4 changes: 4 additions & 0 deletions app/controllers/works_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ class WorksController < ApplicationController

def index
@works = Work.all
respond_to do |format|
format.html
format.rss { render layout: false }
end
end

# Renders the "step 0" information page before creating a new dataset
Expand Down
1 change: 1 addition & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.11.3/css/jquery.dataTables.min.css"/>
<script type="text/javascript" src="https://cdn.datatables.net/1.11.3/js/jquery.dataTables.min.js"></script>
<%= favicon_link_tag asset_path('favicon.png') %>
<%= auto_discovery_link_tag :rss, works_url(:format => :rss) %>
</head>

<body>
Expand Down
19 changes: 19 additions & 0 deletions app/views/works/index.rss.builder
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true
xml.instruct! :xml, version: "1.0"
xml.rss version: "2.0" do
xml.channel do
xml.title "Princeton Data Commons RSS Feed"
xml.description "Princeton Data Commons RSS Feed of approved submissions"
xml.link root_url

@works.each do |work|
# Only include approved works in the RSS feed
next unless work.state == "approved"
xml.item do
xml.title work.title
xml.url datacite_work_url(work)
xml.date_changed work.updated_at
end
end
end
end
6 changes: 6 additions & 0 deletions spec/controllers/works_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@
expect(response).to render_template("index")
end

it "has an rss feed" do
sign_in user
get :index, format: "rss"
expect(response.content_type).to eq "application/rss+xml; charset=utf-8"
end

it "renders the new submission wizard' step 0" do
sign_in user
get :new, params: { wizard: true }
Expand Down
37 changes: 37 additions & 0 deletions spec/system/rss_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# frozen_string_literal: true
require "rails_helper"

RSpec.describe "RSS feed of approved works, for harvesting and indexing", type: :system, mock_ezid_api: true do
let(:work1) { FactoryBot.create(:draft_work) }
let(:work2) { FactoryBot.create(:draft_work) }
let(:work3) { FactoryBot.create(:draft_work) }
let(:admin) { FactoryBot.create(:super_admin_user) }
let(:user) { FactoryBot.create(:princeton_submitter) }

before do
stub_datacite(host: "api.datacite.org", body: datacite_register_body(prefix: "10.34770"))
allow(work1).to receive(:publish_doi).and_return(true)
allow(work2).to receive(:publish_doi).and_return(true)

# Works 1 & 2 are approved, so they should show up in the RSS feed
work1.complete_submission!(admin)
work1.approve!(admin)

work2.complete_submission!(admin)
work2.approve!(admin)

# Ensure work3 exists before running the tests, but leave it in draft state.
# It should NOT appear in the RSS feed.
work3
end

it "provides a list of approved works, with links to their datacite records" do
sign_in user
visit "/works.rss"
doc = Nokogiri::XML(page.body)
expect(doc.xpath("//item").size).to eq 2
urls = doc.xpath("//item/url/text()").map(&:to_s)
expect(urls.include?(datacite_work_url(work1))).to eq true
expect(urls.include?(datacite_work_url(work2))).to eq true
end
end

0 comments on commit d825e22

Please sign in to comment.