require 'spec_helper' require 'json' puts ZooppaApiV3 describe ZooppaApiV3 do context '#new' do subject(:zooppa_api) { ZooppaApiV3 } it 'sets default @api_host' do expect(zooppa_api.new.instance_variable_get('@api_host')).to eq 'http://localhost:3000/' end it 'sets default @version' do expect(zooppa_api.new.instance_variable_get('@version')).to eq 'v3' end it 'sets default @encrypt to true' do expect(zooppa_api.new.instance_variable_get('@encrypt')).to eq true end it 'sets @app_secret' do expect(zooppa_api.new(app_secret: 'secret').instance_variable_get('@app_secret')).to eq 'secret' end it 'sets @app_id' do expect(zooppa_api.new(app_id: 'id').instance_variable_get('@app_id')).to eq 'id' end end context '#build_url' do subject(:zooppa_api) { ZooppaApiV3.new } context 'Given .where is called with one filter argument' do it 'returns correct url' do expect(zooppa_api.ads.where('contest_id,=,1').build_url).to eq 'http://localhost:3000/api/v3/ads.json?filters[filter_0]=contest_id,=,1' end end context 'Given .where is called with two filter argument' do it 'returns correct url' do expect(zooppa_api.ads.where('contest_id,=,1', 'resource_type,=,video').build_url).to eq 'http://localhost:3000/api/v3/ads.json?filters[filter_0]=contest_id,=,1&filters[filter_1]=resource_type,=,video' end end context 'Given .sort is called an argument' do it 'returns correct url' do expect(zooppa_api.ads.sort('title ASC').build_url).to eq 'http://localhost:3000/api/v3/ads.json?sort_by=title+ASC' end end context 'Given .sort is called with an argument and .where with two filter argument' do it 'returns correct url' do expect(zooppa_api.ads.where('contest_id,=,1', 'resource_type,=,video').sort('title ASC').build_url).to eq 'http://localhost:3000/api/v3/ads.json?filters[filter_0]=contest_id,=,1&filters[filter_1]=resource_type,=,video&sort_by=title+ASC' end end context 'Given .find(1) is called' do it 'returns the correct url' do expect(zooppa_api.ads.find(1).build_url).to eq 'http://localhost:3000/api/v3/ads/1.json' end end context 'Given .page(1) is called' do it 'returns the correct url' do expect(zooppa_api.ads.page(1).build_url).to eq 'http://localhost:3000/api/v3/ads.json?page=1' end end context 'Given .per_page(10) is called' do it 'returns the correct url' do expect(zooppa_api.ads.per_page(10).build_url).to eq 'http://localhost:3000/api/v3/ads.json?per_page=10' end end context 'Given .per_page(10) and .page(1) is called' do it 'returns the correct url' do expect(zooppa_api.ads.page(1).per_page(10).build_url).to eq 'http://localhost:3000/api/v3/ads.json?page=1&per_page=10' end end context 'Given .per_page(10), .page(1), .where with two filter argument and .sort() is called' do it 'returns the correct url' do expect(zooppa_api.ads.page(1).per_page(10).where('contest_id,=,1', 'resource_type,=,video').sort('title ASC').build_url).to eq 'http://localhost:3000/api/v3/ads.json?filters[filter_0]=contest_id,=,1&filters[filter_1]=resource_type,=,video&sort_by=title+ASC&page=1&per_page=10' end end context 'Given .where is called' do before do zooppa_api.ads.where('contest_id,=,1', 'resource_type,=,video').build_url end context 'and a subsequent .ads (index) call without a .where method' do it 'returns correct url' do expect(zooppa_api.ads.build_url).to eq 'http://localhost:3000/api/v3/ads.json' end end end end context '#execute' do subject(:zooppa_api) { ZooppaApiV3.new } context 'Given .find(1) is called with .execute(false)' do it 'it calls Restclient.send with :get as method and correct url' do expect(RestClient).to receive(:send).with(:get, 'http://localhost:3000/api/v3/ads/1.json').and_return({}.to_json) zooppa_api.ads.find(1).execute(false) end end context 'Given .find(1) is called with .execute(token)' do it 'it calls Restclient.send with :get as method and correct url with access token' do expect(RestClient).to receive(:send).with(:get, 'http://localhost:3000/api/v3/ads/1.json?access_token=token').and_return({}.to_json) zooppa_api.ads.find(1).execute('token') end end context 'Given .where("contest_id,=,1") is called' do it 'it calls Restclient.send with :get as method and correct url' do expect(RestClient).to receive(:send).with(:get, 'http://localhost:3000/api/v3/ads.json?filters[filter_0]=contest_id,=,1').and_return({}.to_json) zooppa_api.ads.where('contest_id,=,1').execute end end context 'Given .where("contest_id,=,1") is called with .execute(token)' do it 'it calls Restclient.send with :get as method and correct url with access token' do expect(RestClient).to receive(:send).with(:get, 'http://localhost:3000/api/v3/ads.json?filters[filter_0]=contest_id,=,1&access_token=token').and_return({}.to_json) zooppa_api.ads.where('contest_id,=,1').execute('token') end end context 'Given .delete is called' do it 'it calls Restclient.send with :delete as method and correct url' do expect(RestClient).to receive(:send).with(:delete, 'http://localhost:3000/api/v3/ads/1.json').and_return({}.to_json) zooppa_api.ads.find(1).delete.execute end end context 'Given .delete is called with execute(token)' do it 'it calls Restclient.send with :delete as method and correct url' do expect(RestClient).to receive(:send).with(:delete, 'http://localhost:3000/api/v3/ads/1.json?access_token=token').and_return({}.to_json) zooppa_api.ads.find(1).delete.execute('token') end end context 'Given .create is called' do it 'it calls Restclient.send with :post method, correct url and parameters' do expect(RestClient).to receive(:send).with(:post, 'http://localhost:3000/api/v3/ads.json', {ad: {title: 'title'}}).and_return({}.to_json) zooppa_api.ads.create({ad: {title: 'title'}}).execute end end context 'Given .create is called with .execute(token)' do it 'it calls Restclient.send with :post method, correct url and parameters with access token' do expect(RestClient).to receive(:send).with(:post, 'http://localhost:3000/api/v3/ads.json', { ad: { title: 'title'}, access_token: 'token'}).and_return({}.to_json) zooppa_api.ads.create({ad: {title: 'title'}}).execute('token') end end context 'Given .update_attributes is called' do it 'it calls Restclient.send with :patch method, correct url and parameters' do expect(RestClient).to receive(:send).with(:patch, 'http://localhost:3000/api/v3/ads/1.json', {ad: {title: 'title'}}).and_return({}.to_json) zooppa_api.ads.find(1).update_attributes({ad: {title: 'title'}}).execute end end context 'Given .update_attributes is called with .execute(token)' do it 'it calls Restclient.send with :patch method, correct url and parameters with access token' do expect(RestClient).to receive(:send).with(:patch, 'http://localhost:3000/api/v3/ads/1.json', { ad: { title: 'title' }, access_token: 'token' }).and_return({}.to_json) zooppa_api.ads.find(1).update_attributes({ad: {title: 'title'}}).execute('token') end end end end