Added rss caching.
This commit is contained in:
parent
06b5b1e166
commit
1adfda5b4f
1 changed files with 25 additions and 12 deletions
|
@ -5,6 +5,10 @@ class RssController < ApplicationController
|
||||||
def fetch
|
def fetch
|
||||||
url = params[:url]
|
url = params[:url]
|
||||||
limit = params[:limit] || 10
|
limit = params[:limit] || 10
|
||||||
|
|
||||||
|
cache_key = 'rss::' + url
|
||||||
|
items = Rails.cache.read( cache_key )
|
||||||
|
if !items
|
||||||
response = Net::HTTP.get_response( URI.parse(url) )
|
response = Net::HTTP.get_response( URI.parse(url) )
|
||||||
if response.code.to_s != '200'
|
if response.code.to_s != '200'
|
||||||
render :json => { :message => "failed to fetch #{url}, code: #{response.code}"}, :status => :unprocessable_entity
|
render :json => { :message => "failed to fetch #{url}, code: #{response.code}"}, :status => :unprocessable_entity
|
||||||
|
@ -14,10 +18,19 @@ class RssController < ApplicationController
|
||||||
items = []
|
items = []
|
||||||
fetched = 0
|
fetched = 0
|
||||||
rss.items.each { |item|
|
rss.items.each { |item|
|
||||||
items.push item
|
record = {
|
||||||
|
:id => item.id,
|
||||||
|
:title => item.title,
|
||||||
|
:summary => item.summary,
|
||||||
|
:link => item.link,
|
||||||
|
:published => item.published
|
||||||
|
}
|
||||||
|
items.push record
|
||||||
fetched += 1
|
fetched += 1
|
||||||
break item if fetched == limit.to_i
|
break item if fetched == limit.to_i
|
||||||
}
|
}
|
||||||
|
Rails.cache.write( cache_key, items, :expires_in => 4.hours )
|
||||||
|
end
|
||||||
render :json => { :items => items }
|
render :json => { :items => items }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue