Guest User

Untitled

a guest
Jul 20th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.59 KB | None | 0 0
  1. class Notification < ActiveRecord::Base
  2.  
  3. validates_presence_of :feed
  4.  
  5. attr_accessor :doc
  6.  
  7. after_create :create_posts
  8.  
  9. def posts
  10. entries.map { |entry| entry_format(entry) } if entries
  11. end
  12.  
  13. def create_posts
  14. Post.create(posts) if posts
  15. end
  16.  
  17. private
  18.  
  19. def parse
  20. @doc ||= Nokogiri::HTML(self.feed)
  21. end
  22.  
  23. def entries
  24. parse.css('entry') unless dummy?
  25. end
  26.  
  27. def dummy?
  28. parse.css('feed').css('id').first.text.include?('dummy')
  29. end
  30.  
  31. def entry_format(entry)
  32. { url: entry.css('id').text, title: entry.css('title').text, body: entry.css('content').text }
  33. end
  34.  
  35.  
  36. end
Add Comment
Please, Sign In to add comment