Rails2.0によるAtom/RSSフィードの実装(GET)
AtomPubっぽいものを出力する方法です。
- http://localhost:3000/example/index HTML
- http://localhost:3000/example/index.rss RSS2.0
- http://localhost:3000/example/index.atom Atom
という形で定義します。
Rails2.0の場合は、フォーマットによって出力を簡単に変更できますので、それを使います。
respond_to do |format|
format.html
format.atom {
render(:layout => false)
}
format.rss {
render(:layout => false, :action => "index.rxml")
}
end
これで、Atomの場合は/views/example/index.atom.builderが、RSSの場合は/views/example/index.rxmlが読み込まれます。
Atomのテンプレートは以下のようになります。
atom_feed do |feed|
feed.title("#{@title} » Example Feed")
feed.updated(@memos.first.created_at)
@memos.each do |m|
feed.entry(m, :url => create_link(m)) do |entry|
entry.title(m.title)
entry.content(trans(m), :type => "html")
entry.author do |a|
a.name(@user.name)
end
end
end
end
これはベースのようなものです。@memosというリストごとにフィードのエントリーを定義して、タイトルやcontentを作成しています。次にRSS側も同様に処理できます。
xml.instruct!
ml, :version => "1.0", :encoding => "UTF-8"
xml.rss('version' => '2.0', 'xmlns:atom' => 'http://www.w3.org/2005/Atom', 'xmlns:openSearch' => 'http://a9.com/-/spec/opensearchrss/1.0/') do
xml.channel do
xml.title @title
url = url_for(:controller => :memo, :action => :index, :id => @user.name,
nly_path => false)
xml.link url
xml.tag!("atom:id", url)
xml.description(@title)
xml.language "en-us"
xml.ttl "40"
xml.pubDate(Time.now.strftime("%a, %d %b %Y %H:%M:%S %Z"))
xml.managingEditor(@user.name)
xml.generator("Jot a Memo")
xml.tag!("openSearch:totalResults", @memos.paginate.total_entries)
xml.tag!("openSearch:startIndex", @startIndex)
xml.tag!("openSearch:itemsPerPage", @perpage)
xml.guid(url, "isPermaLink" => 'false')
xml.tag!("atom:updated", Time.now.strftime("%a, %d %b %Y %H:%M:%S %Z"))
@memos.each do |m|
xml.item do
xml.title(m.title)
xml.tag!('atom:id', create_link(m))
xml.guid(create_link(m), "isPermaLink" => 'false')
xml.pubDate(m.created_at)
xml.tag!("atom:updated", m.updated_at)
xml.tag!("atom:summary", trans(m))
xml.link(create_link(m))
xml.description(trans(m))
xml.guid(create_link(m))
xml.author(m.user.name)
end
end
end
end
やたらと長いのですが、あまり気にしないでください。大事なのは、xml.tag!を使うと、<atom:id>…</atom:id>のような形式が定義できるということと、xml.guid(aa, bb)のように二つ目の引数を定義すると、それがattributesになるということです。
後は<rss>の中に拡張書式を定義する場合があると思いますが、それもHashで指定可能です。
このように定義しておくと、次に以下のようなURLを考えた場合も容易に対応できます。
- http://localhost:3000/example/view/id HTML
- http://localhost:3000/example/view/id.rss RSS2.0
- http://localhost:3000/example/view/id.atom Atom
でidで指定されたデータの詳細表示を行う場合です。この場合は以下のように対応します。
@memos = [@memo]
respond_to do |format|
format.html
format.atom {
render(:layout => false, :action => "index.atom.builder")
}
format.rss {
render(:layout => false, :action => "index.rxml")
}
end
@memoという1件のデータを@memosに配列で入れた以外は、indexと同じ処理になります。実際、フィードで1件を表示する場合も複数件表示する場合も処理は変わらないので、これで対応できます。もちろん、HTMLの場合は異なると思いますので、view.rhtmlは内容が異なるかと思います。
とは言ってもこれではGETだけの対応なので、これからPOST/PUT/DELETEの実装を完了してはじめてAtomPubへの対応が完了したと言えるのですが。
RubyでGoogle Contacts Data APIを呼び出す
まだ基礎だけですが。とりあえず使うのはGDataライブラリです。
$ sudo gem install gdataSuccessfully installed GData-0.0.4Installing ri documentation for GData-0.0.4…Installing RDoc documentation for GData-0.0.4…
スクリプトは以下のように。
/opt/local/lib/ruby/gems/1.8/gems/GData-0.0.4/lib/gdata/google_contact.rb
require 'gdata/base'
require 'builder'
module GData
class GoogleContact < GData::Base
def initialize
super 'cp', 'gdata-ruby', 'www.google.com'
end
def list
request "/m8/feeds/contacts/#{@email}/base"
end
def authenticate(email, password)
@email = email
super email, password
end
end
使い方は簡単で、
require ‘gdata/google_contact’
>> gc = GData::GoogleContact.new
=> #<GData::GoogleContact:0×157cd34 @url=”www.google.com”, @source=”gdata-ruby”, @service=”cp”>
>> gc.authenticate “example@gmail.com”, “[your password]”
=> {”Authorization”=>”GoogleLogin auth=…\n”, “Content-Type”=>”application/atom+xml”}
>> gc.list
でリストがXMLで返ってきます。本当はこれをさらに解析してvCard形式とかでもとれるようになっていると便利そう。実際に返ってくるデータは次のような感じ(整形しています)。
=> ”
<?xml version=’1.0′ encoding=’UTF-8′?>
<feed xmlns=’http://www.w3.org/2005/Atom’ xmlns:openSearch=’http://a9.com/-/spec/opensearchrss/1.0/’ xmlns:gContact=’http://schemas.google.com/contact/2008′ xmlns:gd=’http://schemas.google.com/g/2005′>
<id>example@gmail.com</id>
<updated>2008-03-10T05:05:34.160Z</updated>
<category scheme=’http://schemas.google.com/g/2005#kind’ term=’http://schemas.google.com/contact/2008#contact’/>
<title type=’text’>Atsushi Nakatsugawa’s Contacts</title>
<link rel=’alternate’ type=’text/html’ href=’http://www.google.com/’/>
<link rel=’http://schemas.google.com/g/2005#feed’ type=’application/atom+xml’ href=’http://www.google.com/m8/feeds/contacts/moongift%40gmail.com/base’/>
<link rel=’http://schemas.google.com/g/2005#post’ type=’application/atom+xml’ href=’http://www.google.com/m8/feeds/contacts/moongift%40gmail.com/base’/>
<link rel=’self’ type=’application/atom+xml’ href=’http://www.google.com/m8/feeds/contacts/moongift%40gmail.com/base?max-results=25′/>
<link rel=’next’ type=’application/atom+xml’ href=’http://www.google.com/m8/feeds/contacts/moongift%40gmail.com/base?start-index=26&max-results=25′/>
<author>
<name>Atsushi Nakatsugawa</name>
<email>example@gmail.com</email>
</author>
<generator version=’1.0′ uri=’http://www.google.com/m8/feeds’>Contacts</generator>
<openSearch:totalResults>376</openSearch:totalResults>
<openSearch:startIndex>1</openSearch:startIndex>
<openSearch:itemsPerPage>25</openSearch:itemsPerPage>
<entry>
<id>http://www.google.com/m8/feeds/contacts/moongift%40gmail.com/base/0</id>
<updated>2007-11-26T02:27:10.592Z</updated>
<category scheme=’http://schemas.google.com/g/2005#kind’ term=’http://schemas.google.com/contact/2008#contact’/>
<title type=’text’>Atsushi Nakatsugawa</title>
<link rel=’self’ type=’application/atom+xml’ href=’http://www.google.com/m8/feeds/contacts/moongift%40gmail.com/base/0′/>
<link rel=’edit’ type=’application/atom+xml’ href=’http://www.google.com/m8/feeds/contacts/moongift%40gmail.com/base/0/1196044030592000′/>
<gd:email rel=’http://schemas.google.com/g/2005#other’ address=’example@moongift.jp’ primary=’true’/>
</entry>
:
タイトルにユーザ名が入って、gd:emailのaddressにメールアドレスが入る形ですね。個別で取得する場合はidを利用するようです。その他、住所などのデータも入っている場合は取得できます。とは言え、http://schemas.google.com/g/2005が見られないので、項目の詳細は不明です。
が、メールアドレスのリストだけであればこれで取得できるので便利です。
