<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/xml.xsl" type="text/xsl"?><feed xmlns="http://www.w3.org/2005/Atom">
  <title>nulog, NULL::something : out of the headphone &gt; 2005 &gt; January &gt; 15 &gt; chokan &amp; FOAF</title>
  <link href="http://lowreal.net/logs/2005/01/15/2"/>
  <icon>http://lowreal.net/img/banner.png</icon>
  <link rel="self" type="application/atom+xml" href="http://lowreal.net/logs/2005/01/15/2.atom"/>
  <link rel="alternate" type="application/xhtml+xml" href="http://lowreal.net/logs/2005/01/15/2.xhtml"/>
  <updated>2005-01-15T18:04:08+09:00</updated>
  <author>
    <name>cho45(砂糖)</name>
  </author>
  <id>http://lowreal.net/2005/01/15/2</id>
  <entry>
    <title>chokan &amp; FOAF</title>
    <link rel="alternate" type="text/html" href="http://lowreal.net/logs/2005/01/15/2.html"/>
    <link rel="alternate" type="application/xml+xhtml" href="http://lowreal.net/logs/2005/01/15/2.xhtml"/>
    <updated>2005-01-15T18:04:08+09:00</updated>
    <published>2005-01-15T18:04:08+09:00</published>
    <id>http://lowreal.net/2005/01/15/2</id>
    <category term="rdf"/>
    <category term="sw"/>
    <category term="irc"/>
    <content type="xhtml" xml:base="http://lowreal.net/">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>実験的に <a href="http://raa.ruby-lang.org/project/rena/">Rena</a> を使ってみたかったので、よろしそうなプログラムを考える。</p>
        <p><abbr title="Internet Relay Chat">IRC</abbr> BOT に何か <abbr title="Friend Of A Friend">FOAF</abbr> アレこれできるような機能をつけてみたかった (意味があるかは考えない) ので、とりあえず <abbr title="Uniform Resource Identifer | Universal Resource Identifer">URI</abbr> に反応して、それが <abbr title="Friend Of A Friend">FOAF</abbr> だったら foaf:nick と foaf:name をとってくるようにしてみた。</p>
        <p><abbr title="Uniform Resource Identifer | Universal Resource Identifer">URI</abbr> (http:) が PRIV されたら <del datetime="2005-01-15T20:19:02+09:00">HEAD</del> でアクセスして、ステータスコードと Content-Type を確認。`text/xml', `application/xml', `application/rdf+xml', `text/ntriples' であれば Rena にロードさせる。</p>
        <p>ロードしたら rdf:about="" なリソースを探し、それが foaf:PersonalProfileDocument であれば foaf:primaryTopic のさすリソースの foaf:nick と foaf:name をてけとーに取得して <abbr title="Internet Relay Chat">IRC</abbr> に NOTICE</p>
        <p>Rena は結構遅いので、<abbr title="Resource Description Framework">RDF</abbr> パース中は chokan が他の処理しないかもしれない。</p>
        <p>作るにあたって <a href="http://www.fakeroot.net/sw/rena-intro/">Using Rena to Process RDF in Ruby</a> が役に立った。</p>
        <ins datetime="2005-01-15T20:19:46+09:00">
          <p>よく考えると open-uri に Accept ヘッダを加えるのは無理 (もしくはめんどう) なので、最初から GET して、response.body を StringIO にして Rena に投げることにする。こうしないとネゴシエーション効いてる場合ダメになる。</p>
        </ins>
        <ins datetime="2005-01-15T23:45:47+09:00">
          <p>メイン部分のコード。</p>
          <pre class="Ruby"># uri は読み込んだ RDF の URI の URI クラスのインスタンス
# res は HTTP#get の値
model = Rena::MemModel.new
model.load(StringIO.new(res.body),{
    :content_type =&gt; Regexp.last_match[0],
    :base =&gt; uri.to_s
})
resource = model[uri.to_s] # 相対 URI は絶対 URI に変換されている。
if resource &amp;&amp;
    resource.get_property(RDF + "type").uri == URI.parse(FOAF + "PersonalProfileDocument")
    mes = "foaf:PersonalProfileDocument"

    foaf = resource.get_property(FOAF + "primaryTopic")

    nick = foaf.get_property_values(FOAF + "nick")
    name = foaf.get_property_values(FOAF + "name")

    # string_array は rdf:Alt とかも全部ひっくるめて単一の文字列の配列にする
    mes += " [nick:#{string_array(nick).join(", ")}]" unless nick.empty?
    mes += " [name:#{string_array(name).join(", ")}]" unless name.empty?

    subject &lt;&lt; notice(channel, mes.to_jis)
else
    puts "Not FOAF"
end
</pre>
        </ins>
      </div>
    </content>
  </entry>
</feed>
