MerbアプリケーションでAtomフィードを配信する方法のメモです。

まずは、config/dependencies.rbに、利用するGemを登録します。

ruby>>
dependency "merb-builder"
dependency "merb_full_url"
<<--

merb_full_url
は、フィード中で埋め込まれる
absoluteなURLを生成するためのプラグインです。
以下のようにインストールできます。

pre>>
% sudo gem install merb_full_url --source http://merbi.st
<<--

次に、コントローラのindexアクションで
atomフィードを提供する事を指示します。

ruby>>
def index
provides :atom
<<--

さらに、config/init.rbで、MIMEタイプとしてatomを登録します。

ruby>>
Merb::BootLoader.before_app_loads do

(..snip..)

Merb.add_mime_type(:atom, :to_atom, %w[application/atom+xml])
end

<<--

続いて、フィードを生成するためのindex.atom.builderファイルを
app/views/{resource_name}の下に作ります。
このあたりはRailsのXmlBuilderと同じような感じです。

ruby>>
xml.instruct! :xml, :version=>"1.0"
xml.feed(:xmlns => "http://www.w3.org/2005/Atom") do |feed|
feed.title @title
feed.link :type => 'text/html', :rel => 'alternate',
:href => full_resource(:posts)

@posts.each do |post|
feed.entry do |entry|
entry.id post.id
entry.title post.title
entry.content post.body, :type => 'text'
entry.issued post.created_at
entry.modified post.updated_at
entry.link :type => "text/html", :rel => "alternate",
:href => full_resource(post)
entry.author do |author|
author.name post.user.login
end
end
end
end
<<--

以上で完了。これで後は/{resource_name}.atom
にアクセスすれば、フィードが表示されます。

ちなみに、Vimでmerb.vimを使っている場合、.builderのfiletype
を認識させるために、.vim/ftdetect/merb.vimの中で、
以下の1行を加えると、正しくファイルタイプを認識してくれます。

pre>>
au BufRead,BufNewFile /app/{mailers/,}views/.builder set ft=ruby.merb_controller
<<--

posted by genki genki on Tue 27 Jan 2009 at 06:17 with 0 comments
Contents rssrss
光ファイバーを二次元振動させて走査するAR用ディスプレイ
因果の取り違え
Swift2's defer for CoffeeScript
mongodb-3.0からcreateIndexのdropDupsが無くなったらしい
mongodb-3.0以降のWiredTigerの設定を動的に変更する方法
一般楕円の高速生成アルゴリズムへの道標
farro mantecatoのレシピ
Droonga関連の記事のまとめ
RuntimeErrorの特定のメッセージに限定してrescueする方法
jQueryでscriptタグを実行せずにappendする
Tags
coffeescriptdefergroongajsmemonodenodejs
Comments rssrss
https://xn--krken21-bn4c.com https://xn--krken21-bn4c.com: Как отличить настоящий маркетплейс от подделки Kra... 05/01 12:20
https://example.com/xkkpsk https://example.com/xkkpsk: Great blog you have here.. It's difficult to find ... 05/01 09:31
https://onlinemo.ir https://onlinemo.ir: جمع‌بندی برای اون گروه از کاربرا که سیستم‌ها... 05/01 00:58
https://xn--mgmarkt9-9db.com https://xn--mgmarkt9-9db.com: Как обезопасить MEGA: настройка менеджера паролей ... 04/30 20:14
https://qeshmtourist.ir https://qeshmtourist.ir: در کل قضیه برای اونایی که می‌خوان وارد بشن س... 04/30 19:31
https://negarinadv.ir https://negarinadv.ir: سلام و عرض ادب، بنده مدتی قبل هنگام گشتن تو اینت... 04/30 18:56
https://gsmcharge.ir https://gsmcharge.ir: در کل قضیه برای کاربران علاقه‌مند به پلتفرم‌... 04/30 13:51
https://azardl.ir https://azardl.ir: در کل برای کاربرایی که در جستجو هستن پلتفرم‌... 04/30 02:42