rsscache 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 93d9d21a9a587043e1db22b3f64164308c755eee
4
- data.tar.gz: 5e81b40fe30dc757fd46797d03bf670ba553206d
3
+ metadata.gz: 13d621b5f6fada69a9b0d44f228f5a109114ae5b
4
+ data.tar.gz: 2f3b4b9553374db248914d72b8f6e2e612559c9b
5
5
  SHA512:
6
- metadata.gz: cfd87ca7214dde36963ee7a2bede8a41e1bf164a67159c688f93a34e83e80138a53f1d3a2eead31e1436bd9f4e12935333960e23a666982d1619743f077c8874
7
- data.tar.gz: 7ac379f1d015f2a068616d4a0abd576124937c40cdf4e071bf7439580884ad384bcb428f72743f2e03f3eec6849291720865d439a80bccd90b836fa75e1a9f84
6
+ metadata.gz: a38232230077fb48b0eb518883f90e23d48410e0603c4af46db6401558f73c9093239100525f3812733f1cdd19fcba1be9a3892fbf25b48fe92f90c79ee381f9
7
+ data.tar.gz: 54febc9ada04ee92d908fb02110187b8bd70053b4aa0a7e96e8b190d54a0c487d7a8934fe5ad28fe83721eb63e8a5f64bd832773ea9be22fef5e15b384783322
Binary file
data.tar.gz.sig CHANGED
Binary file
@@ -11,13 +11,15 @@ require 'timeout'
11
11
 
12
12
  class RSScache
13
13
 
14
- attr_reader :err_report
14
+ attr_reader :err_report, :dx
15
15
 
16
- def initialize(rsslist, feedsfilepath='.', debug: true)
16
+ def initialize(rsslist=nil, filepath: '.', debug: true)
17
17
 
18
+ rsslist ||= File.join(filepath, 'rsscache.xml')
18
19
  @dx = open_dynarex(rsslist)
19
- @rsslist, @feedsfilepath = rsslist, feedsfilepath
20
- FileUtils.mkdir_p feedsfilepath
20
+ @filepath = filepath
21
+ @cache_filepath = File.join(filepath, 'rsscache')
22
+ FileUtils.mkdir_p @cache_filepath
21
23
 
22
24
  @err_report = []
23
25
  @debug = debug
@@ -46,7 +48,7 @@ class RSScache
46
48
 
47
49
  end
48
50
 
49
- save_dynarex()
51
+ save()
50
52
  end
51
53
 
52
54
  # refresh each RSS feed
@@ -89,43 +91,77 @@ class RSScache
89
91
 
90
92
  end
91
93
  end
94
+
92
95
  puts '@dx: ' + @dx.to_xml(pretty: true) if @debug
93
- save_dynarex()
96
+ save()
94
97
 
95
98
  end
96
99
 
97
100
  alias update refresh
98
101
 
99
- def open_dynarex(x)
102
+ def save()
100
103
 
101
- if x.lines.length == 1 and File.exists?(x) and \
102
- File.extname(x) == '.txt' then
103
- Dynarex.new.import x
104
- else
105
- Dynarex.new x
106
- end
107
- end
104
+ @dx.save File.join(@filepath, 'rsscache.xml')
105
+ File.write File.join(@filepath, 'rsscache.txt'), @dx.to_s
108
106
 
109
- def save_dynarex()
110
-
111
- if @rsslist.lines.length == 1 and File.exists?(@rsslist)
107
+ end
112
108
 
113
- if File.extname(@rsslist) == '.txt'then
109
+
110
+ private
111
+
112
+ def raw_doc(s)
113
+
114
+ heading = '<?dynarex schema="rsscache[title]/feed(uid, title, ' +
115
+ 'url, refresh_rate, next_refresh, filename)"?>'
116
+
117
+ raw_dx=<<EOF
118
+ #{heading}
119
+ title: RSS Feeds to be cached
114
120
 
115
- File.write @rsslist, @dx.to_s
121
+ --+
116
122
 
117
- else
123
+ #{s.strip.lines.map {|x| 'url: ' + x }.join }
124
+ EOF
125
+
126
+ end
127
+
128
+ def fetch(url, timeout: 2)
118
129
 
119
- @dx.save
130
+ puts 'inside fetch: url: ' + url.inspect if @debug
131
+
132
+ begin
133
+ Timeout::timeout(timeout){
120
134
 
121
- end
135
+ buffer = open(url).read.force_encoding("utf-8")
136
+ return [buffer, 200]
137
+ }
138
+ rescue Timeout::Error => e
139
+ ['connection timed out', 408]
140
+ rescue OpenURI::HTTPError => e
141
+ ['400 bad request', 400]
142
+ end
143
+
144
+ end
145
+
146
+ def open_dynarex(raw_s)
122
147
 
148
+ s, _ = RXFHelper.read(raw_s)
149
+ puts 'inside open_dynarex s: ' + s.inspect if @debug
150
+
151
+ case s
152
+ when /^<?dynarex/
153
+ Dynarex.new.import s
154
+ when /^</
155
+ Dynarex.new s
156
+ else
157
+ Dynarex.new.import raw_doc(s)
123
158
  end
159
+
124
160
  end
125
161
 
126
- # checks for any updates and saves the latest RSS file to
127
- # the cache if there is
128
- #
162
+ # checks for any updates and save the
163
+ # latest RSS file to the cache if there are updates
164
+ #
129
165
  def updates?(feed)
130
166
 
131
167
  if @debug then
@@ -134,10 +170,22 @@ class RSScache
134
170
  end
135
171
 
136
172
  # fetch the feeds from the web
137
- buffer, code = fetch(feed.url)
173
+ begin
174
+ buffer, code = fetch(feed.url)
175
+ rescue
176
+ puts 'RSScache::updates?: fetch() warning for feed ' + feed.url \
177
+ + ' ' + ($!).inspect
178
+ return
179
+ end
138
180
 
139
181
  if code == 200 then
140
- rss = SimpleRSS.parse(buffer)
182
+ begin
183
+ rss = SimpleRSS.parse(buffer)
184
+ rescue
185
+ puts 'RSScache::updates?: err: 100 SimpleRSS warning for feed ' \
186
+ + feed.url + ' ' + ($!).inspect
187
+ return
188
+ end
141
189
  else
142
190
  @err_report << [feed.url, code]
143
191
  return false
@@ -151,11 +199,18 @@ class RSScache
151
199
 
152
200
  end
153
201
 
154
- rssfile = File.join(@feedsfilepath, feed.filename)
202
+ rssfile = File.join(@cache_filepath, feed.filename)
155
203
 
156
204
  if File.exists? rssfile then
157
205
 
158
- rss_cache = SimpleRSS.parse File.read(rssfile)
206
+ begin
207
+ rss_cache = SimpleRSS.parse File.read(rssfile)
208
+ rescue
209
+ puts 'RSScache::updates?: err: 200 SimpleRSS warning for feed ' \
210
+ + feed.url + ' ' + ($!).inspect
211
+ FileUtils.rm rssfile
212
+ return false
213
+ end
159
214
  new_rss_items = rss.items - rss_cache.items
160
215
  (File.write rssfile, rss.source; return true) if new_rss_items.any?
161
216
 
@@ -169,24 +224,7 @@ class RSScache
169
224
  end
170
225
 
171
226
  return false
172
- end
173
-
174
- private
227
+ end
175
228
 
176
- def fetch(url, timeout: 2)
177
-
178
- begin
179
- Timeout::timeout(timeout){
180
-
181
- buffer = open(url).read
182
- return [buffer, 200]
183
- }
184
- rescue Timeout::Error => e
185
- ['connection timed out', 408]
186
- rescue OpenURI::HTTPError => e
187
- ['400 bad request', 400]
188
- end
189
-
190
- end
191
229
 
192
230
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rsscache
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Robertson
metadata.gz.sig CHANGED
@@ -1,3 +1,3 @@
1
- m.RpXD���F��=աkY�/�@�*�鷗-��'$��̹A�7�6ɚwCdb�/�{-�M���H��Zb*!Q[ľߺ:]�p��
2
- �,><�QV-�th4
3
- whu��Vj4�b-КQ�-f�wLZ ��+��/�/!�!��b#Kg��9�E�!E���e��]�bg$҆NT��vQMB���36:�8ƌ��:���2�� ���0ڈ���З.�Z JPS/$zI���~��GI>����M�\s )�h�����)�G
1
+ �XTa��
2
+ Bϴڨ�r�Sջ�BjM�+Z<SezH0*%cb���n�����_Y�2�K�M�֭� ����*��
3
+ '���������f,���w��7 W!��}d�8]�g�q����(Ñ� iۗM��tm�=�"e�Sh�č��s�|�