我在 Rails 应用程序中使用 feed_parser gem。它完全按照开发中的指示工作,也可以在生产控制台中工作,但不会加载到生产 Web 服务器上。
(注意:它甚至在我们的 Ubuntu 12.04 测试服务器上运行良好,在 10.04 生产服务器上失败)
uninitialized constant - Project::FeedParser
我在我们网站的一个模型中运行它:
class Project < ActiveRecord::Base
def self.facebook_feed
url = "http://www.facebook.com/feeds/page.php?id=236004913152511&format=rss20"
posts = Project.parse_feed(url)
return posts
end
def self.blogspot_feed
url = "http://fundinggarage.blogspot.com/feeds/posts/default?alt=rss"
posts = Project.parse_feed(url)
return posts
end
private
def self.parse_feed(feed_url)
feed = FeedParser.new(:url => feed_url).parse
fj = feed.as_json
#fj[:items].first[:description]
posts = []
fj[:items].take(4).each do |fp|
post = {}
doc = Nokogiri::HTML(fp[:description])
img_srcs = doc.css('img').map{ |i| i['src'] }
post[:headline] = fp[:title]
post[:image] = "/assets/fg_image_placehloder.png"
post[:image] = img_srcs.first unless img_srcs.first.nil?
post[:url] = fp[:link]
post[:date] = fp[:published]
posts << post
end
return posts
end
end
在视图中:
<% Project.blogspot_feed.each do |fb| %>
<div class="grid_3">
<div class="media other-post-item">
<a href="<%= fb[:url] %>" class="thumb-left" target="_blank">
<div class="blog-img">
<img src="<%= fb[:image] %>" alt="<%= raw fb[:headline] %>" title="<%= raw fb[:headline] %>">
</div>
<span class="be-fc-orange">
<h4 class="rs title-other-post"><%= raw fb[:headline] %></h4>
<p class="rs fc-gray time-post pb10"><%= "#{time_ago_in_words(fb[:date])} ago" %></p>
</span>
</a>
</div>
</div><!--end: . other-post-item -->
<% end %>