Improved error handling.
This commit is contained in:
parent
6f4ee4c2bf
commit
db9e98f025
1 changed files with 25 additions and 20 deletions
45
lib/rss.rb
45
lib/rss.rb
|
@ -4,27 +4,32 @@ module RSS
|
|||
items = Cache.get( cache_key )
|
||||
return items if items
|
||||
|
||||
puts 'fetch rss...'
|
||||
response = Net::HTTP.get_response( URI.parse(url) )
|
||||
if response.code.to_s != '200'
|
||||
return
|
||||
end
|
||||
rss = SimpleRSS.parse response.body
|
||||
items = []
|
||||
fetched = 0
|
||||
rss.items.each { |item|
|
||||
record = {
|
||||
:id => item.id,
|
||||
:title => item.title,
|
||||
:summary => item.summary,
|
||||
:link => item.link,
|
||||
:published => item.published
|
||||
begin
|
||||
puts 'fetch rss...'
|
||||
response = Net::HTTP.get_response( URI.parse(url) )
|
||||
if response.code.to_s != '200'
|
||||
return
|
||||
end
|
||||
rss = SimpleRSS.parse response.body
|
||||
items = []
|
||||
fetched = 0
|
||||
rss.items.each { |item|
|
||||
record = {
|
||||
:id => item.id,
|
||||
:title => item.title,
|
||||
:summary => item.summary,
|
||||
:link => item.link,
|
||||
:published => item.published
|
||||
}
|
||||
items.push record
|
||||
fetched += 1
|
||||
break item if fetched == limit.to_i
|
||||
}
|
||||
items.push record
|
||||
fetched += 1
|
||||
break item if fetched == limit.to_i
|
||||
}
|
||||
Cache.write( cache_key, items, :expires_in => 4.hours )
|
||||
Cache.write( cache_key, items, :expires_in => 4.hours )
|
||||
rescue Exception => e
|
||||
puts "can't fetch #{url}"
|
||||
puts e.inspect
|
||||
end
|
||||
|
||||
return items
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue