Class: RSScache
- Inherits:
-
Object
- Object
- RSScache
- Defined in:
- lib/rsscache.rb
Instance Attribute Summary collapse
-
#err_report ⇒ Object
readonly
Returns the value of attribute err_report.
Instance Method Summary collapse
-
#import(raw_s) ⇒ Object
Import a list of URLs into the Dynarex document.
-
#initialize(rsslist, feedsfilepath = '.', debug: true) ⇒ RSScache
constructor
A new instance of RSScache.
- #open_dynarex(x) ⇒ Object
-
#refresh ⇒ Object
(also: #update)
refresh each RSS feed.
- #save_dynarex ⇒ Object
-
#updates?(feed) ⇒ Boolean
checks for any updates and saves the latest RSS file to the cache if there is.
Constructor Details
#initialize(rsslist, feedsfilepath = '.', debug: true) ⇒ RSScache
Returns a new instance of RSScache.
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/rsscache.rb', line 16 def initialize(rsslist, feedsfilepath='.', debug: true) @dx = open_dynarex(rsslist) @rsslist, @feedsfilepath = rsslist, feedsfilepath FileUtils.mkdir_p feedsfilepath @err_report = [] @debug = debug end |
Instance Attribute Details
#err_report ⇒ Object (readonly)
Returns the value of attribute err_report.
14 15 16 |
# File 'lib/rsscache.rb', line 14 def err_report @err_report end |
Instance Method Details
#import(raw_s) ⇒ Object
Import a list of URLs into the Dynarex document. URLs which already exist are ignored.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/rsscache.rb', line 30 def import(raw_s) s, _ = RXFHelper.read(raw_s) s.strip.lines.each do |raw_url| url = raw_url.chomp puts 'url : ' + url.inspect if @debug r = @dx.find_by_url url.chomp if r then puts 'exists' if @debug else puts 'new URL found' if @debug @dx.create url: url end end save_dynarex() end |
#open_dynarex(x) ⇒ Object
99 100 101 102 103 104 105 106 107 |
# File 'lib/rsscache.rb', line 99 def open_dynarex(x) if x.lines.length == 1 and File.exists?(x) and \ File.extname(x) == '.txt' then Dynarex.new.import x else Dynarex.new x end end |
#refresh ⇒ Object Also known as: update
refresh each RSS feed
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/rsscache.rb', line 54 def refresh @err_report = [] puts '@dx.to_xml' + @dx.to_xml(pretty: true) if @debug @dx.all.each do |feed| puts 'feed:' + feed.inspect if @debug if feed.next_refresh.empty? or \ Time.now >= Time.parse(feed.next_refresh) then any_new_items = updates? feed feed.refresh_rate = if feed.refresh_rate.empty? then 10 else if Time.now > Time.parse(feed.next_refresh) + \ feed.refresh_rate.to_i and not any_new_items then feed.refresh_rate.to_i + 10 end end feed.next_refresh = Time.now + feed.refresh_rate.to_i * 60 else feed.refresh_rate = feed.refresh_rate.to_i - 10 if feed.refresh_rate.to_i > 10 end end puts '@dx: ' + @dx.to_xml(pretty: true) if @debug save_dynarex() end |
#save_dynarex ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/rsscache.rb', line 109 def save_dynarex() if @rsslist.lines.length == 1 and File.exists?(@rsslist) if File.extname(@rsslist) == '.txt'then File.write @rsslist, @dx.to_s else @dx.save end end end |
#updates?(feed) ⇒ Boolean
checks for any updates and saves the latest RSS file to
the cache if there is
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/rsscache.rb', line 129 def updates?(feed) if @debug then puts 'inside updates?' puts 'feed: ' + feed.inspect end # fetch the feeds from the web buffer, code = fetch(feed.url) if code == 200 then rss = SimpleRSS.parse(buffer) else @err_report << [feed.url, code] return false end if feed.filename.empty? then filename = feed.url[6..-1].gsub(/\W+/,'').\ reverse.slice(0,40).reverse.downcase + '.xml' feed.filename = filename end rssfile = File.join(@feedsfilepath, feed.filename) if File.exists? rssfile then rss_cache = SimpleRSS.parse File.read(rssfile) new_rss_items = rss.items - rss_cache.items (File.write rssfile, rss.source; return true) if new_rss_items.any? else File.write rssfile, rss.source feed.title = rss.title if feed.title.empty? return true end return false end |