Saturday, April 16, 2005

eruby Source Code For "Bloglines Shuffler"


<html>
<head>
<title>Bloglines Shuffler</title>
<style type='text/css' media='all'>
body {
font: small Verdana, Arial, Helvetica, sans-serif;
}
#item-title {
font-weight: bold;
font-size: medium;
}
#item-body {
font-size: small;
}
</style>
</head>
<body>
<h1>Bloglines Shuffler</h1>
<table width='100%' cellspacing='0' cellpadding='0'>
<%
begin
require "cgi"
cgi = CGI.new
username = cgi["username"]
password = cgi["password"]

require 'net/http'
def get(address, path, username, password)
Net::HTTP.start(address) { |http|
req = Net::HTTP::Get.new(path)
req.basic_auth username, password
response = http.request(req).body
}
end

# my_shuffle! code by David Alan Black, "Array.shuffle",
# http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/19148
# [Jon Aquino 2005-04-16]
class Array
def my_shuffle!
size.downto(1) do |n| push(delete_at(rand(n))) end
self
end
end

items = []
require "rexml/document"
include REXML
doc = Document.new(get("bloglines.com", "/getitems?s=0&n=0", username, password))
XPath.each(doc, "/rss/channel") { |channel|
channel.elements.each('item') { |item|
# "link" elements are sometimes not provided [Jon Aquino 2005-04-16]
items << "<span id='item-title'><a href=\"#{item.text('link')}\">#{item.text('title')}</a></span><br>" +
"<span id='item-body'><a href=\"#{channel.text('link')}\">#{channel.text('title')}</a><br>" +
"#{item.text('description')}</span><br>"
}
}
i = 0

items.my_shuffle!.each { |item|
i += 1
%>
<tr bgcolor="<%=i%2==0?"#efefef":"#ffffff"%>"><td><br>
<%=item%>
<br></td></tr>
<%
}
rescue => e
%>
<%="Exception: #{e.class}: #{e.message}\n\t#{e.backtrace.join("\n\t")}"%>
<%
end
%>
</table>
<%=Time.new%>
</body>
</html>

0 Comments:

Post a Comment

<< Home