モバイルサイト用のヘッダ出力を Python で書いてみた - Tosshi Note
モバイルサイトを3キャリアでほぼ共通化して作りたい - http://d.hatena.ne.jp/tilfin/20080530/1212376626
上記のエントリで DoCoMo, au, SoftBank 用のモバイルサイトを XHTML で作成する場合のそれぞれの書き方を調べた。
その書き方に従って HTTP ヘッダと XHTML 宣言部分を出力する CGI サンプルを Python で書いてみた。
で書いたスクリプトを今度は Ruby で書いてみました。
require ‘kconv’ class UserAgent def initialize(ua) if ua.nil? @kind = 0 elsif ua.match(/^DoCoMo/) @kind = 1 elsif ua.match(/^KDDI/) @kind = 2 elsif ua.match(/^SoftBank|Vodafone|MOT/) @kind = 3 else @kind = 0 end end def is_mobile return @kind > 0 end def is_docomo return @kind == 1 end def is_au return @kind == 2 end def is_softbank return @kind == 3 end def get_content_type if @kind > 0 return “Content-Type:application/xhtml+xml; charset=Shift_JIS” else return “Content-Type:text/html; charset=UTF-8” end end def get_declare if @kind == 1 return «EOF
EOF elsif @kind == 2 return «EOF
EOF elsif @kind == 3 return «EOF
EOF else return «EOF
EOF end end end agent = UserAgent.new(ENV[‘HTTP_USER_AGENT’]) print agent.get_content_type() + “\r\n\r\n” + agent.get_declare() print ‘’ print “
” file = “content.html” if agent.is_mobile() print Kconv.kconv(File.read(file), Kconv::SJIS) else print File.read(file) end print “”