Created
September 30, 2010 18:56
-
-
Save djones/605100 to your computer and use it in GitHub Desktop.
Shows how to get the latest posts from an external RSS feed into Refinery
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BlogController < ApplicationController | |
def index | |
begin | |
Timeout::timeout(5) { | |
@posts = RSSParser.run('http://refinerycms.com/blog.rss')[:items][0..3] # get the first 3 posts | |
} | |
rescue Timeout::Error => e | |
logger.warn("timeout exceeded for downloading blog RSS: #{e}") | |
@posts = nil | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h1>Fresh off the blog</h1> | |
<% @posts.each do |post| %> | |
<% unless post.nil? or post[:description].blank? %> | |
<% post[:description] = post[:description].gsub(/’|’/, "'").gsub(/“|”/, '"') %> | |
<p> | |
<strong><%= link_to post[:title], post[:link] %></strong><br/> | |
<%= truncate(strip_tags(post[:description]), | |
:length => RefinerySetting.find_or_set(:blog_blurb_length, 270).to_i) %> | |
</p> | |
<% end %> | |
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# http://snippets.dzone.com/posts/show/68 | |
require 'rexml/document' | |
require 'net/http' | |
class RSSParser | |
require 'rexml/document' | |
def self.run(url) | |
xml = REXML::Document.new Net::HTTP.get(URI.parse(url)) | |
data = { | |
:title => xml.root.elements['channel/title'].text, | |
:home_url => xml.root.elements['channel/link'].text, | |
:rss_url => url, | |
:items => [] | |
} | |
xml.elements.each '//item' do |item| | |
new_items = {} and item.elements.each do |e| | |
new_items[e.name.gsub(/^dc:(\w)/,"\1").to_sym] = e.text | |
end | |
data[:items] << new_items | |
end | |
data | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note there will now be a setting in your Refinery backend called "blog_blurb_length" where you can use the UI to adjust the amount each post truncates by.