Monday, March 31, 2008

ruby, xml and hpricot

I'm starting to play with xml in ruby. After asking around on IRC, using hpricot was recommend to me.

I've been play a city at myminicity.com.

You can get xml of city details from an URL like this http://gnollford.myminicity.com/xml

I've written a small chunk of ruby that read & analysis the xml and generates a web address the help build the city to best advantage.

require 'net/http'
require 'rubygems'
require 'hpricot'

url = ['http://gnollford.myminicity.com/',
'http://extremesville.myminicity.com/',
'http://froosh.myminicity.com/',
'http://griff-stadt.myminicity.com/',
'http://warlach.myminicity.com/',
'http://aeoth.myminicity.com/',
'http://halloranelder.myminicity.com/',
'http://cricklewood.myminicity.com/',
'http://twodogs.myminicity.com/',
'http://iliad.myminicity.com/',
'http://meatteam.myminicity.com/',
'http://aushpb.myminicity.com/',
'http://brisbane.myminicity.com/']

source="NET"
#source="FILE"

url.each do |cityurl|

if source == "NET"
xml_data = Net::HTTP.get_response(URI.parse(cityurl+"xml")).body
else # FILE branch
xml_data = File.open('data1.xml')
end

doc = Hpricot::XML(xml_data)
(doc/:city).each do |xml_city|
#puts doc.search("/city/bases").first.attributes["ind"]
#puts doc.search("/city/bases").first.attributes["tra"]
#puts doc.search("/city/bases").first.attributes["sec"]
#puts doc.search("/city/bases").first.attributes["env"]

region = doc.search("/city/region").first.attributes["code"]
nm = doc.search("/city/name").first.innerHTML
ranking = doc.search("/city/ranking").first.innerHTML

typ = ""
vector = 0

uem = doc.search("/city/unemployment").first.innerHTML.to_i
if uem > vector
vector = uem
typ = "ind"
end

tra = doc.search("/city/transport").first.innerHTML.to_i
trap = 100-tra
if trap > vector
vector = trap
typ = "tra"
end

cri = doc.search("/city/criminality").first.innerHTML.to_i
if cri > vector
vector = cri
typ = "sec"
end

pol = doc.search("/city/pollution").first.innerHTML.to_i
if pol > vector
vector = pol
typ = "env"
end

printf(cityurl+typ+" "+nm+" "+typ+" "+region+" "+ranking+"\n")
end
end

the output

http://gnollford.myminicity.com/ Gnollford AU 1743
http://extremesville.myminicity.com/ Extremesville AU 1450
http://froosh.myminicity.com/tra Froosh tra AU 803
http://griff-stadt.myminicity.com/tra Griff-Stadt tra AU 1029
http://warlach.myminicity.com/ Warlach AU 1196
http://aeoth.myminicity.com/ind Aeoth ind AU 1304
http://halloranelder.myminicity.com/ HalloranElder AU 505
http://cricklewood.myminicity.com/ cricklewood GB 452
http://twodogs.myminicity.com/ TwoDogs AU 779
http://iliad.myminicity.com/tra Iliad tra AU 81
http://meatteam.myminicity.com/ meatteam AU 14644
http://aushpb.myminicity.com/env aushpb env AU 82
http://brisbane.myminicity.com/tra brisbane tra AU 3

It easy to take the next step and generate html, I didn't just to keep this blog post's html simple.


Gnoll110