3f91cf60c92b20940674ebdeb46f6582

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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 !

3f91cf60c92b20940674ebdeb46f6582

Brianthecoder

November 6, 2007, November 06, 2007 01:01, permalink

No rating. Login to rate!

No one has any optimizations?

3f91cf60c92b20940674ebdeb46f6582

Brianthecoder

November 6, 2007, November 06, 2007 02:23, permalink

No rating. Login to rate!

A little shorter using xmlrpc library. Gonna try to make the ping a separate method to use with other pinging services.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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

Your refactoring





Format Copy from initial code

or Cancel