Pinging Technorati (or anything) with Ruby.

by Rabbit

Searching Google has probably led you to _why’s code. While it works, I’m not satisfied with it. Why? Frankly, I don’t understand it. Not that the code itself is horribly difficult, but I’ve read Technorati’s page on how you generate your own ping, and I see very little of what they describe in _why’s code.

Technorati describes a ping as an HTTP header and an XML document. Well… okay… How do I make that? How do I tell Ruby to send what is essentially an arbitrary HTTP request out over the wire?

Enter Net::HTTP.

There’s some documentation for Net::HTTP, and even some good examples, but by and large, it’s a confusing set of libraries. It’s been a few days since I wrote this code, and I doubt I could retrace my mental steps, so I’ll just lay it on you. As with most code I’m sure it can be made better, but here she is, a fairly manual, custom ping to Technorati:

require 'rubygems'
require 'hpricot'
require 'uri'
require 'net/http'
 
xmlrpc_request = <<EOF
<?xml version="1.0"?>
<methodCall>
  <methodName>weblogUpdates.ping</methodName>
  <params>
    <param>
      <value>Rabbit Creative</value>
    </param>
    <param>
      <value>http://rabbitcreative.com/</value>
    </param>
  </params>
</methodCall>
EOF
 
url = URI.parse('http://rpc.technorati.com/rpc/ping')
request = Net::HTTP::Post.new(url.path)
request.body = xmlrpc_request
request['User-Agent'] = 'Ruby'
request['Host'] = 'rpc.technorati.com'
request['Content-Type'] = 'text/xml'
request['Content-Length'] = 250
resource = Net::HTTP.new(url.host).start do |http|
  http.request(request)
end
 
document = Hpricot.XML(resource.body)
puts document.search(:string).inner_html

The Hpricot bit isn’t necessary, but I figured I’d throw it in there in case you wanted to make your own interface and wanted to grab the result of the ping.

Oh yeah, the W3C actually has some decent documentation on request headers, and I will mention that the User Agent attribute could be (and probably should be) a little more accurate. From the W3C:

This line if present gives the software program used by the original client. This is for statistical purposes and the tracing of protocol violations. It should be included. The first white space delimited word must be the software product name, with an optional slash and version designator. Other products which form part of the user agent may be put as separate words.

Their example: User-Agent: LII-Cello/1.0 libwww/2.5

So perhaps my User Agent could be… User Agent: Ruby/1.8 Net::HTTP or something like that.

All in all everything I’ve shown you is pretty damn procedural, but it’s meant to shed light on the questions left after reading Technorati’s ping docs and _why’s code. Think of it as a shallow peek into the internals of how Ruby’s XMLRPC library could possibly work.

As always, feel free to desecrate anything here and rebuild; that’s how you learn!