FIX: skip invalid URLs when checking for audio/video in search blurbs

Fixes 500 errors on search queries introduced in 580a4a8
This commit is contained in:
Penar Musaraj 2019-11-06 10:32:15 -05:00
parent b869ef8a76
commit 0dfc594784
2 changed files with 16 additions and 7 deletions

View File

@ -78,14 +78,16 @@ class Search
urls = Set.new urls = Set.new
cooked.scan(URI.regexp(%w{http https})) { urls << $& } cooked.scan(URI.regexp(%w{http https})) { urls << $& }
urls.each do |url| urls.each do |url|
begin
case File.extname(URI(url).path || "") case File.extname(URI(url).path || "")
when Oneboxer::VIDEO_REGEX when Oneboxer::VIDEO_REGEX
cooked.gsub!(url, I18n.t("search.video")) cooked.gsub!(url, I18n.t("search.video"))
when Oneboxer::AUDIO_REGEX when Oneboxer::AUDIO_REGEX
cooked.gsub!(url, I18n.t("search.audio")) cooked.gsub!(url, I18n.t("search.audio"))
end end
rescue URI::InvalidURIError
end
end end
if term if term

View File

@ -28,7 +28,7 @@ describe Search do
expect(result).to eq("link to an external page: https://google.com/?u=bar link to an audio file: #{I18n.t("search.audio")} link to a video file: #{I18n.t("search.video")}") expect(result).to eq("link to an external page: https://google.com/?u=bar link to an audio file: #{I18n.t("search.audio")} link to a video file: #{I18n.t("search.video")}")
end end
it "strips URLs correctly when blurb is" do it "strips URLs correctly when blurb is longer than limit" do
cooked = <<~RAW cooked = <<~RAW
Here goes a test cooked with enough characters to hit the blurb limit. Here goes a test cooked with enough characters to hit the blurb limit.
@ -41,6 +41,13 @@ describe Search do
expect(result).to eq("Here goes a test cooked with enough characters to hit the blurb limit. Something is very interesting about this audio file. #{I18n.t("search.audio")}") expect(result).to eq("Here goes a test cooked with enough characters to hit the blurb limit. Something is very interesting about this audio file. #{I18n.t("search.audio")}")
end end
it "does not fail on bad URLs" do
cooked = <<~RAW
invalid URL: http:error] should not trip up blurb generation.
RAW
result = Search::GroupedSearchResults.blurb_for(cooked)
expect(result).to eq("invalid URL: http:error] should not trip up blurb generation.")
end
end end
end end