More Related Content
Similar to ActiveResourceが面白すぎる件 (20)
ActiveResourceが面白すぎる件
- 12. 変化自由な変態性 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 + Camelize Journal.find(:last).comments.foo.bar ・・・ などと辿っていくことができる。
- 19. 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_record json/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
- 22. Monkeys ActiveResource::Baseにmonkey-patchを当てる。 想定API: Lang-8 backend API GET /journals (複数結果の場合) #=> {“from”: 0, “totalnum”: 20, “size”: 3, “list”: [ { “id”: “1”, “subject”: “...” }, { } … } GET /journals/1 (単一結果の場合) #=> { “id”: “1”, “subject”: “...” , ... }