前回のアノゾンに引き続き(http://b.ruyaka.com/2010/02/08/rails-on-amazon-web-service/。
Railsで楽天APIを利用してみる。
手順① 楽天ウェブサービスでアカウント登録
アカウント登録をします。
「デベロッパーID」「アフェリエイトID」などを取得します。
http://webservice.rakuten.co.jp/
点順② 楽天用のActiveResourceを作成する
「ActiveRecord::Base」ではなく「ActiveResource::Base」なので注意。
これでほとんど必要な機能は満たしてくれてます。Rails素敵。
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 |
<textarea class="html" cols="60" rows="5" name="code"> class Rakuten < ActiveResource::Base self.site = "http://api.rakuten.co.jp" self.format = :xml FROM = "/rws/2.0/rest" AFFILIATE_ID = "******************************" DEVLOPER_ID = "******************************" VERSION = "2009-04-15" def self.item_search(keyword, genre_id=0, page=1) self.find( :one, :from => FROM, :params => { :developerId => DEVLOPER_ID, :affiliateId => AFFILIATE_ID, :version => VERSION, :operation => "ItemSearch", :genreId => genre_id, :page => page, :keyword => keyword } ) end end </textarea> |
手順③ データを取得する
適当なコントローラーから下記のようにコマンドを打ちます。
items = Rakuten.item_search(“Rails”)
■参考サイト
商品検索の各種パラメーター一覧
http://webservice.rakuten.co.jp/api/itemsearch/