# 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_groups_for_user, :user_id => "2155" 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_items_for_user, :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, :clusters) end end describe "post" do it "should return status 200 when add an item" do intercept_resquest_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_resquest_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 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_resquest_to(method, options) action = options.delete(:action) params = options.delete(:params) response = options.delete(:response) uri = URI.parse(Zunnit.url).merge(Zunnit.actions[action]) uri = uri + params.map {|k,v| "#{k}=#{v}" }.join("&") unless method.to_s =~ /post|put/ FakeWeb.register_uri(method, uri, response) end end