GnuVince
May 31st, 2005, 03:00 PM
Hi guys,
I've been writing quite a few Ruby scripts lately, so I thought I'd share a few with you. I insist on the fact that these are scripts, so they are quick and dirty. Most don't do extensive error checking, but they get the job done.
1. I just wrote this script, it transforms a URL into a TinyURL. I may work on the TinyUrl class and put it on Rubyforge. (By the way, do you guys prefer TinyUrl or TinyURL?)
#!/usr/local/bin/ruby
require "net/http"
class TinyUrl
def initialize(url)
@url = url
end
def shorten
http = Net::HTTP.start("tinyurl.com", 80)
response = http.post("/create.php", "url=#{@url}")
if response.code == "200"
body = response.read_body
line = body.split("\n").find { |l| l =~ /hidden name=tinyurl/ }
i1 = line.index("http")
i2 = line.rindex("\"")
return line[i1...i2]
end
end
end
def main
if ARGV[0]
url = ARGV[0]
t = TinyUrl.new(url)
puts t.shorten
else
puts "Usage: #$0 <url>"
end
end
if $0 == __FILE__
main
end
2. Syntax highlighting for Ruby code. I used the syntax module by Jamis Buck to make a very simple syntax highlighter. The first argument is your Ruby script, so put that inside a here-doc
#!/usr/bin/env ruby
require "rubygems"
require "syntax/convertors/html"
if ARGV[0]
puts Syntax::Convertors::HTML.new(Syntax::load("ruby")).convert(ARGV[0])
else
puts "Usage: $0 <code>"
end
And here's a nice CSS file to have nice colors:
pre {
font-family: courier;
color: #cfbfad;
background-color: #1e1e27;
border: 1px solid #ccc;
padding: 5px;
}
.normal { color: #cfbfad;}
.comment { color: #cd8b00; font-style: italic; }
.keyword { color: #808bed; }
.method { color: #ff8bff; }
.class { color: #409090; }
.module { color: #409090; }
.punct { color: #ffffcd; }
.symbol { color: #ffcd8b; }
.string { color: #ffcd8b; background-color: #404040; }
.char { color: #ffffcd; }
.ident { color: #ffffcd; }
.constant { color: #ff8bff; }
.regex { color: #ffcd8b; background-color: #404040; }
.number { color: #506dbd; }
.attribute { color: #ff8bff; }
.global { color: #ffcd8b; }
.expr { color: #ff8bff; }
3. A script to make a RSS feed of my favorite net cartoons.
#!/usr/local/bin/ruby
require "rubygems"
require "builder"
require "net/http"
DIR = File.dirname(__FILE__) + File::Separator
module Comics
def penny_arcade
begin
s = Net::HTTP.get(URI.parse("http://www.penny-arcade.com/view.php3"))
s.split("\n").grep(/img.penny-arcade.com/)[0].strip[0..-6]
rescue Exception
"Can't get strip"
end
end
def pvponline
now = Time.now
"<img src=\"http://www.pvponline.com/archive/#{now.year}/pvp#{now.strftime("%Y%m%d")}.gif\">"
end
def calvin_and_hobbes
begin
s = Net::HTTP.get(URI.parse("http://www.ucomics.com/calvinandhobbes/"))
s = s.split("\n").find { |ss| ss =~ /\/ch\// }
i1 = s.index("http")
i2 = s.index("gif") + 3
"<img src=\"#{s[i1...i2]}\">"
rescue Exception
"Can't get strip"
end
end
def user_friendly
begin
year_month = Time.now.strftime("%y%b").downcase
s = Net::HTTP.get(URI.parse("http://www.userfriendly.org"))
s = s.split("\n").find { |ss| ss =~ Regexp.new(year_month) }
i1 = s.index("<IMG")
i2 = s.index("gif") + 5
s[i1...i2]
rescue Exception
"Can't get strip"
end
end
def flying_mccoys
begin
s = Net::HTTP.get(URI.parse("http://www.ucomics.com/theflyingmccoys/"))
s.split("\n").find { |ss| ss =~ Regexp.new("fmc/#{Time.now.year}") }.strip
rescue Exception
"Can't get strip"
end
end
def peanuts
begin
s = Net::HTTP.get(URI.parse("http://www.comics.com/comics/peanuts/"))
s = s.split("\r\n").find { |ss| ss =~ /\d\.gif.+Today's/ }
i1 = s.index("comics")
i2 = s.index("gif") + 3
"<img src=\"http://www.comics.com/#{s[i1...i2]}\">"
rescue Exception
"Can't get strip"
end
end
end
class ComicCollection
attr_reader :arr
def initialize
@arr = [ ]
end
def add(title, url, description)
@arr << {
"title" => title,
"url" => url,
"desc" => description
}
end
def add_array(arr)
arr.each { |a| add(*a) }
end
end
def build_rss_feed(comics)
xml = Builder::XmlMarkup.new(:target => STDOUT, :indent => 2)
xml.rss do
xml.channel do
xml.title("Comics")
xml.link("http://localhost")
xml.description("Vincent's comics")
xml.language "en-us"
xml.ttl "40"
comics.each do |comic|
xml.item do
xml.title("#{comic['title']} for: #{Time.now.strftime('%Y-%m-%d')}")
xml.description(Time.now.to_s + "<P>" + comic["desc"])
xml.pubDate(Time.now.strftime("%a, %d %b %Y %T %Z"))
xml.guid(comic["url"])
xml.link(comic["url"])
xml.category(comic["title"])
end
end
end
end
end
def main
include Comics
cc = ComicCollection.new
comics = [
["Penny-Arcade",
"http://www.penny-arcade.com",
penny_arcade],
["PvP Online",
"http://www.pvponline.com",
pvponline],
["Calvin and Hobbes",
"http://www.ucomics.com/calvinandhobbes/",
calvin_and_hobbes],
["User Friendly",
"http://www.userfriendly.org",
user_friendly],
["The Flying McCoys",
"http://www.ucomics.com/theflyingmccoys/",
flying_mccoys],
["Peanuts",
"http://www.comics.com/comics/peanuts/",
peanuts]
]
cc.add_array(comics)
build_rss_feed(cc.arr)
end
if $0 == __FILE__
main
end
So, there they are. It seems like these days, I'm writing Ruby scripts like crazy, I got another one to fetch bash.org quotes, last night I made a quick Rails application to register to the next LUG in my region (we used meetup.com, but now we're cheap bastards and refuse to pay :)), I wrote a script for someone in my LUG to analyze his Amavis log files, and I'm writing a Ruby scripting tutorial based on that script.
Ruby is all around me! I LOVE IT!
I've been writing quite a few Ruby scripts lately, so I thought I'd share a few with you. I insist on the fact that these are scripts, so they are quick and dirty. Most don't do extensive error checking, but they get the job done.
1. I just wrote this script, it transforms a URL into a TinyURL. I may work on the TinyUrl class and put it on Rubyforge. (By the way, do you guys prefer TinyUrl or TinyURL?)
#!/usr/local/bin/ruby
require "net/http"
class TinyUrl
def initialize(url)
@url = url
end
def shorten
http = Net::HTTP.start("tinyurl.com", 80)
response = http.post("/create.php", "url=#{@url}")
if response.code == "200"
body = response.read_body
line = body.split("\n").find { |l| l =~ /hidden name=tinyurl/ }
i1 = line.index("http")
i2 = line.rindex("\"")
return line[i1...i2]
end
end
end
def main
if ARGV[0]
url = ARGV[0]
t = TinyUrl.new(url)
puts t.shorten
else
puts "Usage: #$0 <url>"
end
end
if $0 == __FILE__
main
end
2. Syntax highlighting for Ruby code. I used the syntax module by Jamis Buck to make a very simple syntax highlighter. The first argument is your Ruby script, so put that inside a here-doc
#!/usr/bin/env ruby
require "rubygems"
require "syntax/convertors/html"
if ARGV[0]
puts Syntax::Convertors::HTML.new(Syntax::load("ruby")).convert(ARGV[0])
else
puts "Usage: $0 <code>"
end
And here's a nice CSS file to have nice colors:
pre {
font-family: courier;
color: #cfbfad;
background-color: #1e1e27;
border: 1px solid #ccc;
padding: 5px;
}
.normal { color: #cfbfad;}
.comment { color: #cd8b00; font-style: italic; }
.keyword { color: #808bed; }
.method { color: #ff8bff; }
.class { color: #409090; }
.module { color: #409090; }
.punct { color: #ffffcd; }
.symbol { color: #ffcd8b; }
.string { color: #ffcd8b; background-color: #404040; }
.char { color: #ffffcd; }
.ident { color: #ffffcd; }
.constant { color: #ff8bff; }
.regex { color: #ffcd8b; background-color: #404040; }
.number { color: #506dbd; }
.attribute { color: #ff8bff; }
.global { color: #ffcd8b; }
.expr { color: #ff8bff; }
3. A script to make a RSS feed of my favorite net cartoons.
#!/usr/local/bin/ruby
require "rubygems"
require "builder"
require "net/http"
DIR = File.dirname(__FILE__) + File::Separator
module Comics
def penny_arcade
begin
s = Net::HTTP.get(URI.parse("http://www.penny-arcade.com/view.php3"))
s.split("\n").grep(/img.penny-arcade.com/)[0].strip[0..-6]
rescue Exception
"Can't get strip"
end
end
def pvponline
now = Time.now
"<img src=\"http://www.pvponline.com/archive/#{now.year}/pvp#{now.strftime("%Y%m%d")}.gif\">"
end
def calvin_and_hobbes
begin
s = Net::HTTP.get(URI.parse("http://www.ucomics.com/calvinandhobbes/"))
s = s.split("\n").find { |ss| ss =~ /\/ch\// }
i1 = s.index("http")
i2 = s.index("gif") + 3
"<img src=\"#{s[i1...i2]}\">"
rescue Exception
"Can't get strip"
end
end
def user_friendly
begin
year_month = Time.now.strftime("%y%b").downcase
s = Net::HTTP.get(URI.parse("http://www.userfriendly.org"))
s = s.split("\n").find { |ss| ss =~ Regexp.new(year_month) }
i1 = s.index("<IMG")
i2 = s.index("gif") + 5
s[i1...i2]
rescue Exception
"Can't get strip"
end
end
def flying_mccoys
begin
s = Net::HTTP.get(URI.parse("http://www.ucomics.com/theflyingmccoys/"))
s.split("\n").find { |ss| ss =~ Regexp.new("fmc/#{Time.now.year}") }.strip
rescue Exception
"Can't get strip"
end
end
def peanuts
begin
s = Net::HTTP.get(URI.parse("http://www.comics.com/comics/peanuts/"))
s = s.split("\r\n").find { |ss| ss =~ /\d\.gif.+Today's/ }
i1 = s.index("comics")
i2 = s.index("gif") + 3
"<img src=\"http://www.comics.com/#{s[i1...i2]}\">"
rescue Exception
"Can't get strip"
end
end
end
class ComicCollection
attr_reader :arr
def initialize
@arr = [ ]
end
def add(title, url, description)
@arr << {
"title" => title,
"url" => url,
"desc" => description
}
end
def add_array(arr)
arr.each { |a| add(*a) }
end
end
def build_rss_feed(comics)
xml = Builder::XmlMarkup.new(:target => STDOUT, :indent => 2)
xml.rss do
xml.channel do
xml.title("Comics")
xml.link("http://localhost")
xml.description("Vincent's comics")
xml.language "en-us"
xml.ttl "40"
comics.each do |comic|
xml.item do
xml.title("#{comic['title']} for: #{Time.now.strftime('%Y-%m-%d')}")
xml.description(Time.now.to_s + "<P>" + comic["desc"])
xml.pubDate(Time.now.strftime("%a, %d %b %Y %T %Z"))
xml.guid(comic["url"])
xml.link(comic["url"])
xml.category(comic["title"])
end
end
end
end
end
def main
include Comics
cc = ComicCollection.new
comics = [
["Penny-Arcade",
"http://www.penny-arcade.com",
penny_arcade],
["PvP Online",
"http://www.pvponline.com",
pvponline],
["Calvin and Hobbes",
"http://www.ucomics.com/calvinandhobbes/",
calvin_and_hobbes],
["User Friendly",
"http://www.userfriendly.org",
user_friendly],
["The Flying McCoys",
"http://www.ucomics.com/theflyingmccoys/",
flying_mccoys],
["Peanuts",
"http://www.comics.com/comics/peanuts/",
peanuts]
]
cc.add_array(comics)
build_rss_feed(cc.arr)
end
if $0 == __FILE__
main
end
So, there they are. It seems like these days, I'm writing Ruby scripts like crazy, I got another one to fetch bash.org quotes, last night I made a quick Rails application to register to the next LUG in my region (we used meetup.com, but now we're cheap bastards and refuse to pay :)), I wrote a script for someone in my LUG to analyze his Amavis log files, and I'm writing a Ruby scripting tutorial based on that script.
Ruby is all around me! I LOVE IT!