【ruby on rails】rubyでトラックバックを受信する。

excerptは256バイトらしいけど、単に全角半角の泣き別れを考えるのが面倒だったので200文字まで受けるようにした。

class TbController < ApplicationController
  def recv
    @error = 0; @massage = 'OK.'
    unless request.post? 
      @error = 1
      @massage = 'Not POST method.'
    end
    
    uri_re = URI.regexp(['http', 'https'])
    if @error == 0 && (uri_re =~ params[:url]) == false
      @error = 1
      @massage = 'Missing TrackBack url.'
    end
    if @error == 0 && params[:title].empty?
      @error = 1
      @massage = 'Missing your entry title.'
    end
    if @error == 0 && params[:excerpt].empty?
      @error = 1
      @massage = 'Missing excerpt.'
    end
    if @error == 0 && params[:blog_name].empty?
      @error = 1
      @massage = 'Missing your blogname.'
    end

    if @error == 0
      log = Log.find(params[:id])
      require 'jstring'
      excerpt = params[:excerpt]
      excerpt = excerpt[0,200] + "..." if excerpt.length > 200
      log.trackbacks.create( :url => params[:url] ,
                             :title       => params[:title] ,
                             :excerpt     => excerpt ,
                             :blog_name   => params[:blog_name] ,
                             :remote_host => request.env["REMOTE_ADDR"] )
      if log.user_id != 0
        Log.update(log.id, :is_trackbacked=>true, :last_updated=>Time.now)
      else
        Log.update(log.id, :last_updated=>Time.now)
      end
    end
    output
  rescue
    logger.error("存在しないログ(#{params[:id]})へのトラックバックです。")
      @error = 1
      @massage = 'Unknown page.'
      output
  end
  
  :private
  def output
  str = '<?xml version="1.0" encoding="iso-8859-1"?>'
  str += '<response>'
  if @error == 0
    str += '<error>0</error>'
  else
    str += '<error>1</error>'
    str += '<message>' + @massage + '</message>'
  end
  str += '</response>'
  @headers["Content-Type"] = 'application/xml; charset=UTF-8'
  render :text => str, :layout => false
  end
end

参考
http://hippos-lab.com/blog/node/56

タグ:[ruby on rails

このログへのコメント(0件)

コメント投稿フォーム

コメント

※半角英数字だけのコメントは投稿できません。

トラックバック

関連ログ