2008年6月20日 星期五

未完成的 trackback 功能

本來想實在此功能,但發現 XDite 目前也是需要手動填入 trackback . 所以看到一半放棄,此文紀錄一些查到的資料,跟我實作到一半的步驟.

1. 編寫 send_trackback 的 task ,內容修改自 XDite 範本.

 
# To change this template, choose Tools | Templates
# and open the template in the editor.


require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'

namespace :utils do

# 送出 trackback .
def send_trackback(trackback)
title = "MAPLOG notification" # 主旨.
post_url = "http://maplog.twgg.org/" + trackback.post.id.to_s # 哪篇文章引用.
blog_name = "MAPLOG 地圖碎碎念" # 來源的站名.
content = trackback.post.user.login + " MAPLOG 引用了這篇文章." # 引用內容.
url = trackback.trackback_url
uri = URI.parse(url)
poststring = URI.escape("title=" + title +
"&url=" + post_url +
"&excerpt=" + content +
"&blog_name=" + blog_name)
request = Net::HTTP.new(uri.host, uri.port)
begin
response = request.post(url.gsub(/http:\/\/#{uri.host.to_s}/,''), poststring)
puts response.body
rescue
puts "fail"
end
end

desc "This task will sent trackback"
task :send_trackback => :environment do
puts "sending trackback..."
@trackbacks = Trackback.find(:all, :conditions => "status is NULL")

for trackback in @trackbacks
send_trackback(trackback)
trackback.status = "sent"
trackback.save
end
end

end



2. 檔名存為 rakefile.rake 存至 RailsApp/Libs/taks 下.


3. 在 RailsApp Console 下鍵入 rake -T ,看到最下面出現 utils:send_trackback ,代表新寫的 task 已經加到 task 列表中.


4. 利用 cronjob (定期) 執行 task (執行繁瑣工作).


延伸閱讀:


0 則回應:

Related Posts Plugin for WordPress, Blogger...