Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
SlideShare a Scribd company logo
ActiveResourceが面白すぎる件松本 一輝Lang-8,inc.http://d.hatena.ne.jp/kazuk_i
自己紹介Lang-8
Ruby、RoR暦
RoRに手を出した経緯Lang-8での利用
ARes概要[ARes]Person.find(1)[Net::HTTP]GET /people/1.json#=> #<Person:0xb7ad7368 @attributes={“name”=>”kazuki”...}...>[AC]HTTP/1.x 200 OK{“name”:”kazuki” “age”:25 “...”:”...”}Rails2.0で導入
AResはクライアントを担当
ActionController(AC)はサーバを担当※今回はクライアント(ARes)の話に限定
規約RESTful
レスポンスはXML又はJSONで表現
レスポンスのステータスはHTTP status codeで表現
ページャは邪道→ サポート外使用例class Journal < ActiveResource::Base  self.site = "http://foo.com"  self.format = :jsonendJournal.find(119515, :params => {:with_comments => 1})#=> will return reply fromhttp://foo.com/journals/119515.json?with_comments=1
変化自由な変態性AResではARと違い、レスポンスデータが入れ子の場合がある。  => その場で動的にクラスを作って対応{“id”:119515, “user_id”:39691 “subject”:”foo” “body”:”bar” “comments”:[		{“user_id”:23311,“body”:”zoo”...}, ...]}#<Journal:0xb7b1a6f4 @prefix_options={}, @attributes={"user_id”=>39691,”subject”=>”foo”,”body”=>”bar”,”comments”=>[#<Journal::Comment:0xb7b18480 @prefix_options={}, @attributes={"user_id"=>23311, ...]}>, … ]}>Singularize+CamelizeJournal.find(:last).comments.foo.bar ・・・ などと辿っていくことができる。
物足りないところURL pathの自由度
Pagination
JSONパーサの切り替え
透過キャッシュ機構URL pathの自由度collection_path(例) http://foo.com/journals.json=> ARでいうところの、:all スコープ=> GET, POSTの操作対象element_path(例) http://foo.com/journals/119515.json=> GET, PUT, DELETE の操作対象
PaginationWAN越しなので、データ量の制限が不可欠 しかし・・・The paginator produces horribly unscalable code which will bring your server to a halt. – rabble
(参考) non-Rails way APIs世間のREST APIは、Rails wayではない場合が多い・・・例:OpenSocial v0.8 - Activity Resourceの複数取得http://foo.com/activities/{guid}/@friends#=>{“startIndex”: 0, “totalResults”: 20, “itemsPerPage”: 3, “entry”: [        { “id”: “http://...”          “title”: “...” },        {        } … }
JSONパーサの切り替えプロファイリング:100.times { Journal.find(119515, :params => {:with_comments => 1}) }(※JournalはActiveResource::Baseの継承クラス)Simple-jsonを使った場合:  0.02   920.39      0.17      100     1.70   168.20  ActiveResource::Connection#request  0.03   915.76      0.27      100     2.70  8259.20  JsonParser#parse  0.01   929.72      0.05      100     0.50   605.40  Journal#instantiate_recordjson/extを使った場合:  0.10   100.00      0.11      100     1.10   166.70  ActiveResource::Connection#request  0.04   105.45      0.04      100     0.40     6.20  JSON.parse  0.03   106.76      0.03      100     0.30   605.90  Journal#instantiate_record
JSONパーサの切り替え(ms)

More Related Content

ActiveResourceが面白すぎる件