Skip to content

Commit 12e5184

Browse files
committed
Support feeds with stories with scheme-less urls
Example is https://blog.golang.org/feed.atom. Urls in this feed are referenced to as scheme-less urls (//blog.golang.org/context). This commit adds normalization of story urls using the feed url. If a story url doesn't have a scheme it will use the scheme of feed's url.
1 parent da4b5a2 commit 12e5184

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

app/repositories/story_repository.rb

+13
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
class StoryRepository
55
def self.add(entry, feed)
6+
entry.url = normalize_url(entry.url, feed.url)
7+
68
Story.create(feed: feed,
79
title: entry.title,
810
permalink: entry.url,
@@ -109,6 +111,17 @@ def self.expand_absolute_urls(content, base_url)
109111
doc.to_html
110112
end
111113

114+
def self.normalize_url(url, base_url)
115+
uri = URI.parse(url)
116+
117+
unless uri.scheme
118+
base_uri = URI.parse(base_url)
119+
uri.scheme = base_uri.scheme || 'http'
120+
end
121+
122+
uri.to_s
123+
end
124+
112125
def self.samples
113126
[
114127
SampleStory.new("Darin' Fireballs", "Why you should trade your firstborn for a Retina iPad"),

spec/repositories/story_repository_spec.rb

+36
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,20 @@
33
app_require "repositories/story_repository"
44

55
describe StoryRepository do
6+
describe '.add' do
7+
let(:feed) { double(url: 'http://blog.golang.org/feed.atom') }
8+
before do
9+
Story.stub(:create)
10+
end
11+
12+
it 'normalizes story urls' do
13+
entry = double(url: '//blog.golang.org/context', content: '').as_null_object
14+
StoryRepository.should receive(:normalize_url).with(entry.url, feed.url)
15+
16+
StoryRepository.add(entry, feed)
17+
end
18+
end
19+
620
describe ".expand_absolute_urls" do
721
it "preserves existing absolute urls" do
822
content = '<a href="http://foo">bar</a>'
@@ -98,4 +112,26 @@
98112
end
99113
end
100114
end
115+
116+
describe ".normalize_url" do
117+
it "resolves scheme-less urls" do
118+
%w{http https}.each do |scheme|
119+
feed_url = "#{scheme}://blog.golang.org/feed.atom"
120+
121+
url = StoryRepository.normalize_url("//blog.golang.org/context", feed_url)
122+
url.should eq "#{scheme}://blog.golang.org/context"
123+
end
124+
end
125+
126+
it "leaves urls with a scheme intact" do
127+
input = 'http://blog.golang.org/context'
128+
normalized_url = StoryRepository.normalize_url(input, 'http://blog.golang.org/feed.atom')
129+
normalized_url.should eq(input)
130+
end
131+
132+
it "falls back to http if the base_url is also sheme less" do
133+
url = StoryRepository.normalize_url("//blog.golang.org/context", "//blog.golang.org/feed.atom")
134+
url.should eq 'http://blog.golang.org/context'
135+
end
136+
end
101137
end

0 commit comments

Comments
 (0)