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 )
|
items = Cache.get( cache_key )
|
||||||
return items if items
|
return items if items
|
||||||
|
|
||||||
puts 'fetch rss...'
|
begin
|
||||||
response = Net::HTTP.get_response( URI.parse(url) )
|
puts 'fetch rss...'
|
||||||
if response.code.to_s != '200'
|
response = Net::HTTP.get_response( URI.parse(url) )
|
||||||
return
|
if response.code.to_s != '200'
|
||||||
end
|
return
|
||||||
rss = SimpleRSS.parse response.body
|
end
|
||||||
items = []
|
rss = SimpleRSS.parse response.body
|
||||||
fetched = 0
|
items = []
|
||||||
rss.items.each { |item|
|
fetched = 0
|
||||||
record = {
|
rss.items.each { |item|
|
||||||
:id => item.id,
|
record = {
|
||||||
:title => item.title,
|
:id => item.id,
|
||||||
:summary => item.summary,
|
:title => item.title,
|
||||||
:link => item.link,
|
:summary => item.summary,
|
||||||
:published => item.published
|
:link => item.link,
|
||||||
|
:published => item.published
|
||||||
|
}
|
||||||
|
items.push record
|
||||||
|
fetched += 1
|
||||||
|
break item if fetched == limit.to_i
|
||||||
}
|
}
|
||||||
items.push record
|
Cache.write( cache_key, items, :expires_in => 4.hours )
|
||||||
fetched += 1
|
rescue Exception => e
|
||||||
break item if fetched == limit.to_i
|
puts "can't fetch #{url}"
|
||||||
}
|
puts e.inspect
|
||||||
Cache.write( cache_key, items, :expires_in => 4.hours )
|
end
|
||||||
|
|
||||||
return items
|
return items
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue