The code powering m.abunchtell.com https://m.abunchtell.com
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

47 строки
1.3 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. return nil if response.code != 200
  6. if response.mime_type == 'application/atom+xml'
  7. return [url, fetch(url)]
  8. elsif !response['Link'].blank?
  9. return process_headers(url, response)
  10. else
  11. return process_html(fetch(url))
  12. end
  13. end
  14. private
  15. def process_html(body)
  16. Rails.logger.debug 'Processing HTML'
  17. page = Nokogiri::HTML(body)
  18. alternate_link = page.xpath('//link[@rel="alternate"]').find { |link| link['type'] == 'application/atom+xml' }
  19. return nil if alternate_link.nil?
  20. return [alternate_link['href'], fetch(alternate_link['href'])]
  21. end
  22. def process_headers(url, response)
  23. Rails.logger.debug 'Processing link header'
  24. link_header = LinkHeader.parse(response['Link'].is_a?(Array) ? response['Link'].first : response['Link'])
  25. alternate_link = link_header.find_link(['rel', 'alternate'], ['type', 'application/atom+xml'])
  26. return process_html(fetch(url)) if alternate_link.nil?
  27. return [alternate_link.href, fetch(alternate_link.href)]
  28. end
  29. def fetch(url)
  30. http_client.get(url).to_s
  31. end
  32. def http_client
  33. HTTP.timeout(:per_operation, write: 20, connect: 20, read: 50).follow
  34. end
  35. end