20090418 イケテルRails勉強会 第1部Rails編
をやってみた。
準備
↓一通り読んで、API利用登録。
[http://tabelog.com/help/api/:title=食べログAPI サービス案内[食べログ]]
やってみた感想
ActiveResource凄い。
apiの処理が簡単に扱える。
(Hash.from_xmlでxmlをhashにして扱う。)
注意事項
資料上、APIのリクエストパラメータが小文字で記載されてますが
実際は、先頭大文字なので注意。
詳細は下記。
[http://tabelog.com/help/api_manual#rst-api:title=食べログAPI マニュアル[食べログ]]
一応ソース
prefectureモデルはscaffoldで作成するので略。
#models/restaurant.rb class Restaurant < ActiveResource::Base self.site = "http://api.tabelog.com/" def self.find_restaurants(restaurant_params={}) restaurant_params[:Key] = "取得したAPIアクセスキー" Hash.from_xml( self.find( :one, :from => "/Ver2.1/RestaurantSearch/", :params => restaurant_params ).to_xml ) end end
#models/bookmark.rb class Bookmark < ActiveRecord::Base has_attached_file :photo end
#controllers/restaurants_controller class RestaurantsController < ApplicationController def index @prefectures = Prefecture.find(:all) @pages = Array.new(100){ |i| i+1 } @restaurants = Restaurant.find_restaurants(request.query_parameters)["restaurant"] @restaurants["Item"].each do |item| bookmark = Bookmark.find_by_rcd item["Rcd"] if bookmark item["Memo"] = bookmark.memo item["image_url"] = bookmark.photo_file_name ? request.protocol + request.host_with_port + bookmark.photo.url : nil end end #xmlの仕様にあわせるため、ItemをItemsに変更 @restaurants["Items"] = @restaurants["Item"] @restaurants.delete "Item" respond_to do |format| format.html # index.html.erb format.xml { render :xml => @restaurants.to_xml(:root => "restaurant", :dasherize => false ) } end end def create if params[:create] && params[:Rcd] bookmark_params = { :rcd => params[:Rcd], :memo => params[:Memo], :photo => params[:photo] } bookmark = Bookmark.find_or_initialize_by_rcd bookmark_params bookmark.attributes = bookmark_params bookmark.save end restaurants_params = { :Prefecture => params[:Prefecture], :PageNum => params[:PageNum].to_i } redirect_to :controller => :restaurants, :action => :index, :params => restaurants_params end end
#views/restaurants/index.html.erb <h1>RBC食べログ</h1> <% form_tag({}, {:multipart => true}) do -%> <p> 都道府県:<%= select_tag :Prefecture, options_from_collection_for_select(@prefectures, :en, :ja, params[:Prefecture]) %> </p> <p> ページ:<%= select_tag :PageNum, options_for_select(@pages, params[:PageNum].to_i) %> </p> <%= submit_tag "検索", {:name => "search"} %> <ul> <% @restaurants["Items"].each do |item| -%> <li> <%= radio_button_tag :Rcd, item["Rcd"] %> <%= link_to item["RestaurantName"], item["TabelogUrl"], {:target => '_blank'} %> <%= item["Memo"] %> <% unless item["image_url"].blank? -%> <br /><%= image_tag item["image_url"] %> <% end -%> </li> <% end -%> </ul> <p> メモ:<%= text_field_tag :Memo %> </p> <p> イメージ:<%= file_field_tag :photo %> </p> <%= submit_tag "登録", { :name => "create" } %> <% end -%>
資料では、初期マスタデータである都道府県情報の登録を
migrationファイル内に記載しているが、seedの方が適切と判断し
seeds.rbに記載した。
#db/seeds.rb Prefecture.delete_all prefs = [ ["全国","japan"], ["北海道","hokkaido"], ["青森","aomori"], ["岩手","iwate"], ["宮城","miyagi"], ["秋田","akita"], ["山形","yamagata"], ["福島","fukushima"], ["東京","tokyo"], ["神奈川","kanagawa"], ["埼玉","saitama"], ["千葉","chiba"], ["茨城","ibaraki"], ["栃木","tochigi"], ["群馬","gunma"], ["山梨","yamanashi"], ["長野","nagano"], ["新潟","niigata"], ["富山","toyama"], ["石川","ishikawa"], ["福井","fukui"], ["愛知","aichi"], ["岐阜","gifu"], ["静岡","shizuoka"], ["三重","mie"], ["大阪","osaka"], ["兵庫","hyogo"], ["京都","kyoto"], ["滋賀","shiga"], ["奈良","nara"], ["和歌山","wakayama"], ["鳥取","tottori"], ["島根","shimane"], ["岡山","okayama"], ["広島","hiroshima"], ["山口","yamaguchi"], ["徳島","tokushima"], ["香川","kagawa"], ["愛媛","ehime"], ["高知","kochi"], ["福岡","fukuoka"], ["佐賀","saga"], ["長崎","nagasaki"], ["熊本","kumamoto"], ["大分","oita"], ["宮崎","miyazaki"], ["鹿児島","kagoshima"], ["沖縄","okinawa"] ] prefs.each do |pref| Prefecture.create!(:ja => pref[0], :en => pref[1]) end
あとで
paperclipは調べよう。