Skip to content

Instantly share code, notes, and snippets.

@jeffdeville
Created January 30, 2011 03:29
Show Gist options
  • Select an option

  • Save jeffdeville/802491 to your computer and use it in GitHub Desktop.

Select an option

Save jeffdeville/802491 to your computer and use it in GitHub Desktop.
class Hunch
include HTTParty
base_uri 'http://api.hunch.com/api/v1/'
def get_recommendations(fb_id, price_range)
options = {:query => {:auth_token => HUNCH_TOKEN, :friend_id => fb_id, :topic_ids => 'all_484,all_12834,all_76624,all_75594,all_24304,all_24284,all_12734,all_293104,all_394864,all_339804,all_75454,all_339814,all_24274,all_544,all_338474,all_339774,all_75634,all_24294,all_339794,all_24314', :limit => 10, :popularity => 0}}
hunch_results = self.class.get('/get-recommendations', options)
hunch_results["recommendations"].map{|result| to_gift_idea(result) } unless hunch_results["recommendations"].nil?
[]
end
def to_gift_idea(hunch_result)
gift_idea = GiftIdea.new({:price => hunch_result["price"] || "", :title => hunch_result["title"], :url => hunch_result["url"], :images => [{:url => hunch_result["image_url"]}], :gift_idea_id => hunch_result["result_id"]})
end
end
def show
@reco_request = RecommendationRequest.load_by_id(params[:request_id]).first
gift_idea_ids = @reco_request.recommendations.collect{ |reco| BSON::ObjectId.from_string(reco.gift_idea_id) }
recommender_ids = @reco_request.recommendations.collect{ |reco| reco.recommender_id.to_s }
@gift_ideas = GiftIdea.where(:_id.in => gift_idea_ids).
inject({}){|hsh,gi| hsh[gi._id.to_s] = gi; hsh}
@recommenders = User.where(:_id.in => recommender_ids).only(:picture).
inject({}){|hsh,user| hsh[user._id.to_i] = user; hsh}
@recipient = User.load_by_id(@reco_request.for_id).only(:birthday, :interests, :gender, :picture).first
hunch = Hunch.new
@hunch_ideas = hunch.get_recommendations("fb_#{@reco_request.for_id}", @reco_request.price_range)
render :layout => "wish_genie_lean"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment