Looking for changes on website content https://projet-alice-demo.herokuapp.com/diffs/
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.

93 lines
2.0KB

  1. class Site < ApplicationRecord
  2. attribute :targets, :targets
  3. belongs_to :group, optional: true
  4. belongs_to :template, optional: true
  5. has_many :targets
  6. validates :url, presence: true
  7. def self.grab(url)
  8. response = HTTParty.get url, timeout: 10.seconds
  9. raise "Receive #{response.code}" unless response.success?
  10. response
  11. end
  12. def self.html(url)
  13. response = self.grab url
  14. content_type = response.content_type
  15. raise "Expecting #{'text/html'.colorize :yellow}, got #{content_type.colorize :yellow}" unless content_type == 'text/html'
  16. content = response.body
  17. Nokogiri::HTML.parse content
  18. end
  19. def self.title(url)
  20. html = self.html url
  21. tag = html.at 'head title'
  22. tag&.text
  23. end
  24. def all_targets
  25. targets = self.targets
  26. group = self.group
  27. targets += groups.targets if group
  28. template = self.template
  29. targets = template.targets if template
  30. targets
  31. end
  32. def check
  33. self.checked_at = Time.now
  34. state = :no_changes
  35. error = nil
  36. begin
  37. reference = self.reference
  38. response = self.class.grab self.url
  39. content = response.body
  40. unless reference
  41. self.reference = content
  42. state = :new
  43. else
  44. self.content = content
  45. unchanged = true
  46. content_type = response.content_type
  47. case content_type
  48. when 'text/html'
  49. targets = self.targets
  50. if targets
  51. targets.each do |target|
  52. target_content = target.extract content
  53. target_reference = target.extract reference
  54. target_unchanged = target_content == target_reference
  55. unless target_unchanged
  56. unchanged = target_unchanged
  57. break
  58. end
  59. end
  60. else
  61. unchanged = content == reference
  62. end
  63. else
  64. unchanged = content == reference
  65. end
  66. unless unchanged
  67. self.changed_at = self.checked_at
  68. state = :changes
  69. end
  70. end
  71. self.last_error = nil
  72. rescue => e
  73. self.last_error = e.to_s
  74. error = e
  75. end
  76. self.save!
  77. raise error if error
  78. state
  79. end
  80. end