Robot Has No Heart

Xavier Shay blogs here

A robot that does not have a heart

Practical Hpricot: CruiseControl.rb results

1
2
3
4
5
6
7
8
9
10
require 'hpricot'
require 'open-uri'

url = "http://mydomain.com/builds/myapp/#{ARGV[0]}"
doc = Hpricot(open(url))

puts (doc/"div#build_details h1").first.inner_text.gsub(/^\s*/, '')
(doc/"div.test-results").each do |results|
  puts results.inner_html
end

Grabs the current build status from CruiseControl.rb. Especially handy since our build server isn’t sending emails at the moment.

Practical Hpricot: SVG

Inkscape does a pretty good job of creating plain SVG files, but they could be nicer. A particular file I was working on had many path elements, all with the same style attribute that I wanted to move into a parent tag (or external style or whatever). What better way to strip them out than Hpricot?

1
2
3
4
5
6
7
8
9
10
11
require 'hpricot'

doc = open(ARGV[0]) { |f| Hpricot.XML(f) }

(doc/:path).each do |path|
  [:id, :style].each do |attr| 
    path.remove_attribute(attr)
  end
end

puts doc

And you get the benefit of prettier formatting!

Practical Hpricot: XML to INI

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
require 'hpricot'
require 'open-uri'

def ini_entry(url, name)
  buffer = "[#{url}]\\n"
  buffer += "name = #{name}\\n"
  buffer += "\\n"
  buffer
end

doc = Hpricot(open("http://www.byteclub.net/testsite/getFeeds.php"))

(doc/"blog").each do |elem|
  url  = (elem/"url")
  name = (elem/"name")
  comments = (elem/"comments")
  
  if name.length > 0
    puts ini_entry(url.inner_text, name.inner_text) if url.inner_text.length > 0
    puts ini_entry(comments.inner_text, name.inner_text + " Comments") if comments.inner_text.length > 0
  end
end

Planet coming soon!

A pretty flower Another pretty flower