This repository has been archived by the owner on Feb 18, 2024. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This displays an rss feed for posts at blog.edwardloveall.com/rss. I used this for the specification: https://cyber.law.harvard.edu/rss/rss.html Of note, the post content is converted from markdown to html, including syntax highlighting, which seems to work in my limited testing of RSS readers.
- Loading branch information
1 parent
c1f2de1
commit fd280c6
Showing
6 changed files
with
139 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
xml.instruct! :xml, version: '1.0' | ||
|
||
xml.rss version: '2.0' do | ||
xml.channel do | ||
xml.title title | ||
xml.link root_url(subdomain: 'blog') + 'rss' | ||
xml.lastBuildDate @posts.first.created_at | ||
xml.language 'en-US' | ||
|
||
@posts.each do |post| | ||
xml.item do | ||
xml.title post.title | ||
xml.link post_url(post.slug, subdomain: 'blog') | ||
xml.description convert_markdown(post.body) | ||
xml.pubDate post.created_at | ||
xml.guid post.guid, isPermalink: false | ||
end | ||
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
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,84 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe 'Posts requests' do | ||
it 'returns an rss response' do | ||
create(:post) | ||
|
||
get feed_url | ||
|
||
expect(response.content_type).to eq(Mime::Type.lookup_by_extension(:rss)) | ||
end | ||
|
||
it 'has channel and meta attributes' do | ||
post = create(:post) | ||
|
||
get feed_url | ||
|
||
rss = xml[:rss] | ||
channel = rss[:channel] | ||
|
||
expect(rss[:version]).to eq('2.0') | ||
expect(channel[:title]).to eq('Edward Loveall') | ||
expect(channel[:link]).to eq(feed_url) | ||
expect(channel[:lastBuildDate]).to eq(post.created_at.to_s) | ||
expect(channel[:language]).to eq('en-US') | ||
end | ||
|
||
it 'has entry attributes' do | ||
create(:post) | ||
post = create(:post) | ||
post_body = MarkdownRenderer.to_html(post.body).html_safe | ||
|
||
get feed_url | ||
|
||
entry = xml[:rss][:channel][:item].first | ||
|
||
expect(entry[:title]).to eq(post.title) | ||
expect(entry[:link]).to eq(post_url(post.slug, subdomain: 'blog')) | ||
expect(entry[:description]).to eq(post_body) | ||
expect(entry[:pubDate]).to eq(post.created_at.to_s) | ||
expect(entry[:guid]).to eq(post.guid) | ||
end | ||
|
||
it 'reports the guid as a non-permalink' do | ||
create(:post) | ||
|
||
get feed_url | ||
|
||
xml = Nokogiri::XML(response.body) | ||
guid = xml.at('guid') | ||
|
||
expect(guid.attribute('isPermalink').value).to eq('false') | ||
end | ||
|
||
it 'returns a 304 when there are no new posts' do | ||
create(:post) | ||
get feed_url | ||
headers = { 'HTTP_IF_NONE_MATCH' => response.etag } | ||
|
||
get feed_url, {}, headers | ||
|
||
expect(response).to have_http_status(:not_modified) | ||
end | ||
|
||
it 'returns a 200 when there are new posts' do | ||
Timecop.travel(Date.yesterday) | ||
create(:post) | ||
get feed_url | ||
headers = { 'HTTP_IF_NONE_MATCH' => response.etag } | ||
Timecop.return | ||
create(:post) | ||
|
||
get feed_url, {}, headers | ||
|
||
expect(response).to have_http_status(:ok) | ||
end | ||
|
||
def xml | ||
Hash.from_xml(response.body).deep_symbolize_keys | ||
end | ||
|
||
def feed_url | ||
"#{root_url(subdomain: 'blog')}rss" | ||
end | ||
end |