LOLFeed for Ruby

Flickr's recent introduction of the LOLFeed format was highly amusing. Following Mark Christian's footsteps, I decided to write a Ruby version of the LOLFeed parser. At first it was merely a port, but I then found the state machine to be too cumbersome, so I went all out with regular expressions. Read on for the code.

#!/usr/bin/env ruby

require "net/http"
require "uri"

class LOLFeed
  attr_reader :url, :title, :items

  def initialize(url)
    @url = url
    process(url)
  end

  def process(url)
    raw = Net::HTTP.get(URI.parse(url))
    raise "Feed does not validate." if not valid? raw
    
    @items = []
    feed_title = /^GIMME UPLOADS FROM (.*)\n\r{0,1}/.match(raw)
    @title = feed_title[1] unless feed_title.nil?
    
    [/^HAI\n\r{0,1}IM IN UR BUCKETS MAKING UP FORMATS\n\r{0,1}/,
     /^GIMME UPLOADS FROM .*\n\r{0,1}/,
     /\n\r{0,1}I IS BORED\n\r{0,1}KTHXBYE\.$/].each { |r| raw.gsub! r, "" }
    
    raw.split("KTHX.").each do |entry|
      entry.strip!; item = {}
      
      title = /^I CAN HAS PHOTO (.*)\n\r{0,1}/.match(entry)
      item["title"] = title[1] unless title.nil?
      entry.gsub!(/^I CAN HAS PHOTO .*\n\r{0,1}/, ""); entry.strip!
      
      url = /^ITZ AT (.*)\n\r{0,1}/.match(entry)
      item["url"] = url[1] unless url.nil?
      entry.gsub!(/^ITZ AT .*\n\r{0,1}/, ""); entry.strip!
      
      @items << item
    end
  end

  def valid?(string)
    not (string =~ /^HAI\n\r{0,1}IM IN UR BUCKETS MAKING UP FORMATS/).nil? and
    not (string =~ /I IS BORED\n\r{0,1}KTHXBYE\.$/).nil?
  end
end

This piece of code is in public domain, so you can use it to take over the world with an army of lolcats. Or something.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.