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.

54 lines
1.5 KiB

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