XML-RPC ping is one of those great things that allows you to tell web-based services about a change to your content, with very little input from you. In this post I show you how to add RPC Ping for Technorati, but this can be modified to allow you to ping a multitude of services using the specifications laid out on their API Pages.

Ok, here we go..

In the controller where you have your create and update methods (In this case for adding blog posts), add a private method (Remembering that anything after private will be only be available to this class) called rpc_ping.


private

def rpc_ping
server = XMLRPC::Client.new2(“http://rpc.technorati.com/rpc/ping”)
server.call(“weblogUpdates.ping”, “Douglas F Shearer”, “http://douglasfshearer.dyndns.org”)
end


Remember to replace my blog name and url with your own.

Now in each of the methods in which you content changes add a call to our new method.


def update
	post = Post.new(params[:post])
	if post.save
		rpc_ping
	end
end

One final thing. It’s nice to give a bit of feedback, so we’ll pass the result of the RPC call back to the interface using flash. We could format this nicely, but as it’s not going to be seen publicly, it’s enough to pass this back as a plain string for the moment.


def update
	post = Post.new(params[:post])
	if post.save
		result = rpc_ping
		flash[:notice] = 'Ping result: ' + result.to_s
	end
end

That’s it. In the future I’ll write-up how to ping multiple services, once I find a nice clean way to do that. My method above was based on olleolleolle’s Bigbold code snippet. Comments as always are welcome.

Did you like my Ruby on Rails related article? Then why not recommend me on Working with Rails?