Class: Feedbook::Listener
- Inherits:
-
Object
- Object
- Feedbook::Listener
- Defined in:
- lib/feedbook/listener.rb
Class Method Summary collapse
-
.load_feeds_archive(path) ⇒ Array
Load feeds from serialized YAML file.
-
.observe_and_notify(feed) ⇒ NilClass
Single run of loop for listening for changes in RSS feeds and notifies about updates.
-
.save_feeds_archive(path, feeds) ⇒ NilClass
Saves feeds into serialized YAML file.
-
.start(path, plugins_path) ⇒ Object
Starts listening on feeds and notifies if there is new post.
-
.start_offline(path, archive_path, plugins_path) ⇒ Object
Starts listening on feeds and notifies if there is new post (offline mode).
Class Method Details
.load_feeds_archive(path) ⇒ Array
Load feeds from serialized YAML file.
72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/feedbook/listener.rb', line 72 def self.load_feeds_archive(path) print 'Reading feeds from file... ' if File.exist? path puts 'completed.' YAML::load(File.read(path)) else puts 'canceled. File does not exist.' [] end end |
.observe_and_notify(feed) ⇒ NilClass
Single run of loop for listening for changes in RSS feeds and notifies about updates
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/feedbook/listener.rb', line 104 def self.observe_and_notify(feed) new_posts = feed[:feed].fetch difference = Comparers::PostsComparer.get_new_posts(feed[:posts], new_posts) if difference.empty? puts 'No new posts found.' else puts "#{difference.size} new posts found." print 'Started sending notifications... ' end difference.each do |post| feed[:feed].notifications.each do |notification| notification.notify(post) end end unless difference.empty? puts 'completed.' end feed[:posts] = new_posts feed end |
.save_feeds_archive(path, feeds) ⇒ NilClass
Saves feeds into serialized YAML file
89 90 91 92 93 94 95 96 97 |
# File 'lib/feedbook/listener.rb', line 89 def self.save_feeds_archive(path, feeds) print 'Saving feeds to file... ' File.open(path, 'w') do |f| f.write YAML::dump(feeds) end puts 'completed.' end |
.start(path, plugins_path) ⇒ Object
Starts listening on feeds and notifies if there is new post.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/feedbook/listener.rb', line 15 def self.start(path, plugins_path) handle_exceptions do feeds, configuration = initialize_listener(path, plugins_path) print 'Fetching feeds for the first use... ' observed_feeds = feeds.map do |feed| { feed: feed, posts: feed.fetch } end puts 'completed.' puts 'Listener started...' every configuration.interval do puts 'Fetching feeds...' observed_feeds.each do |feed| observe_and_notify(feed) end end end end |
.start_offline(path, archive_path, plugins_path) ⇒ Object
Starts listening on feeds and notifies if there is new post (offline mode).
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/feedbook/listener.rb', line 42 def self.start_offline(path, archive_path, plugins_path) handle_exceptions do feeds, configuration = initialize_listener(path, plugins_path) observed_feeds = load_feeds_archive(archive_path) if observed_feeds.blank? print 'Fetching feeds for the first use... ' observed_feeds = feeds.map do |feed| { feed: feed, posts: feed.fetch } end puts 'completed.' end puts 'Fetching feeds...' observed_feeds.each do |feed| observe_and_notify(feed) end save_feeds_archive(archive_path, observed_feeds) end end |