BUGFIX: more robust onebox implementation

This commit is contained in:
Sam 2014-05-28 17:15:10 +10:00
parent d9f51961c4
commit 0bc3525b10
2 changed files with 30 additions and 13 deletions

View File

@ -35,12 +35,20 @@ module Oneboxer
if c = Rails.cache.read(onebox_cache_key(url)) if c = Rails.cache.read(onebox_cache_key(url))
c[:onebox] c[:onebox]
end end
rescue => e
invalidate(url)
Rails.logger.warn("invalid cached onebox for #{url} #{e}")
""
end end
def self.cached_preview(url) def self.cached_preview(url)
if c = Rails.cache.read(onebox_cache_key(url)) if c = Rails.cache.read(onebox_cache_key(url))
c[:preview] c[:preview]
end end
rescue => e
invalidate(url)
Rails.logger.warn("invalid cached preview for #{url} #{e}")
""
end end
def self.oneboxer_exists_for_url?(url) def self.oneboxer_exists_for_url?(url)
@ -110,21 +118,20 @@ module Oneboxer
end end
def self.onebox_raw(url) def self.onebox_raw(url)
Rails.cache.fetch(onebox_cache_key(url)){ Rails.cache.fetch(onebox_cache_key(url), expires_in: 1.day){
begin # This might be able to move to whenever the SiteSetting changes?
Oneboxer.add_discourse_whitelists
# This might be able to move to whenever the SiteSetting changes? r = Onebox.preview(url, cache: {})
Oneboxer.add_discourse_whitelists {
onebox: r.to_s,
r = Onebox.preview(url, cache: {}) preview: r.try(:placeholder_html).to_s
{ }
onebox: r.to_s,
preview: r.try(:placeholder_html).to_s
}
rescue => e
Discourse.handle_exception(e, url: url)
end
} }
rescue => e
Discourse.handle_exception(e, url: url)
# return a blank hash, so rest of the code works
{preview: "", onebox: ""}
end end
end end

View File

@ -0,0 +1,10 @@
require 'spec_helper'
require_dependency 'oneboxer'
describe Oneboxer do
it "returns blank string for an invalid onebox" do
Oneboxer.preview("http://boom.com").should == ""
Oneboxer.onebox("http://boom.com").should == ""
end
end