Fix redundant HTTP request in FetchLinkCardService (#6002)

This commit is contained in:
Eugen Rochko 2017-12-13 12:15:28 +01:00 committed by GitHub
parent 20a6584d2d
commit a8deb6648b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 32 deletions

View file

@ -2,13 +2,26 @@
class ProviderDiscovery < OEmbed::ProviderDiscovery
class << self
def get(url, **options)
provider = discover_provider(url, options)
options.delete(:html)
provider.get(url, options)
end
def discover_provider(url, **options)
res = Request.new(:get, url).perform
format = options[:format]
raise OEmbed::NotFound, url if res.code != 200 || res.mime_type != 'text/html'
if options[:html]
html = Nokogiri::HTML(options[:html])
else
res = Request.new(:get, url).perform
html = Nokogiri::HTML(res.to_s)
raise OEmbed::NotFound, url if res.code != 200 || res.mime_type != 'text/html'
html = Nokogiri::HTML(res.to_s)
end
if format.nil? || format == :json
provider_endpoint ||= html.at_xpath('//link[@type="application/json+oembed"]')&.attribute('href')&.value