闭社主体 forked from https://github.com/tootsuite/mastodon
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

36 lines
1.1 KiB

  1. # frozen_string_literal: true
  2. class ProviderDiscovery < OEmbed::ProviderDiscovery
  3. extend HttpHelper
  4. class << self
  5. def discover_provider(url, options = {})
  6. res = http_client.get(url)
  7. format = options[:format]
  8. raise OEmbed::NotFound, url if res.code != 200 || res.mime_type != 'text/html'
  9. html = Nokogiri::HTML(res.to_s)
  10. if format.nil? || format == :json
  11. provider_endpoint ||= html.at_xpath('//link[@type="application/json+oembed"]')&.attribute('href')&.value
  12. format ||= :json if provider_endpoint
  13. end
  14. if format.nil? || format == :xml
  15. provider_endpoint ||= html.at_xpath('//link[@type="application/xml+oembed"]')&.attribute('href')&.value
  16. format ||= :xml if provider_endpoint
  17. end
  18. begin
  19. provider_endpoint = Addressable::URI.parse(provider_endpoint)
  20. provider_endpoint.query = nil
  21. provider_endpoint = provider_endpoint.to_s
  22. rescue Addressable::URI::InvalidURIError
  23. raise OEmbed::NotFound, url
  24. end
  25. OEmbed::Provider.new(provider_endpoint, format || OEmbed::Formatter.default)
  26. end
  27. end
  28. end