Jon Aquino's Mental Garden

Engineering beautiful software jon aquino labs | personal blog

Sunday, December 05, 2004

The Ruby script I used to parse www.freewareppc.com


class Rating
attr_reader :stars, :comment
def initialize(stars, comment)
@stars = stars
@comment = comment
end
end

class Program
attr_reader :name, :url, :ratings
def initialize(name, url)
@ratings = []
@name = name
@url = url
end
def imageurl
"#{url.gsub('freewareppc.com', 'freewareppc.com/images/products').gsub('shtml', 'gif')}"
end
def addRating(rating)
@ratings << rating
end
def ratingsWithStars(stars)
ratings.select {|rating| rating.stars == stars}.length
end
def <=>(other)
result = other.ratings.length <=> @ratings.length
return result if result != 0
result = other.ratingsWithStars(5) <=> ratingsWithStars(5)
return result if result != 0
result = other.ratingsWithStars(4) <=> ratingsWithStars(4)
return result if result != 0
result = other.ratingsWithStars(3) <=> ratingsWithStars(3)
return result if result != 0
result = other.ratingsWithStars(2) <=> ratingsWithStars(2)
return result if result != 0
ratingsWithStars(1) <=> other.ratingsWithStars(1)
end
def report
return if ratings.length == 0
puts ""
puts "

#{name}

"
puts ""
puts "

"
ratings.sort {|a,b| b.stars <=> a.stars }.each {|rating|
puts "    " + ("*" * rating.stars) + " " + rating.comment + "
"
}
end
end
programs = []
IO.foreach("FreewarePPC-simplified.txt") { | line |
if line =~ /\$\$\$(.*) \^ (.*)/ then
programs << Program.new($1, $2.strip)
elsif line =~ />>> (\**) \^ (.*)/ then
programs.last.addRating(Rating.new($1.length, $2))
else
puts line
throw :shouldNeverReachHere
end
}

programs.sort.each {|program| program.report}

0 Comments:

Post a Comment

<< Home