コントローラーのupdateメソッドでViralheatのAPIを介してリクエストを送信し、ユーザーが送信ボタンを押すとアクションが完了してAPI呼び出しが行われるようにします。http://www.viralheat.com/api/sentiment/review.json?text=i&do¬&like&this&api_key=[*あなたのAPIキー* ]に投稿したい
これにより、JSONが次の形式で返されます。
{"mood":"negative","prob":0.773171679917001,"text":"i do not like this"}
コントローラメソッドの実行中にそのAPI呼び出しを同時に行うことは可能ですか?JSON応答をどのように処理しますか?どのコントローラーメソッドに入れますか?
最終的には、BrandUsersテーブルの感情列に応答ムードを保存したいと思います。送信はmain.html.erbにあり、更新メソッドを使用します。
コントローラ
def update
@brand = Brand.find(params[:id])
current_user.tag(@brand, :with => params[:brand][:tag_list], :on => :tags)
if @brand.update_attributes(params[:brand])
redirect_to :root, :notice => "Brand tagged."
else
render :action => 'edit'
end
end
def main
@brands = Brand.all
if current_user
@brand = current_user.brands.not_tagged_by_user(current_user).order("RANDOM()").first
end