Lightweight Ruby Webserver to list podcasts

Once you get your head around ruby constructs the following was easy to write:

As a benefit it actually displays properly…

require ‘socket’

def latest3podcasts(session, podcast)
session.print “


  • Dir.glob(podcast)[-3..-1].each {|x| session.print “
  • #{x}
  • ” }
    session.print “

rn”
end

port = 8080.to_i
server = TCPServer.new(‘serveraddress’,port)
while (session = server.accept)
puts “Request: #{session.gets}”
session.print “HTTP/1.1 200/OKrnContent-type: text/htmlrnrn”
session.print “”
latest3podcasts(session, “/storage/podcast/dotnetrocks/*.mp3”)
latest3podcasts(session, “/storage/podcast/hanselminutes/*.mp3”)
latest3podcasts(session, “/storage/podcast/polymorphicpodcast/*.mp3”)
session.print “rn”
session.close
end

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s