闭社主体 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.

37 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. raise OEmbed::NotFound, url if provider_endpoint.nil?
  19. begin
  20. provider_endpoint = Addressable::URI.parse(provider_endpoint)
  21. provider_endpoint.query = nil
  22. provider_endpoint = provider_endpoint.to_s
  23. rescue Addressable::URI::InvalidURIError
  24. raise OEmbed::NotFound, url
  25. end
  26. OEmbed::Provider.new(provider_endpoint, format)
  27. end
  28. end
  29. end