Skip to content

Instantly share code, notes, and snippets.

@djones
Created September 30, 2010 18:56
Show Gist options
  • Save djones/605100 to your computer and use it in GitHub Desktop.
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
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
<h1>Fresh off the blog</h1>
<% @posts.each do |post| %>
<% unless post.nil? or post[:description].blank? %>
<% post[:description] = post[:description].gsub(/&#8217;|’/, "'").gsub(/&#8220;|&#8221;/, '"') %>
<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 %>
# 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
@djones
Copy link
Author

djones commented Sep 30, 2010

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment