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.

50 lines
1.5 KiB

  1. # frozen_string_literal: true
  2. class FetchAtomService < BaseService
  3. def call(url)
  4. response = http_client.head(url)
  5. Rails.logger.debug "Remote status HEAD request returned code #{response.code}"
  6. response = http_client.get(url) if response.code == 405
  7. Rails.logger.debug "Remote status GET request returned code #{response.code}"
  8. return nil if response.code != 200
  9. return [url, fetch(url)] if response.mime_type == 'application/atom+xml'
  10. return process_headers(url, response) unless response['Link'].blank?
  11. process_html(fetch(url))
  12. rescue OpenSSL::SSL::SSLError => e
  13. Rails.logger.debug "SSL error: #{e}"
  14. end
  15. private
  16. def process_html(body)
  17. Rails.logger.debug 'Processing HTML'
  18. page = Nokogiri::HTML(body)
  19. alternate_link = page.xpath('//link[@rel="alternate"]').find { |link| link['type'] == 'application/atom+xml' }
  20. return nil if alternate_link.nil?
  21. [alternate_link['href'], fetch(alternate_link['href'])]
  22. end
  23. def process_headers(url, response)
  24. Rails.logger.debug 'Processing link header'
  25. link_header = LinkHeader.parse(response['Link'].is_a?(Array) ? response['Link'].first : response['Link'])
  26. alternate_link = link_header.find_link(%w(rel alternate), %w(type application/atom+xml))
  27. return process_html(fetch(url)) if alternate_link.nil?
  28. [alternate_link.href, fetch(alternate_link.href)]
  29. end
  30. def fetch(url)
  31. http_client.get(url).to_s
  32. end
  33. def http_client
  34. HTTP.timeout(:per_operation, write: 20, connect: 20, read: 50).follow
  35. end
  36. end