1

내 컨트롤러의 업데이트 메소드에서 Viralheat의 API를 통해 요청을 보내고 사용자가 제출 버튼을 클릭하면 작업이 완료되고 API 호출이 이루어 지도록합니다. 나는 &하지 & & 등이 & API_KEY = 할 & http://www.viralheat.com/api/sentiment/review.json?text=i을 게시 할 [* API 키 *]제출시 viralheat에 API 호출을 수행 한 다음 JSON 응답을 구문 분석하고 저장하려면 어떻게해야합니까?

이 형식의 일부 JSON을 반환합니다

{"mood":"negative","prob":0.773171679917001,"text":"i do not like this"} 

는 컨트롤러 메소드를 실행하는 동안 동시에 그 API 통화를 할 수 있나요 어떻게 내가 JSON 응답을 처리 할 것인가? 어떤 컨트롤러 방법을 넣을까요?

궁극적으로 BrandUsers 테이블에서 내 정서 열에 응답 분위기를 저장하고 싶습니다. 제출은 main.html.erb에 있으며, 그러면 update 메소드가 사용됩니다.

컨트롤러 설치 wrest 보석으로

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 

답변

2

, 당신은 이미 해시로 바뀌 json으로 포함됩니다

params[:api_key] = "your key" 

url = "http://www.viralheat.com/api/sentiment/review.json" 

response = url.to_uri.get(params).deserialize 

response처럼 뭔가를 할 수 있습니다. 그래서 당신은 기분에 액세스 할 수 있습니다

response[:mood] 
+0

나는 그것을 http://wrestdemo.heroku.com/에서 시도했고 deserialize를 인식하지 못합니다. 대신'HTTPARTY'를 사용하겠습니다. – Simpleton

+0

실제로 설치를 시도 했습니까? 데모 응용 프로그램은 다소 불안해 할 수 있습니다. – netmute

+0

그래서 설치했지만 응답 [: 기분]을 사용할 수 없습니다. – Simpleton