ping.rb
上传用户:netsea168
上传日期:2022-07-22
资源大小:4652k
文件大小:3k
源码类别:

Ajax

开发平台:

Others

  1. require 'rexml/document'
  2. class Ping < ActiveRecord::Base
  3.   belongs_to :article
  4.   class Pinger
  5.     attr_accessor :article
  6.     attr_accessor :blog
  7.     def send_pingback_or_trackback
  8.       begin
  9.         @response = Net::HTTP.get_response(URI.parse(ping.url))
  10.         send_pingback or send_trackback
  11.       rescue Timeout::Error => err
  12.         return
  13.       rescue => err
  14.         raise err
  15.         # Ignore
  16.       end
  17.     end
  18.     def pingback_url
  19.       if response["X-Pingback"]
  20.         response["X-Pingback"]
  21.       elsif response.body =~ /<link rel="pingback" href="([^"]+)" ?/?>/
  22.         $1
  23.       end
  24.     end
  25.     def origin_url
  26.       @origin_url
  27.     end
  28.     def response
  29.       @response
  30.     end
  31.     def ping
  32.       @ping
  33.     end
  34.     def send_xml_rpc(*args)
  35.       ping.send(:send_xml_rpc, *args)
  36.     end
  37.     def trackback_url
  38.       rdfs = response.body.scan(/<rdf:RDF.*?</rdf:RDF>/m)
  39.       rdfs.each do |rdf|
  40.         xml = REXML::Document.new(rdf)
  41.         xml.elements.each("//rdf:Description") do |desc|
  42.           if rdfs.size == 1 || desc.attributes["dc:identifier"] == ping.url
  43.             return desc.attributes["trackback:ping"]
  44.           end
  45.         end
  46.       end
  47.       # Didn't find a trackback url, so fall back to the url itself.
  48.       @ping.url
  49.     end
  50.     def send_pingback
  51.       if pingback_url
  52.         send_xml_rpc(pingback_url, "pingback.ping", origin_url, ping.url)
  53.         return true
  54.       else
  55.         return false
  56.       end
  57.     end
  58.     def send_trackback
  59.       ping.send_trackback(trackback_url, origin_url)
  60.     end
  61.     private
  62.     def initialize(origin_url, ping)
  63.       @origin_url = origin_url
  64.       @ping       = ping
  65.       # Make sure these are fetched now for thread safety purposes.
  66.       self.article = ping.article
  67.       self.blog    = article.blog
  68.     end
  69.   end
  70.   def send_pingback_or_trackback(origin_url)
  71.     Pinger.new(origin_url, self).send_pingback_or_trackback
  72.   end
  73.   def send_trackback(trackback_url, origin_url)
  74.     trackback_uri = URI.parse(trackback_url)
  75.     post = "title=#{CGI.escape(article.title)}"
  76.     post << "&excerpt=#{CGI.escape(article.html(:body).strip_html[0..254])}"
  77.     post << "&url=#{origin_url}"
  78.     post << "&blog_name=#{CGI.escape(article.blog.blog_name)}"
  79.     path = trackback_uri.path
  80.     path += "?#{trackback_uri.query}" if trackback_uri.query
  81.     net_request = Net::HTTP.start(trackback_uri.host, trackback_uri.port) do |http|
  82.       http.post(path, post, 'Content-type' => 'application/x-www-form-urlencoded; charset=utf-8')
  83.     end
  84.   end
  85.   def send_weblogupdatesping(server_url, origin_url)
  86.     send_xml_rpc(self.url, "weblogUpdates.ping", article.blog.blog_name,
  87.                  server_url, origin_url)
  88.   end
  89.   protected
  90.   def send_xml_rpc(xml_rpc_url, name, *args)
  91.     begin
  92.       server = XMLRPC::Client.new2(URI.parse(xml_rpc_url).to_s)
  93.       begin
  94.         result = server.call(name, *args)
  95.       rescue XMLRPC::FaultException => e
  96.         logger.error(e)
  97.       end
  98.     rescue Exception => e
  99.       logger.error(e)
  100.     end
  101.   end
  102. end