Sunday, February 20, 2011

How to make an RSS feed in Rails


Easy. Don’t. Use an ATOM feed instead. Rails is opinionated software, it has an opinion on feeds, there is an atom_feed helper, but no rss_feedhelper? Omission? Nup.
I added the ATOM feed to Rails Plugins website very simply, by using the Rails atom_feed helper. It was a simple case of creating aFeedsController with just a single index action, which collected up all the time line events that needed to go onto the feed and assigned them to the view in an @events ivar.
So the controller looks like this:
class FeedsController < ApplicationController

layout false

def index
@events = TimelineEvent.for_feed
@title = "Rails Plugins Feed"
@updated = @events.first.created_at unless @events.empty?
end
end
Then in the view file app/views/feeds/index.atom.builder looks like this:
atom_feed do |feed|
feed.title(@title)

feed.updated(@updated)

@events.each do |event|
feed.entry(event) do |entry|

entry.title(h(event.title))
entry.summary(truncate(strip_tags(event.description), :length => 100))

entry.author do |author|
author.name(event.author_name)
end
end
end
end
And then the route to wire it up:
# config/routes.rb (Rails 2.0)
ActionController::Routing::Routes.draw do |map|
map :resources :feeds, :only => [:index]
end
And finally the auto_discovery_link_tag in the application.html.erb:
<%= auto_discovery_link_tag(:atom, "/feeds.atom") %>
Now this was all well and good, and I thought, why not add an RSS feed as well?
I started down on this path and the first thing that struck me was, there was no rss_feed helper in Rails, why? Seemed like an ommission…
Then I was looking around the web and found many examples on how to do an RSS feed in Rails, but none of them mentioned this omission of the helper, why?
I didn’t puzzle over this long, and just went ahead and added an RSSfeed, it wasn’t hard, you can find this out from your friend google.
But then I got this IM from Sam Ruby after I let him know the RSS feed for Rails Plugins was now online:
“Ewwwww”
Which was not really the answer I was expecting, so I stated:
“I never did get the ATOM v RSS debate”
To which he replied:
“I started the Atom vs RSS debate.”
And that started an interesting chat! Let me tell you!
Turns out that ATOM is actually defined, with RFC 4287 that covers all the basics of what you need to know.
Also turns out that any modern reader will handle ATOM, just as easily asRSS.
Also turns out that with ATOM you can define things like (gasp) language settings, Internationalization, content types and several other points which make the data a lot more consumable.
So, then I added the following to the Rails Plugins website:
class FeedsController < ApplicationController
def index
# ...
respond_to do |wants|
wants.rss do
redirect_to feeds_path(:format => :atom),
:status=>:moved_permanently
end
wants.atom
end
end
end

How to use Mail / ActionMailer 3 with GMail SMTP

Stripping dollar signs and commas from a string


It is moments like these that you realise why it is you love Ruby so much…
Today was one of those moments with the String class’s :delete method.
I have used this method many, many times in the past.. and this is nothing new for me… but it was one of those moments where I just went “Gee, I love coding in Ruby…” and I wanted to share :)
If you ever need to convert, say $12,345.12 into a single float number like12345.12 a Ruby newbie, might do something like this:
"$12,345.12".gsub("$",'').gsub(",",'') #=> "12345.12"
But hark! Look into the string class and you shall find the :deletemethod, which lets you do this:
"$12,345.12".delete("$,") #=> "12345.12"

Getting Heroku, memcached and Rails 3 working


The docs for Heroku and memecached do not cover Rails 3. So here is the short version:
In your Gemfile:
group :production do
gem "memcache-client"
gem 'memcached-northscale', :require => 'memcached'
end
In your environment.rb:
config.cache_store = :mem_cache_store, Memcached::Rails.new
Told you it was short.
Oh, and remember to install the memcached add on to your app:
heroku addons:add memcache:5mb

'rbuf_fill': execution expired (Timeout::Error)

Friday, February 18, 2011

Ruby: HTTP Request


require 'net/http'
req = Net::HTTP.get_response(URI.parse('http://www.juretta.com/'))
print req.body
require 'open-uri'
open("http://www.juretta.com/") {|f|
print f.read
}

Thursday, February 17, 2011

HTML / CSS / Link Validation - Check for Errors, Standards Compliance, Broken Links


W3C HTML Validation Service
You can use the W3 Consortium's HTML validator to check your web documents either by giving it your web page's URL or uploading your document directly to the validator. Many people consider it the authoritative source for checking your web pages for standards compliance (since the W3 Consortium is responsible for the various HTML/CSS standards). Source code for the validator is also available.
HTML Tidy (HTML Validator)
HTML Tidy is not just a HTML validator for your web documents. It is able to correct (repair and fix) bad HTML code and give you pointers to areas where you need to change in order to make your web page accessible to people with disabilities. Source code and binaries are available for a wide variety of operating systems, including Windows 95/98/NT/2000, Mac OS, Atari, Amiga, BeOS, AIX, Linux, UnixWare, HP-UX, MSDOS, Solaris, OS/2, FreeBSD, RISC OS, MiNT (Atari) OS, etc. There is even a Java port of HTML Tidy.
Tidy Online
This is an online version of the HTML Tidy validator that you can use to clean up your web pages if you're too lazy to download your own copy. (To get your own copy, see elsewhere on this page - listed under "HTML Tidy".)
Web Page Purifier
The Web Page Purifier allows you to check your HTML pages for compliance ("purity") with various HTML standards (eg HTML 4.0 Transitional, HTML 4.0 Strict, WebTV 1.1, HTML 3.2, etc). There's a form where you can enter the URL of the page to check, or you can also download the Perl CGI source code.
Web Page Backward Compatibility Viewer
If you need to check your web pages to see if they are compatible with browsers that lack certain features, this page checker might be useful. You actually have to specify which features you do not want on a page, like tables, images, etc. It is probably not as useful as the title of the site sounds, since if you knew which features you needed to avoid, you would not put them in a page in the first place. (It would be better if there were simple options where you could simply select the browser versions you want to check compatibility with, instead of having to specify features.)

Free Web Page Accessibility Checkers

ATRC Web Accessibility Checker New
The ATRC Web Accessibility Checker is a free online service that checks your website for accessibility issues. You can use it by either supplying it with your website's address or by uploading the page you want to test from your browser.
WAVE Accessibility Tool
WAVE checks your web page for compliance with various accessibility standards. It loads the page or URL you supply, and re-displays it with icons placed at various parts of your page indicating whether that particular portion creates accessibility problems or not. You can use it for a page on the web, upload a page from your hard disk, install a toolbar in your browser or add a bookmarklet (a bookmark that executes a JavaScript) that when clicked will send the current page for evaluation.
Cynthia Says Portal
Cynthia checks your site against the US Section 508 standards and the Web Content Acessibility Guidelines (WCAG). It only allows you to test one page at a time.
A-Prompt
A-Prompt is a Windows program that checks your pages for accessibility issues and fixes the pages for you (with your help). It has a Wizard-like interface which guides you through the individual (potential) problems that your web page may have and prompts you for information for repairing the page.

Free Cascading Style Sheets (CSS) Validators

W3C CSS Validation Service
This is the W3 Consortium's CSS validator for validating Cascading Style Sheets. You can either download it and use it on your own machine, upload your cascading style sheet (CSS) to their online service, or supply your URL for their spider to visit your site. The W3 Consortium is responsible for the various CSS and HTML standards.
CSSCheck: Cascading Style Sheets Lint
This is a validator for Cascading Style Sheets (CSS) that also looks into a few browser compatibility issues with your style sheet.

Free Broken Links Validators

W3C Link Checker
Worried about broken links in your web documents? This online validator from the W3 Consortium is able to recursively check your document for dead links. You simply enter a URL in the form provided, and it will visit your site and check the links.
Xenu Link Sleuth: Find Broken Links
Xenu is a utility for Windows that checks your web site for broken links. It can work both with a "live" website as well as on a copy of your web site residing on your own hard disk. It's a favourite of many webmasters for checking broken links on their site.