# encoding: utf-8 require File.expand_path(File.dirname(__FILE__) + '/spec_helper') describe Zunnit do # let(zunnit) { Zunnit::Api.new } describe "get" do it "should instanciate a Zunnit::Api object" do default_key = Zunnit.key default_client = Zunnit.client default_version = Zunnit.version zunnit = Zunnit::Api.new do |z| z.key = "new-key" z.version = "1.1" end zunnit.class.should == Zunnit::Api zunnit.client.should == default_client zunnit.key.should == "new-key" zunnit.version.should == "1.1" Zunnit.client.should == default_client Zunnit.key.should == default_key Zunnit.version.should == default_version end it "should return related items" do response = Zunnit.api.get :related_items, :item_id => "20275919", :user_id => "255" validate_response(response, :items) end it "should get 400 error if :item_id is not given to related items" do response = Zunnit.api.get(:related_items) response[:status].should == 400 end it "should return recommendation items for user" do response = Zunnit.api.get :recommendation_items_for_user, :user_id => "255" validate_response(response, :items) end it "should return recommendation groups for user" do #pending "404" response = Zunnit.api.get :recommendation_groups_for_user, :user_id => "255" validate_response(response, :groups) end it "should return recommendation users for user" do #pending "404" response = Zunnit.api.get :recommendation_users_for_user, :user_id => "255" validate_response(response, :users) end it "should return recommendation users for item" do #pending "It should return a collection of users but is returning items" response = Zunnit.api.get :recommendation_users_for_item, :item_id => "20275919", :user_id => "255" validate_response(response, :users) end it "should return recommendation tags for item" do #pending "404" response = Zunnit.api.get :recommendation_tags_for_item, :item_id => "20275919" validate_response(response, :tags) end it "should return recommendation cluster for item" do pending "404" response = Zunnit.api.get :recommendation_cluster_for_item, :item_id => "20275919" validate_response(response, :cluster) end it "should return recommendation product for item" do intercept_request_to :get, :action => :recommendation_products_for_item, :params => { :item_id => "2121" }, :response => { :status => 200, :body => { :msg => 'Ok', :status => 200, :rec_id => "dba158a81706dbd", :products => [ { :id => "MLB199848107", :title => "ReloĢgio Nike", :price => 199.99, :thumbnail => "http://img1.mlstatic.com/s_MLB_v_I_f_156827423_9293.jpg" } ] }.to_json } response = Zunnit.api.get :recommendation_products_for_item, :item_id => "2121" validate_response(response, :products) end end describe "post" do it "should return status 200 when add an item" do intercept_request_to :post, :action => :action_items_add, :params => { :item_id => "2121", :item_text => "text" }, :response => { :status => 200, :body => {:msg => 'Ok', :status => 200}.to_json } validate_response Zunnit.api.post :action_items_add, :item_id => "2121", :item_text => "text" end it "should return status 200 when user clip an item" do intercept_request_to :post, :action => :action_items_rate, :params => { :item_id => "2121", :user_id => "2121", :rating => 1 }, :response => { :status => 200, :body => {:msg => 'Ok', :status => 200}.to_json } validate_response Zunnit.api.post :action_items_rate, :item_id => "2121", :user_id => "2121" end it "should return status 200 when user view an recommended item" do intercept_request_to :post, :action => :action_feedback_click, :params => {:rec_id => "2121", :rank => 0}, :response => { :status => 200, :body => { :msg => "Ok", :status => 200 }.to_json } validate_response Zunnit.api.post :action_feedback_click, :rec_id => "2121", :rank => 0 end end private def validate_response(response, results_key = nil) response[:status].should == 200 response[:msg].should == "Ok" response[results_key].size.should >= 1 if results_key end def intercept_request_to(method, options) action = options.delete(:action) params = options.delete(:params) params = params.merge :api_key => Zunnit.key response = options.delete(:response) uri = URI.parse(Zunnit.url).merge(Zunnit.actions[action]) uri.query = params.map {|k,v| "#{k}=#{v}" }.join("&") unless method.to_s =~ /post|put/ FakeWeb.register_uri(method, uri, response) end end