<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>

<channel>
	<title>poqu.log</title>
	<atom:link href="http://blog.poqu.org/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.poqu.org</link>
	<description>ポクポクポク...</description>
	<pubDate>Thu, 21 Aug 2008 16:40:24 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5.1</generator>
	<language>ja</language>
			<item>
		<title>Mixi OpenID からニックネームを簡単に取得する</title>
		<link>http://blog.poqu.org/2008/08/22/mixi-openid/</link>
		<comments>http://blog.poqu.org/2008/08/22/mixi-openid/#comments</comments>
		<pubDate>Thu, 21 Aug 2008 16:08:31 +0000</pubDate>
		<dc:creator>kenji</dc:creator>
		
		<category><![CDATA[Rails]]></category>

		<category><![CDATA[ウェブ]]></category>

		<category><![CDATA[OpenID]]></category>

		<category><![CDATA[open_id_authentication]]></category>

		<guid isPermaLink="false">http://blog.poqu.org/?p=76</guid>
		<description><![CDATA[先日、待望の Mixi OpenID がリリースされました。
id:ZIGOROu さんのところで、くわしく解説されていますが、Mixi OpenIDでは、sreg（OpenID Simple Registration  [...]]]></description>
			<content:encoded><![CDATA[<p>先日、待望の Mixi OpenID がリリースされました。</p>
<p><a href="http://d.hatena.ne.jp/ZIGOROu/" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://d.hatena.ne.jp/ZIGOROu/');">id:ZIGOROu</a> さんのところで、くわしく<a href="http://d.hatena.ne.jp/ZIGOROu/20080820/1219218753" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://d.hatena.ne.jp/ZIGOROu/20080820/1219218753');">解説</a>されていますが、Mixi OpenIDでは、sreg（OpenID Simple Registration Extension）と呼ばれる OpenID に付随するユーザのプロフィールをやりとりするためのプロトコルを使って、Mixi で使われるニックネームを取得することができます。</p>
<p>sreg の仕様は、<a href="http://openid.net/specs/openid-simple-registration-extension-1_0.html" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://openid.net/specs/openid-simple-registration-extension-1_0.html');">OpenID Simple Registration Extension 1.0</a>や、<a href="http://d.hatena.ne.jp/ZIGOROu/20070323/1174631045" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://d.hatena.ne.jp/ZIGOROu/20070323/1174631045');">ここ</a>を参照してください。ぼくはこんがらがってしまい、ちゃんと理解できませんでした。。</p>
<p>仕様を完全に理解しなくても（理解するべきでしょうが）、Rails の <a href="http://agilewebdevelopment.com/plugins/openidauthentication" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://agilewebdevelopment.com/plugins/openidauthentication');">Open ID Authentication</a> を使って、簡単にニックネームを取得することができました。</p>
<p>下記は open_id_authentication の README ファイルにあった、sreg 取得方法の例をちょっとだけ変えたものです。open_id_authentication メソッドは、identity_url (ex. https://mixi.jp, http://profile.livedoor.com/****)を引き取り、OP にパラメータを組み立てて認証を行うメソッドのようです。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
</pre></td><td class="code"><pre class="rails"><span style="color:#9966CC; font-weight:bold;">def</span> open_id_authentication<span style="color:#006600; font-weight:bold;">&#40;</span>identity_url<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#008000; font-style:italic;"># Pass optional :required and :optional keys to specify what sreg fields you want.</span>
  <span style="color:#008000; font-style:italic;"># Be sure to yield registration, a third argument in the #authenticate_with_open_id block.</span>
  authenticate_with_open_id<span style="color:#006600; font-weight:bold;">&#40;</span>identity_url, 
      <span style="color:#ff3333; font-weight:bold;">:optional</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:nickname</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>status, identity_url, registration<span style="color:#006600; font-weight:bold;">|</span>
    <span style="color:#9966CC; font-weight:bold;">case</span> status
    <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#ff3333; font-weight:bold;">:missing</span>
      failed_login <span style="color:#996600;">&quot;Sorry, the OpenID server couldn't be found&quot;</span>
    <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#ff3333; font-weight:bold;">:canceled</span>
      failed_login <span style="color:#996600;">&quot;OpenID verification was canceled&quot;</span>
    <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#ff3333; font-weight:bold;">:failed</span>
      failed_login <span style="color:#996600;">&quot;Sorry, the OpenID verification failed&quot;</span>
    <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#ff3333; font-weight:bold;">:successful</span>
      <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0066ff; font-weight:bold;">@current_user</span> = <span style="color:#0066ff; font-weight:bold;">@account</span>.<span style="color:#9900CC;">users</span>.<span style="color:#9900CC;">find_by_identity_url</span><span style="color:#006600; font-weight:bold;">&#40;</span>identity_url<span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#0066ff; font-weight:bold;">@current_user</span>.<span style="color:#9900CC;">nickname</span> = registration<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;nickname&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
&nbsp;
        <span style="color:#9966CC; font-weight:bold;">if</span> current_user.<span style="color:#5A0A0A; font-weight:bold;">save</span>
          successful_login
        <span style="color:#9966CC; font-weight:bold;">else</span>
          failed_login <span style="color:#996600;">&quot;Your OpenID profile registration failed: &quot;</span> <span style="color:#006600; font-weight:bold;">+</span>
            <span style="color:#0066ff; font-weight:bold;">@current_user</span>.<span style="color:#5A0A0A; font-weight:bold;">errors</span>.<span style="color:#9900CC;">full_messages</span>.<span style="color:#9900CC;">to_sentence</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
      <span style="color:#9966CC; font-weight:bold;">else</span>
        failed_login <span style="color:#996600;">&quot;Sorry, no user by that identity URL exists&quot;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>authenticate_with_open_id メソッドに :options => :nickname としているところが（5行目）、sreg を用いてニックネームを取得する宣言です。コメントにあるとおり、sreg を用いる際には、status, identity_url に加え、3つめの変数である regsitration を与える必要があります。</p>
<p>この registration にハッシュ形式で sreg 情報が返ってくるようなので、15行目のように nickname を取り出し、RP である自分のアプリのニックネームとして入れてあげています。</p>
<p>open_id_authenticationプラグインを用いることで簡単に取得することができましたが、もういちど sreg の仕様などを読んで理解を深めたいと思います。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.poqu.org/2008/08/22/mixi-openid/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Neo Classics 2 が出る模様</title>
		<link>http://blog.poqu.org/2008/08/19/neo-classics-2/</link>
		<comments>http://blog.poqu.org/2008/08/19/neo-classics-2/#comments</comments>
		<pubDate>Mon, 18 Aug 2008 16:26:54 +0000</pubDate>
		<dc:creator>kenji</dc:creator>
		
		<category><![CDATA[音楽]]></category>

		<guid isPermaLink="false">http://blog.poqu.org/?p=74</guid>
		<description><![CDATA[
長年（10年以上）聞き続けてるDJ クボタタケシさんのミックスCD「Neo Classics 2」がどうやら9/24に出るようです。
CD版である本「Neo Classics」シリーズも趣きがあってよいですが、ミックス [...]]]></description>
			<content:encoded><![CDATA[<div style="float:left;margin-right:5px;"><img src="http://blog.poqu.org/wp-content/uploads/2008/08/41trettrqal_sl500_aa240_.jpg" alt="" title="41trettrqal_sl500_aa240_" width="240" height="240" class="alignnone size-full wp-image-75" /></div>
<p>長年（10年以上）聞き続けてるDJ クボタタケシさんのミックスCD「<a href="http://www.amazon.co.jp/%E3%82%AF%E3%83%9C%E3%82%BF%E3%82%BF%E3%82%B1%E3%82%B7MIX-CD%E2%80%9CNEO-CLASSICS2%E2%80%9D-%E3%82%AA%E3%83%A0%E3%83%8B%E3%83%90%E3%82%B9/dp/B001CCHINW" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://www.amazon.co.jp/%E3%82%AF%E3%83%9C%E3%82%BF%E3%82%BF%E3%82%B1%E3%82%B7MIX-CD%E2%80%9CNEO-CLASSICS2%E2%80%9D-%E3%82%AA%E3%83%A0%E3%83%8B%E3%83%90%E3%82%B9/dp/B001CCHINW');">Neo Classics 2</a>」がどうやら9/24に出るようです。</p>
<p>CD版である本「Neo Classics」シリーズも趣きがあってよいですが、ミックステープ「Classics」シリーズの方が権利処理の関係？で比較的自由にやれている分とてもおもしろいので、テープで復活してくれるととても嬉しいなあ。<br />
いまもこれを書きながら「Classics Vol.1」を聞いているのですが、「I&#8217;m in The Mood For Dancing」からレザボア・ドッグスのオープニングテーマ「Little Green Bag」への流れとか嬉しくなります。</p>
<p>最近は現場に行っていないのでどうも言えないのですが、平日のMIXの時のような実験をする場があるのでしょうか。しばらく足を運んでいないので実験日にぜひ行ってみたいと思います。</p>
<div style="clear:both;"></div>
]]></content:encoded>
			<wfw:commentRss>http://blog.poqu.org/2008/08/19/neo-classics-2/feed/</wfw:commentRss>
		</item>
		<item>
		<title>[Edge Rails] join時のcondtionsをわかりやすく : Easy Join Table Conditions</title>
		<link>http://blog.poqu.org/2008/08/03/edge-rails-easy-join-table-conditions/</link>
		<comments>http://blog.poqu.org/2008/08/03/edge-rails-easy-join-table-conditions/#comments</comments>
		<pubDate>Sun, 03 Aug 2008 09:03:23 +0000</pubDate>
		<dc:creator>kenji</dc:creator>
		
		<category><![CDATA[Edge Rails]]></category>

		<category><![CDATA[ActiveRecord]]></category>

		<category><![CDATA[joins]]></category>

		<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://blog.poqu.org/?p=73</guid>
		<description><![CDATA[追記: すみません、join時だけでなく通常のfindでも使えるようです。
関連するモデルからデータを引くときに、↓のように:joinsオプションを使うことはよくあります。

1
2
3
4
5
6
7
8
9
10
1 [...]]]></description>
			<content:encoded><![CDATA[<p><em>追記: すみません、join時だけでなく通常のfindでも使えるようです。</em></p>
<p>関連するモデルからデータを引くときに、↓のように:joinsオプションを使うことはよくあります。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="rails"><span style="color:#9966CC; font-weight:bold;">class</span> Article <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  <span style="color:#5A0A0A; font-weight:bold;">belongs_to</span> <span style="color:#ff3333; font-weight:bold;">:user</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> User <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  <span style="color:#5A0A0A; font-weight:bold;">has_many</span> <span style="color:#ff3333; font-weight:bold;">:articles</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Get all the users that have published articles</span>
User.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:all</span>, <span style="color:#ff3333; font-weight:bold;">:joins</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:article</span>,
  <span style="color:#ff3333; font-weight:bold;">:conditions</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;articles.published = ?&quot;</span>, <span style="color:#0000FF; font-weight:bold;">true</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></td></tr></table></div>

<p><a href="http://github.com/rails/rails/commit/cd994eff9a343df376bfaec59de5b24a2ab51256" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://github.com/rails/rails/commit/cd994eff9a343df376bfaec59de5b24a2ab51256');">今回のコミット</a>では、join時での:conditionsオプションをハッシュで指定できるようになりました。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="rails"><span style="color:#008000; font-style:italic;"># Get all the users that have published articles</span>
User.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:all</span>, <span style="color:#ff3333; font-weight:bold;">:joins</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:article</span>,
  <span style="color:#ff3333; font-weight:bold;">:conditions</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:articles</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:published</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span> <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></td></tr></table></div>

<p>綺麗に書けますが、これ使うかなあ。。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.poqu.org/2008/08/03/edge-rails-easy-join-table-conditions/feed/</wfw:commentRss>
		</item>
		<item>
		<title>[Edge Rails] 入力文字数の長さを定義する : Custom Length Validation Tokenizer</title>
		<link>http://blog.poqu.org/2008/08/03/edge-rails-custom-length-validation-tokenizer/</link>
		<comments>http://blog.poqu.org/2008/08/03/edge-rails-custom-length-validation-tokenizer/#comments</comments>
		<pubDate>Sun, 03 Aug 2008 08:25:05 +0000</pubDate>
		<dc:creator>kenji</dc:creator>
		
		<category><![CDATA[Edge Rails]]></category>

		<category><![CDATA[Rails]]></category>

		<category><![CDATA[tokenizer]]></category>

		<category><![CDATA[validates_length_of]]></category>

		<guid isPermaLink="false">http://blog.poqu.org/?p=72</guid>
		<description><![CDATA[validates_length_of では、:minimum, :maximumなどのオプションによって入力最小/最大文字数を定義することができます。いままでは単純に文字数のカウントを行っているだけでしたが、今回追加さ [...]]]></description>
			<content:encoded><![CDATA[<p>validates_length_of では、:minimum, :maximumなどのオプションによって入力最小/最大文字数を定義することができます。いままでは単純に文字数のカウントを行っているだけでしたが、今回追加された :tokenizer オプションによって、文字のカウント方法を定義できるようになりました。</p>
<p>たとえば下記の場合、3行目で入力文字が何ワードあるかを調べるように定義し、10ワード以下であればエラーになるようになっています。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="rails"><span style="color:#5A0A0A; font-weight:bold;">validates_length_of</span> <span style="color:#ff3333; font-weight:bold;">:article</span>, <span style="color:#ff3333; font-weight:bold;">:minimum</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">10</span>,
  <span style="color:#ff3333; font-weight:bold;">:too_short</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;Your article must be at least %d words in length.&quot;</span>,
  <span style="color:#ff3333; font-weight:bold;">:tokenizer</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#CC0066; font-weight:bold;">lambda</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>str<span style="color:#006600; font-weight:bold;">|</span> str.<span style="color:#9900CC;">scan</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>\w<span style="color:#006600; font-weight:bold;">+/</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.poqu.org/2008/08/03/edge-rails-custom-length-validation-tokenizer/feed/</wfw:commentRss>
		</item>
		<item>
		<title>[Edge Rails] partialのcollection時に好きな変数名をつける：Collection Partial Variable Naming</title>
		<link>http://blog.poqu.org/2008/07/29/edge-rails-collection-partial-variable-naming/</link>
		<comments>http://blog.poqu.org/2008/07/29/edge-rails-collection-partial-variable-naming/#comments</comments>
		<pubDate>Mon, 28 Jul 2008 16:28:00 +0000</pubDate>
		<dc:creator>kenji</dc:creator>
		
		<category><![CDATA[Edge Rails]]></category>

		<category><![CDATA[Rails]]></category>

		<category><![CDATA[collection]]></category>

		<category><![CDATA[partial]]></category>

		<category><![CDATA[render]]></category>

		<guid isPermaLink="false">http://blog.poqu.org/?p=71</guid>
		<description><![CDATA[# またまた遅れてごめんなさい。。。
今回の機能は、render :partial で :collection を使った場合に好きな変数名を渡せるものです。
恥ずかしながらいままで collection を知らなかったの [...]]]></description>
			<content:encoded><![CDATA[<p># またまた遅れてごめんなさい。。。</p>
<p>今回の機能は、render :partial で :collection を使った場合に好きな変数名を渡せるものです。<br />
恥ずかしながらいままで collection を知らなかったので、自分のためにもそのおさらいから。</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">render <span style="color:#ff3333; font-weight:bold;">:partial</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'employees'</span>, <span style="color:#ff3333; font-weight:bold;">:collection</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0066ff; font-weight:bold;">@workers</span></pre></div></div>

<p>Viewで render partial で外部テンプレートをよく呼び出しまが、collectionはその外部テンプレートをループさせるときに利用するものです。<br />
上記の場合、下の　_employees.html.erb が @workersの要素（workerひとりひとり）を引き取って、ぐるぐるとまわります。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre>名前：&lt;%= woker.name %&gt;
年齢：&lt;%= worker.age %&gt;</pre></td></tr></table></div>

<p>collectionは複数形で渡しておくと、テンプレート先で単数形で自動的に引き取るようです。これだと変数名に制約が出てくるので、今回の機能である、:asで変数名を指定することができます。</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">render <span style="color:#ff3333; font-weight:bold;">:partial</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;employees&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:collection</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0066ff; font-weight:bold;">@workers</span>, <span style="color:#ff3333; font-weight:bold;">:as</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:person</span></pre></div></div>

<p>上記のようにすると、workersの要素ひとつひとつがpersonとしてemployeesテンプレートで展開することができます。</p>
<p>今回の機能、というよりも　collection を知って嬉しくなりましたが、<a href="http://www.kaeruspoon.net/articles/271" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://www.kaeruspoon.net/articles/271');">表示は遅いよう</a>ですね。。。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.poqu.org/2008/07/29/edge-rails-collection-partial-variable-naming/feed/</wfw:commentRss>
		</item>
		<item>
		<title>iPhone へアドレスブックの内容を整形してインポートする</title>
		<link>http://blog.poqu.org/2008/07/22/iphone-address-book/</link>
		<comments>http://blog.poqu.org/2008/07/22/iphone-address-book/#comments</comments>
		<pubDate>Tue, 22 Jul 2008 14:34:35 +0000</pubDate>
		<dc:creator>kenji</dc:creator>
		
		<category><![CDATA[ウェブ]]></category>

		<category><![CDATA[iPhone]]></category>

		<guid isPermaLink="false">http://blog.poqu.org/?p=69</guid>
		<description><![CDATA[iPhoneのような携帯端末で GPS を使ったアプリを作ってみたかったので、テスト端末と自分に言い聞かせて購入に踏み切りました。買ったはいいのですが、アドレスデータの移行がうまいこといかず、ちょっと手を入れたのでメモし [...]]]></description>
			<content:encoded><![CDATA[<p>iPhoneのような携帯端末で GPS を使ったアプリを作ってみたかったので、テスト端末と自分に言い聞かせて購入に踏み切りました。買ったはいいのですが、アドレスデータの移行がうまいこといかず、ちょっと手を入れたのでメモしておきます。<br />
<img src="http://blog.poqu.org/wp-content/uploads/2008/07/iphone.gif" alt="" title="iphone" width="568px" height="" class="aligncenter size-full wp-image-70" /></p>
<h3>携帯からアドレスをインポートしたものの、データがうまくない</h3>
<p>携帯（MediaSkin）からアドレスのデータをエクスポートしたところ、vcfフォーマットのデータが生成されました。このファイルにはすこし問題があって、</p>
<ul>
<li>姓名がわかれてなかったり</li>
<li>フリガナがついてなかったり</li>
</ul>
<p>します。</p>
<p>実際、姓名はMediaSkinに分けて入れるフィールドがなかったためなのですが、フリガナについては入っているものの、インポート先のアドレスブック（Mac）で認識できませんでした。</p>
<h3>手順</h3>
<p>手順というほどのことではないのですが、移行は下記の手順で行いました。通常ならば3. は必要ないのかもしれませんが、今回「姓名を分ける」「フリガナをつける」に対応するため、スクリプトを書いています。</p>
<ol>
<li>既存の携帯から miniSD や USBケーブルなどでデータを引き抜く</li>
<li>抜いた.vcfファイルを UTF-16 に変換</li>
<li>自作スクリプトで姓名を分割＋フリガナの追加</li>
<li>アドレスブック（Mac）へインポート</li>
<li>iTunes でシンク</li>
</ol>
<h3></h3>
<p>.vcfはよくわからなかったのですが、下のような感じで BEGIN:VCARD ~ END:VCARD までで一行ずつアドレスデータを記述していくようです（ぼくの携帯からはCHARSET=SHIFT_JISという設定が入っていますが、これは必要ありませんでした）。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code"><pre>BEGIN:VCARD
VERSION:3.0
N;CHARSET=SHIFT_JIS:姓 名;;;;
FN;CHARSET=SHIFT_JIS:姓 名
SORT-STRING;CHARSET=SHIFT_JIS:ｾｲﾒｲ
X-PHONETIC-FIRST-NAME:
X-PHONETIC-LAST-NAME:
TEL;TYPE=PREF,CELL:080********
TEL;TYPE=VOICE:03********
EMAIL;TYPE=PREF,CELL:sei_mei@hoge-mobile.com
EMAIL;TYPE=PCS:sei_mei@hoge-pc.com
CLASS:PUBLIC
BDAY:********
X-CONSTELLATION:
REV:
X-REDUCTION:
X-GNO:
X-GN;CHARSET=SHIFT_JIS:ｸﾞﾙｰﾌﾟ設定なし
END:VCARD</pre></td></tr></table></div>

<h4>姓名をわける</h4>
<p>上の３行目「N;CHARSET=SHIFT_JIS:姓 名;;;;」の箇所で指定することが出来ます。このままだと「姓 名」が名字になってしまいますが、下記のように「;」で姓名をわけてあげることで、ちゃんと認識してくれます（CHARSET~は取ってしまっても問題ありません）。<br />
ぼくの場合、携帯の電話帳に登録するときに姓名を半角スペースでわけていたので、わけるのは簡単でした。。</p>

<div class="wp_syntax"><div class="code"><pre>N;姓;名;;;</pre></div></div>

<h4>フリガナを振る</h4>
<p>上の６，７行目「X-PHONETIC ~」に指定することでちゃんと認識します。携帯から出力したときにはここの行がなかったため、スクリプトで追加しました。フリガナは５行目から持ってきたかったのですが、どこからが姓で名なのかsplitのしようがないので、登録されている漢字から、Yahoo! の <a href="http://developer.yahoo.co.jp/jlp/FuriganaService/V1/furigana.html" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://developer.yahoo.co.jp/jlp/FuriganaService/V1/furigana.html');">ルビ振りWebサービス</a> を使って登録しました。</p>

<div class="wp_syntax"><div class="code"><pre>X-PHONETIC-FIRST-NAME:めい
X-PHONETIC-LAST-NAME:せい</pre></div></div>

<p>（ルビ振りAPI は 光一（こういち）を「ぴかいち」と読むなど、あんまりなところもありましたが、自分で書いていくよりよいので、スクリプトでグルグルまわして取得しました。）</p>
<div style="border-bottom: 1px solid silver"></div>
<p>これでアドレスブックにすっきりインポートすることができました。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.poqu.org/2008/07/22/iphone-address-book/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Ruby Enterprise Edition + Passenger2.0 RC1 でRailsを高速化?</title>
		<link>http://blog.poqu.org/2008/06/15/ruby-enterprise-edition-passenger20/</link>
		<comments>http://blog.poqu.org/2008/06/15/ruby-enterprise-edition-passenger20/#comments</comments>
		<pubDate>Sun, 15 Jun 2008 04:59:50 +0000</pubDate>
		<dc:creator>kenji</dc:creator>
		
		<category><![CDATA[Rails]]></category>

		<category><![CDATA[Ruby]]></category>

		<category><![CDATA[mod_rails]]></category>

		<category><![CDATA[Passenger]]></category>

		<category><![CDATA[Ruby Enterprise Edition]]></category>

		<guid isPermaLink="false">http://blog.poqu.org/?p=68</guid>
		<description><![CDATA[mod_railsで有名なオランダのPhusion社から Ruby Enterprise Edition がリリースされたので、早速Passenger(mod_rails)と組み合わせて試してみました。
Ruby Ent [...]]]></description>
			<content:encoded><![CDATA[<p>mod_railsで有名なオランダの<a href="http://www.phusion.nl/index.html" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://www.phusion.nl/index.html');">Phusion</a>社から <a href="http://www.rubyenterpriseedition.com/" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://www.rubyenterpriseedition.com/');">Ruby Enterprise Edition</a> がリリースされたので、早速<a href="http://www.modrails.com/" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://www.modrails.com/');">Passenger</a>(mod_rails)と組み合わせて試してみました。</p>
<p>Ruby Enterprise Edition は、Rails に最適化されたRubyのようです（An enhanced garbage collector. An improved memory allocator. ）。</p>
<p>インストールはいたって簡単で、指定したディレクトリに（gemも）すべて展開されるため、既存のRubyとの併用も可能です。またアンインストールしたければそのディレクトリを削除すればいいだけです。</p>
<h3>Ruby Enterprise Edition のインストール</h3>
<p>Ruby Enterprise EdtionはPassengerとの組み合わせでメモリ使用量を33%減にできるようですので、ちょっと期待して使ってみました。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="bash"><span style="color: #808080; font-style: italic;"># wget http://rubyforge.org/frs/download.php/38084/ruby-enterprise-1.8.6-20080507.tar.gz</span>
<span style="color: #808080; font-style: italic;"># tar zxvf ruby-enterprise-1.8.6-20080507.tar.gz</span>
<span style="color: #808080; font-style: italic;"># ./installer</span>
&nbsp;
......
&nbsp;
Where would you like to <span style="color: #c20cb9; font-weight: bold;">install</span> Ruby Enterprise Edition to?
<span style="color: #7a0874; font-weight: bold;">&#40;</span>All Ruby Enterprise Edition files will be put inside that directory.<span style="color: #7a0874; font-weight: bold;">&#41;</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span><span style="color: #7a0874; font-weight: bold;">local</span><span style="color: #000000; font-weight: bold;">/</span>ruby-enterprise<span style="color: #000000; font-weight: bold;">/</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> :インストールするディレクトリを指定</pre></td></tr></table></div>

<p>installerを起動すると、どこに設置するのか(line.10)聞かれてインストールが始まります。途中でRailsもインストールされます（すこし時間がかかりました）。</p>
<h3>Passenger(mod_rails) 2.0 RC1 のインストール</h3>
<p><a href="http://blog.phusion.nl/2008/06/09/phusion-passenger-20-rc-1-and-ruby-enterprise-edition-released/" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://blog.phusion.nl/2008/06/09/phusion-passenger-20-rc-1-and-ruby-enterprise-edition-released/');">ここ</a>を参考に。</p>
<h4>gemでインストール</h4>
<p>2.0RC1はpassenger-1.9.0となっているようです。まずは<a href="http://phusion-passenger.googlecode.com/files/passenger-1.9.0.gem" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://phusion-passenger.googlecode.com/files/passenger-1.9.0.gem');">ここ</a>からダウンロードしておきます。下記のようにさきほどインストールしたRuby Enterprise Editionを利用してgemでインストール。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="bash"><span style="color: #808080; font-style: italic;"># cd /usr/local/ruby-enterprise/</span>
<span style="color: #808080; font-style: italic;"># ./bin/ruby ./bin/gem install passenger-1.9.0.gem</span></pre></td></tr></table></div>

<p>これでRuby Enterprise Editionディレクトリ内にpassengerが配置されます。</p>
<h4>Apacheの設定</h4>
<p>次にRuby Enterprise Editionディレクトリ内のbin配下にpassenger-install-apache2-moduleというスクリプトを叩きます。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="bash"><span style="color: #808080; font-style: italic;"># cd /usr/local/ruby-enterprise/</span>
<span style="color: #808080; font-style: italic;"># ./bin/passenger-install-apache2-module</span></pre></td></tr></table></div>

<p>ずらずらとメッセージが出てきて、httpd.confへの編集を促されるので、下記をhttpd.confなどに追記します。Ruby Enterprise Editionをインストールしたディレクトリによってパスは変わるので注意してください。また、デフォルトでproductionとして起動しますが、もしdevelopmentで起動したいなら『 RailsEnv development』を追記してください（<a href="http://www.modrails.com/documentation/Users%20guide.html#_configuring_passenger" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://www.modrails.com/documentation/Users%20guide.html#_configuring_passenger');">Configuring Passenger</a>を参照）。</p>

<div class="wp_syntax"><div class="code"><pre>LoadModule passenger_module /usr/local/ruby-enterprise/lib/ruby/gems/1.8/gems/passenger-1.9.0/ext/apache2/mod_passenger.so
PassengerRoot /usr/local/ruby-enterprise/lib/ruby/gems/1.8/gems/passenger-1.9.0
PassengerRuby /usr/local/ruby-enterprise/bin/ruby</pre></div></div>

<p>ヴァーチャルホストの設定は下記の通りです。DocumentRootはRailsアプリのpublicディレクトリを指定します。</p>

<div class="wp_syntax"><div class="code"><pre>&lt;VirtualHost *:80&gt;
      ServerName www.yourhost.com
      DocumentRoot /somewhere/public
&lt;/VirtualHost&gt;</pre></div></div>

<h3>起動</h3>
<p>これで設定は完了なはずです。Apacheを再起動して設定ファイルを反映して、アクセスしてみます。productionだとエラーがわかりづらいので、最初はdevelopmentで起動した方がいいのかもしれません。</p>
<p>ぼくの環境の場合、普段使っているgemに入っているものが、Ruby Enterprise Editionのgemに入っていなかったため、エラーが起きてしまいました。その場合は、こっちのgemで再インストールを行います。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="bash"><span style="color: #808080; font-style: italic;"># cd /usr/local/ruby-enterpriise</span>
<span style="color: #808080; font-style: italic;"># ./bin/ruby ./bin/gem install &lt;パッケージ&gt;</span></pre></td></tr></table></div>

<h3>メモリ使用量の比較</h3>
<p>scaffoldしてから、ちょっと手を加えた程度の小さいアプリを動かしてみたところ、140Mくらい利用しているみたいです。増井さんの<a href="http://blog.masuidrive.jp/index.php/2008/04/14/mod_rails_uses_triple_memory_than_mongrel/" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://blog.masuidrive.jp/index.php/2008/04/14/mod_rails_uses_triple_memory_than_mongrel/');">記事</a> （素Ruby + Passenger 1.x系）とあまり変わらない。。。うーむ、設定を間違えたのでしょうか。。アプリも環境も異なるので、後ほどmongrelなどと比較してみたいと思います。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.poqu.org/2008/06/15/ruby-enterprise-edition-passenger20/feed/</wfw:commentRss>
		</item>
		<item>
		<title>フレキシブルなto_xml</title>
		<link>http://blog.poqu.org/2008/06/10/to_xml/</link>
		<comments>http://blog.poqu.org/2008/06/10/to_xml/#comments</comments>
		<pubDate>Tue, 10 Jun 2008 12:27:36 +0000</pubDate>
		<dc:creator>kenji</dc:creator>
		
		<category><![CDATA[Edge Rails]]></category>

		<category><![CDATA[Rails]]></category>

		<category><![CDATA[to_xml]]></category>

		<guid isPermaLink="false">http://blog.poqu.org/2008/06/10/67/</guid>
		<description><![CDATA[Rails2.0でscaffoldを行うと、HTMLと同じURL+.xmlでXMLの取得も行えます。リソースひとつだけ取得するのであればこのままでも大丈夫なのですが、たとえばuserとblogが1:nの関係で、userのXMLを取得する時に同時に複数のblogも取得したい場合どのようにすればよいのかわかりませんでした。
to_xmlを使わずテンプレートでがんばろうかなと思っていたところ、１年前のRyan&#8217;s Scrapsでまさにそのものな:includeオプションを見つけました。

1
2
3
4
5
6
7
@user = User.find&#40;1&#41;
@out = @user.to_xml&#40;:include =&#62; :blogs&#41;
&#160;
respond_to do &#124;format&#124;
    format.html # show.html.erb
    format.xml  &#123; render :xml =&#62; @out &#125;
end

scaffold後に↑のようなソースになりますが、2行目でto_xmlのオプションに:includeを付与しておくと、

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
&#60;?xml version=&#34;1.0&#34; encoding=&#34;UTF-8&#34;?&#62;
&#60;user&#62;
  &#60;id type=&#34;integer&#34;&#62;2&#60;/id&#62;
  &#60;login&#62;ホゲ&#60;/login&#62;
&#160;
  ~ 中略 ~
&#160;
  &#60;blogs type=&#34;array&#34;&#62;
    &#60;blog&#62;
      &#60;created-at type=&#34;datetime&#34;&#62;2008-06-10T15:06:14+09:00&#60;/created-at&#62;
    [...]]]></description>
			<content:encoded><![CDATA[<p>Rails2.0でscaffoldを行うと、HTMLと同じURL+.xmlでXMLの取得も行えます。リソースひとつだけ取得するのであればこのままでも大丈夫なのですが、たとえばuserとblogが1:nの関係で、userのXMLを取得する時に同時に複数のblogも取得したい場合どのようにすればよいのかわかりませんでした。</p>
<p>to_xmlを使わずテンプレートでがんばろうかなと思っていたところ、１年前の<a href="http://ryandaigle.com/articles/2007/4/13/what-s-new-in-edge-rails-a-more-flexible-to_xml" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://ryandaigle.com/articles/2007/4/13/what-s-new-in-edge-rails-a-more-flexible-to_xml');">Ryan&#8217;s Scraps</a>でまさにそのものな:includeオプションを見つけました。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="rails"><span style="color:#0066ff; font-weight:bold;">@user</span> = User.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#0066ff; font-weight:bold;">@out</span> = <span style="color:#0066ff; font-weight:bold;">@user</span>.<span style="color:#5A0A0A; font-weight:bold;">to_xml</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:include</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:blogs</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
<span style="color:#5A0A0A; font-weight:bold;">respond_to</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>format<span style="color:#006600; font-weight:bold;">|</span>
    <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">html</span> <span style="color:#008000; font-style:italic;"># show.html.erb</span>
    <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">xml</span>  <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#5A0A0A; font-weight:bold;">render</span> <span style="color:#ff3333; font-weight:bold;">:xml</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0066ff; font-weight:bold;">@out</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>scaffold後に↑のようなソースになりますが、2行目でto_xmlのオプションに:includeを付与しておくと、</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
</pre></td><td class="code"><pre class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="font-weight: bold; color: black;">?&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;user<span style="font-weight: bold; color: black;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;id</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;integer&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>2<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/id<span style="font-weight: bold; color: black;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;login<span style="font-weight: bold; color: black;">&gt;</span></span></span>ホゲ<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/login<span style="font-weight: bold; color: black;">&gt;</span></span></span>
&nbsp;
  ~ 中略 ~
&nbsp;
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;blogs</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;array&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>
    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;blog<span style="font-weight: bold; color: black;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;created-at</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;datetime&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>2008-06-10T15:06:14+09:00<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/created-at<span style="font-weight: bold; color: black;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;id</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;integer&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>1<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/id<span style="font-weight: bold; color: black;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;user-id</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;integer&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>2<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/user-id<span style="font-weight: bold; color: black;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;title<span style="font-weight: bold; color: black;">&gt;</span></span></span>ホゲブログ1<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/title<span style="font-weight: bold; color: black;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;updated-at</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;datetime&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>2008-06-10T15:06:14+09:00<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/updated-at<span style="font-weight: bold; color: black;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/blog<span style="font-weight: bold; color: black;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;blog<span style="font-weight: bold; color: black;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;created-at</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;datetime&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>2008-06-10T15:36:23+09:00<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/created-at<span style="font-weight: bold; color: black;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;id</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;integer&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>5<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/id<span style="font-weight: bold; color: black;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;user-id</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;integer&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>2<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/user-id<span style="font-weight: bold; color: black;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;title<span style="font-weight: bold; color: black;">&gt;</span></span></span>ホゲブログ2<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/title<span style="font-weight: bold; color: black;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;updated-at</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;datetime&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>2008-06-10T15:36:23+09:00<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/updated-at<span style="font-weight: bold; color: black;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/blog<span style="font-weight: bold; color: black;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;blogs</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/user<span style="font-weight: bold; color: black;">&gt;</span></span></span></pre></td></tr></table></div>

<p>のように、userの子要素として blogs > blog が追加されました。</p>
<p>そのほか to_xml には :include 以外にも便利なオプションがあります。</p>
<p>たとえば不必要な項目を除外する:except、指定項目だけを表示する:only。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="rails">user = User.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
user.<span style="color:#5A0A0A; font-weight:bold;">to_xml</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:except</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:id</span>, <span style="color:#ff3333; font-weight:bold;">:created_at</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#008000; font-style:italic;">#=&gt;</span>
<span style="color:#008000; font-style:italic;"># &lt;user&gt;</span>
<span style="color:#008000; font-style:italic;">#   &lt;name&gt;Ryan&lt;/name&gt;</span>
<span style="color:#008000; font-style:italic;">#   &lt;email&gt;ryan@spamme.com&lt;/email&gt;</span>
<span style="color:#008000; font-style:italic;"># &lt;/user&gt;</span>
&nbsp;
user.<span style="color:#5A0A0A; font-weight:bold;">to_xml</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:only</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:email</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#008000; font-style:italic;">#=&gt;</span>
<span style="color:#008000; font-style:italic;"># &lt;user&gt;</span>
<span style="color:#008000; font-style:italic;">#   &lt;email&gt;ryan@spamme.com&lt;/email&gt;</span>
<span style="color:#008000; font-style:italic;"># &lt;/user&gt;</span></pre></td></tr></table></div>

<p>また、下記のようにXMLにエレメントを追加することもできます。この例ではidとcreated_atを除外して、serialize_versionを1.1として追加しています。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="rails">user.<span style="color:#5A0A0A; font-weight:bold;">to_xml</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:except</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:id</span>, <span style="color:#ff3333; font-weight:bold;">:created_at</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>xml<span style="color:#006600; font-weight:bold;">|</span>
  xml.<span style="color:#9900CC;">serialize_version</span> <span style="color:#006666;">1.1</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#008000; font-style:italic;">#=&gt;</span>
<span style="color:#008000; font-style:italic;"># &lt;user&gt;</span>
<span style="color:#008000; font-style:italic;">#   &lt;name&gt;Ryan&lt;/name&gt;</span>
<span style="color:#008000; font-style:italic;">#   &lt;email&gt;ryan@spamme.com&lt;/email&gt;</span>
<span style="color:#008000; font-style:italic;">#   &lt;serialize_version&gt;1.1&lt;/serialize_version&gt;</span>
<span style="color:#008000; font-style:italic;"># &lt;/user&gt;</span></pre></td></tr></table></div>

<p>あー便利だなあ。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.poqu.org/2008/06/10/to_xml/feed/</wfw:commentRss>
		</item>
		<item>
		<title>[Edge Rails] DBコンソールを楽に起動：DB Console</title>
		<link>http://blog.poqu.org/2008/05/28/db-console/</link>
		<comments>http://blog.poqu.org/2008/05/28/db-console/#comments</comments>
		<pubDate>Tue, 27 May 2008 15:04:52 +0000</pubDate>
		<dc:creator>kenji</dc:creator>
		
		<category><![CDATA[Edge Rails]]></category>

		<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://blog.poqu.org/?p=66</guid>
		<description><![CDATA[Rails 2.0.91 が使えるようになり、2.1 リリースも間近なようです（2.0.91などマイナーアップデートのものを利用したい場合はこちらを参照してください）。
今回の Edge Rails は、2.0.91 よ [...]]]></description>
			<content:encoded><![CDATA[<p>Rails 2.0.91 が使えるようになり、2.1 リリースも間近なようです（2.0.91などマイナーアップデートのものを利用したい場合は<a href="http://blog.poqu.org/2008/04/28/rails-2-0-2-9126/" >こちらを参照</a>してください）。</p>
<p>今回の Edge Rails は、2.0.91 より使えるようになった DB Console です。<br />
これは Rails プロジェクト配下のscript/にあるスクリプトで、使っているDBのコンソールを起動することができます。IDやPWなどの設定情報は、Railsプロジェクトと同じconfig/database.ymlのものが使われます。</p>

<div class="wp_syntax"><div class="code"><pre class="bash">$ script<span style="color: #000000; font-weight: bold;">/</span>dbconsole
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection <span style="color: #c20cb9; font-weight: bold;">id</span> is <span style="color: #000000;">1</span>
Server version: <span style="color: #000000;">5.0</span>.51a Source distribution
&nbsp;
Type <span style="color: #ff0000;">'help;'</span> or <span style="color: #ff0000;">'<span style="color: #000099; font-weight: bold;">\h</span>'</span> <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #7a0874; font-weight: bold;">help</span>. Type <span style="color: #ff0000;">'<span style="color: #000099; font-weight: bold;">\c</span>'</span> to <span style="color: #c20cb9; font-weight: bold;">clear</span> the buffer.
&nbsp;
mysql<span style="color: #000000; font-weight: bold;">&gt;</span></pre></div></div>

<p>これはMySQLでの例ですが、使っているDBがSQLiteならばSQLiteのコンソールが起動します。</p>
<p>複数のアプリでユーザなどを使い分けている人には特に便利な機能です。</p>
<p># Ryan&#8217;s Scraps の更新を見逃していました。。。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.poqu.org/2008/05/28/db-console/feed/</wfw:commentRss>
		</item>
		<item>
		<title>RESTfulなRailsをtelnetで試す</title>
		<link>http://blog.poqu.org/2008/05/15/restful-rails-telnet/</link>
		<comments>http://blog.poqu.org/2008/05/15/restful-rails-telnet/#comments</comments>
		<pubDate>Wed, 14 May 2008 15:11:25 +0000</pubDate>
		<dc:creator>kenji</dc:creator>
		
		<category><![CDATA[Rails]]></category>

		<category><![CDATA[REST]]></category>

		<category><![CDATA[telnet]]></category>

		<guid isPermaLink="false">http://blog.poqu.org/?p=64</guid>
		<description><![CDATA[いまさらですが必要になったので、Rails2.0 の scaffold で、RESTfulなアプリを試してみました。確認に手間取ったのでメモ。
RESTfulなアプリを作成
いつもどおり scaffold で、アプリを作 [...]]]></description>
			<content:encoded><![CDATA[<p>いまさらですが必要になったので、Rails2.0 の scaffold で、RESTfulなアプリを試してみました。確認に手間取ったのでメモ。</p>
<h3>RESTfulなアプリを作成</h3>
<p>いつもどおり scaffold で、アプリを作成します。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="bash">$ rails rest
$ <span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #7a0874; font-weight: bold;">test</span>
$ script<span style="color: #000000; font-weight: bold;">/</span>generate scaffold entry title:string description:text
$ rake db:migrate
$ script<span style="color: #000000; font-weight: bold;">/</span>server -p <span style="color: #000000;">3333</span></pre></td></tr></table></div>

<p>3行目では、titleとdescriptionを持ったentryモデルを作成しています。</p>
<h3>確認</h3>
<p>GET（データ取得）、DELETE（データ削除）は試せたのですが、POST（データ追加）とPUT（データ更新）がいまいち試せてなかったので、telnetで試してみました。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="bash">$ telnet localhost <span style="color: #000000;">3333</span>
Trying <span style="color: #000000;">127.0</span><span style="color: #000000;">.0</span><span style="color: #000000;">.1</span>...
Connected to localhost.
Escape character is <span style="color: #ff0000;">'^]'</span>.
POST <span style="color: #000000; font-weight: bold;">/</span>entries.xml HTTP<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">1.1</span>
Content-Type: application<span style="color: #000000; font-weight: bold;">/</span>xml
Content-Length: <span style="color: #000000;">83</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;</span>entry<span style="color: #000000; font-weight: bold;">&gt;&lt;</span>title<span style="color: #000000; font-weight: bold;">&gt;</span>this is the title<span style="color: #000000; font-weight: bold;">&lt;/</span>title<span style="color: #000000; font-weight: bold;">&gt;&lt;</span>description<span style="color: #000000; font-weight: bold;">&gt;</span>desc desc<span style="color: #000000; font-weight: bold;">&lt;/</span>description<span style="color: #000000; font-weight: bold;">&gt;&lt;/</span>entry<span style="color: #000000; font-weight: bold;">&gt;</span></pre></td></tr></table></div>

<p>5行目よりHTTPメソッドを発行していて、6行目ではContent-Typeをxmlとしています（Content-Typeは必須のようですね）。7行目のContent-Lengthは、9行目に送信しているデータ（XML）のバイト数です。</p>
<p>この結果、下記のようなレスポンスが返ってきます。16,18行目では、指定したtitle, descriptionが入力されているのを確認することができます（もちろん、http://localhost:3333/entries/2 でも確認できます）。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code"><pre class="bash">HTTP<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">1.1</span> <span style="color: #000000;">201</span> Created
Connection: close
Date: Wed, <span style="color: #000000;">14</span> May <span style="color: #000000;">2008</span> <span style="color: #000000;">14</span>:<span style="color: #000000;">25</span>:<span style="color: #000000;">22</span> GMT
Set-Cookie:<span style="color: #000000; font-weight: bold;">&lt;</span>略<span style="color: #000000; font-weight: bold;">&gt;</span>
Status: <span style="color: #000000;">201</span> Created
Location: http:<span style="color: #000000; font-weight: bold;">//</span>:<span style="color: #000000; font-weight: bold;">/</span>entries<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">2</span>
X-Runtime: <span style="color: #000000;">0.03339</span>
Cache-Control: no-cache
Server: Mongrel <span style="color: #000000;">1.1</span><span style="color: #000000;">.4</span>
Content-Type: application<span style="color: #000000; font-weight: bold;">/</span>xml; <span style="color: #007800;">charset=</span>utf<span style="color: #000000;">-8</span>
Content-Length: <span style="color: #000000;">286</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;</span>?xml <span style="color: #007800;">version=</span><span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #007800;">encoding=</span><span style="color: #ff0000;">&quot;UTF-8&quot;</span>?<span style="color: #000000; font-weight: bold;">&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;</span>entry<span style="color: #000000; font-weight: bold;">&gt;</span>
  <span style="color: #000000; font-weight: bold;">&lt;</span>created-at <span style="color: #007800;">type=</span><span style="color: #ff0000;">&quot;datetime&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000;">2008</span><span style="color: #000000;">-05</span>-14T14:<span style="color: #000000;">25</span>:22Z<span style="color: #000000; font-weight: bold;">&lt;/</span>created-at<span style="color: #000000; font-weight: bold;">&gt;</span>
  <span style="color: #000000; font-weight: bold;">&lt;</span>description<span style="color: #000000; font-weight: bold;">&gt;</span>desc desc<span style="color: #000000; font-weight: bold;">&lt;/</span>description<span style="color: #000000; font-weight: bold;">&gt;</span>
  <span style="color: #000000; font-weight: bold;">&lt;</span>id <span style="color: #007800;">type=</span><span style="color: #ff0000;">&quot;integer&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000;">2</span><span style="color: #000000; font-weight: bold;">&lt;/</span>id<span style="color: #000000; font-weight: bold;">&gt;</span>
  <span style="color: #000000; font-weight: bold;">&lt;</span>title<span style="color: #000000; font-weight: bold;">&gt;</span>this is the title<span style="color: #000000; font-weight: bold;">&lt;/</span>title<span style="color: #000000; font-weight: bold;">&gt;</span>
  <span style="color: #000000; font-weight: bold;">&lt;</span>updated-at <span style="color: #007800;">type=</span><span style="color: #ff0000;">&quot;datetime&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000;">2008</span><span style="color: #000000;">-05</span>-14T14:<span style="color: #000000;">25</span>:22Z<span style="color: #000000; font-weight: bold;">&lt;/</span>updated-at<span style="color: #000000; font-weight: bold;">&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;/</span>entry<span style="color: #000000; font-weight: bold;">&gt;</span></pre></td></tr></table></div>

<p>ちなみにわざわざtelnetを使わなくても、Firefoxのプラグインである <a href="https://addons.mozilla.org/en-US/firefox/addon/2691" target="_blank" onclick="javascript:pageTracker._trackPageview('/outbound/article/https://addons.mozilla.org/en-US/firefox/addon/2691');">Poster</a> を使うとGUIでHTTPメソッドを発行できるので便利です。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.poqu.org/2008/05/15/restful-rails-telnet/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
