Pimp my Ruby

M

Matthew Hinton

Hello from a Ruby noob.

I wrote a script to read in my Bloglines subscriptions and my
NetNewsWire subscriptions. The script compares to the two opml files
and any Bloglines subscription that is also in NNW is ignored. The
subscriptions that are left are written out to an opml file. If anyone
has read Wil Shipley's blog and his pimp my code series will understand
right away what I am asking for. I would like those with more Ruby
experience to look over my code and critic it.

Fire away!



class Merge
def initialize(filename1, filename2)
read_in(filename1, filename2)
@subs1 = extract_subscriptions(@opml1)
@subs2 = extract_subscriptions(@opml2)
end

def all_subs
diff1 = @subs1.keys - @subs2.keys
diff2 = @subs2.keys - @subs1.keys
tmp = diff1 + diff2
@all_subs = find_subs_titles(tmp)
end

def diff_subs
if (@subs1.length < @subs2.length)
small = @subs1
large = @subs2
else
small = @subs2
large = @subs1
end
tmp = small.keys - large.keys
@diff_subs = find_subs_titles(tmp)
end

def find_subs_titles(subs)
subs_titles = Hash.new
subs.each do |xmlUrl|
title = @subs1[xmlUrl] if (@subs1[xmlUrl] != nil)
title = @subs2[xmlUrl] if (@subs2[xmlUrl] != nil)
subs_titles[xmlUrl] = title
end
return subs_titles
end

def read_in(filename1, filename2)
file = File.open(filename1, "r")
@opml1 = file.readlines
file.close
file = File.open(filename2, "r")
@opml2 = file.readlines
file.close
end

def write_opml
file = File.open("diff_subs.opml", "w")
# write out opml
file.puts %q{
<?xml version="1.0" encoding="utf-8" ?>
<opml version="1.0">
<head>
<title>Diffed Subscriptions</title>
<dateCreated>Thu, 19 Jan 2006 21:37:17 GMT</dateCreated>
<ownerName>mhinton</ownerName>
</head>
<body>
<outline title="Subscriptions"> }
@diff_subs.each do |k,v|
file.print "<outline title=\"" + v + "\" type=\"rss\" xmlUrl=\""
+ k + "\" />\n"
end
file.puts %q{
</outline>
</body>
</opml>
}
file.close
end

def extract_subscriptions(opml)
subs = Hash.new
opml.each { |line|
if line =~ /title=\"(.*?)\"/
title = $1
end
if line =~ /xmlUrl=\"(.*?)\"/
xmlUrl = $1
end
if (title != nil && xmlUrl != nil)
subs[xmlUrl] = title
end
title = xmlUrl = nil
}
return subs
end

def print_all_subs
puts @all_subs.inspect
end

def print_diff_subs
puts "diff_subs: #{@diff_subs.length}"
puts @diff_subs.inspect
end

def print_opml(opml)
opml.each { |line|
if line =~ /title=\"(.*?)\"/
@title = $1
end
if line =~ /htmlUrl=\"(.*?)\"/
@htmlUrl = $1
end
if line =~ /xmlUrl=\"(.*?)\"/
@xmlUrl = $1
end
if @title != ""
puts "title: #{@title} htmlUrl: #{@htmlUrl} xmlUrl: #{@xmlUrl}"

puts
end
@title = @htmlUrl = @xmlUrl = ""
}
end
end

merger = Merge.new("bloglines.opml", "NNW.opml")
merger.diff_subs
merger.write_opml
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,778
Messages
2,569,605
Members
45,237
Latest member
AvivMNS

Latest Threads

Top