require 'rubygems'
require 'uri'
require 'net/http'
require 'builder'
module Technorati
def ping(site = {})
pingXml = Builder::XmlMarkup.new()
pingXml.instruct!
pingXml.methodCall{
pingXml.methodName 'weblogUpdates.ping'
pingXml.params{ |ps|
ps.param{ |p| p.value "Brian The Coder"}
ps.param{ |p| p.value "http://brianthecoder.com"}
}
}
url = URI.parse('http://rpc.technorati.com/rpc/ping')
request = Net::HTTP::Post.new(url.path)
request.body = pingXml
request['User-Agent'] = 'Ruby/1.8 Net::HTTP'
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
end
end
Refactorings
No refactoring yet !
Brianthecoder
November 6, 2007, November 06, 2007 02:23, permalink
A little shorter using xmlrpc library. Gonna try to make the ping a separate method to use with other pinging services.
require 'xmlrpc/client'
module Ping
def technorati(site={})
begin
server = XMLRPC::Client.new("rpc.technorati.com","/rpc/ping",80)
begin
result = server.call("weblogUpdates.ping","#{site[:title]}","#{site[:url]}")
rescue XMLRPC::FaultException => e
puts "Error: [#{ e.faultCode }] #{ e.faultString }"
end
rescue Exception => e
puts "Error: #{ e.message }"
end
end
end
This code is made to be in an after_create or after_update call for a post. It pings technorati so your listing is current.