From d21553e6813075585125e0624e8e997c3c3bef9e Mon Sep 17 00:00:00 2001 From: Alex Lushpai Date: Thu, 16 Jul 2015 18:00:51 +0300 Subject: [PATCH] fix references edit methods, fix make request to proper collect of ids & filters, referenses tests added --- .gitignore | 3 +- Rakefile | 6 +- lib/retailcrm.rb | 40 +++++--- retailcrm.gemspec | 2 +- spec/bootstrap-dist.rb | 207 ++++++++++++++++++++++++++++++++++++++++ spec/spec_customers.rb | 30 ++++++ spec/spec_references.rb | 39 ++++++++ spec/spec_retailcrm.rb | 16 ++++ test/bootstrap.rb.dist | 133 -------------------------- test/spec_retailcrm.rb | 26 ----- test/test_retailcrm.rb | 27 ------ 11 files changed, 324 insertions(+), 205 deletions(-) create mode 100644 spec/bootstrap-dist.rb create mode 100644 spec/spec_customers.rb create mode 100644 spec/spec_references.rb create mode 100644 spec/spec_retailcrm.rb delete mode 100644 test/bootstrap.rb.dist delete mode 100644 test/spec_retailcrm.rb delete mode 100644 test/test_retailcrm.rb diff --git a/.gitignore b/.gitignore index 614ce6a..2f547af 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *.gem -/test/bootstrap.rb +spec/bootstrap.rb + diff --git a/Rakefile b/Rakefile index ee1aa40..6721c92 100644 --- a/Rakefile +++ b/Rakefile @@ -3,10 +3,10 @@ require 'rake/testtask' Rake::TestTask.new do |t| - t.libs.push "lib" - t.test_files = FileList['test/test_*.rb', 'test/spec_*.rb'] + t.libs.push 'lib' + t.test_files = FileList['spec/spec_*.rb'] t.verbose = true end -desc "Run tests" +desc 'Run tests' task :default => :test diff --git a/lib/retailcrm.rb b/lib/retailcrm.rb index 765e908..793baba 100644 --- a/lib/retailcrm.rb +++ b/lib/retailcrm.rb @@ -1,11 +1,16 @@ # encoding: utf-8 +# noinspection RubyResolve require 'net/http' +# noinspection RubyResolve require 'net/https' +# noinspection RubyResolve require 'uri' +# noinspection RubyResolve require 'json' # RetailCRM API Client +# noinspection ALL class Retailcrm def initialize(url, key) @@ -33,7 +38,7 @@ class Retailcrm url = "#{@url}orders" @params[:limit] = limit @params[:page] = page - @filter = filter.to_a.map { |x| "filter[#{x[0]}]=#{x[1]}" }.join("&") + @filter = filter.to_a.map { |x| "filter[#{x[0]}]=#{x[1]}" }.join('&') make_request(url) end @@ -48,7 +53,7 @@ class Retailcrm # Arguments: # ids (Array) def orders_statuses(ids = []) - @ids = ids.map { |x| "ids[]=#{x}" }.join("&") + @ids = ids.map { |x| "ids[]=#{x}" }.join('&') url = "#{@url}orders/statuses" make_request(url) end @@ -185,7 +190,7 @@ class Retailcrm url = "#{@url}orders/packs/history" @params[:limit] = limit @params[:page] = page - @filter = filter.to_a.map { |x| "filter[#{x[0]}]=#{x[1]}" }.join("&") + @filter = filter.to_a.map { |x| "filter[#{x[0]}]=#{x[1]}" }.join('&') make_request(url) end @@ -205,7 +210,7 @@ class Retailcrm url = "#{@url}customers" @params[:limit] = limit @params[:page] = page - @filter = filter.to_a.map { |x| "filter[#{x[0]}]=#{x[1]}" }.join("&") + @filter = filter.to_a.map { |x| "filter[#{x[0]}]=#{x[1]}" }.join('&') make_request(url) end @@ -317,7 +322,7 @@ class Retailcrm url = "#{@url}store/inventories" @params[:limit] = limit @params[:page] = page - @filter = filter.to_a.map { |x| "filter[#{x[0]}]=#{x[1]}" }.join("&") + @filter = filter.to_a.map { |x| "filter[#{x[0]}]=#{x[1]}" }.join('&') make_request(url) end @@ -355,6 +360,7 @@ class Retailcrm def delivery_services_edit(delivery_service) code = delivery_service[:code] url = "#{@url}reference/delivery-services/#{code}/edit" + @params[:deliveryService] = delivery_service.to_json make_request(url, 'post') end @@ -373,6 +379,7 @@ class Retailcrm def delivery_types_edit(delivery_type) code = delivery_type[:code] url = "#{@url}reference/delivery-types/#{code}/edit" + @params[:deliveryType] = delivery_type.to_json make_request(url, 'post') end @@ -392,6 +399,7 @@ class Retailcrm def order_methods_edit(order_method) code = order_method[:code] url = "#{@url}reference/order-methods/#{code}/edit" + @params[:orderMethod] = order_method.to_json make_request(url, 'post') end @@ -411,6 +419,7 @@ class Retailcrm def order_types_edit(order_type) code = order_type[:code] url = "#{@url}reference/order-types/#{code}/edit" + @params[:orderType] = order_type.to_json make_request(url, 'post') end @@ -429,6 +438,7 @@ class Retailcrm def payment_statuses_edit(payment_status) code = payment_status[:code] url = "#{@url}reference/payment-statuses/#{code}/edit" + @params[:paymentStatus] = payment_status.to_json make_request(url, 'post') end @@ -447,7 +457,8 @@ class Retailcrm # def payment_types_edit(payment_type) code = payment_type[:code] - url = "#{@url}reference/payment-type/#{code}/edit" + url = "#{@url}reference/payment-types/#{code}/edit" + @params[:paymentType] = payment_type.to_json make_request(url, 'post') end @@ -467,6 +478,7 @@ class Retailcrm def product_statuses_edit(product_status) code = product_status[:code] url = "#{@url}reference/product-statuses/#{code}/edit" + @params[:productStatus] = product_status.to_json make_request(url, 'post') end @@ -485,6 +497,7 @@ class Retailcrm def sites_edit(site) code = site[:code] url = "#{@url}reference/sites/#{code}/edit" + @params[:site] = site.to_json make_request(url, 'post') end @@ -512,6 +525,7 @@ class Retailcrm def statuses_edit(status) code = status[:code] url = "#{@url}reference/statuses/#{code}/edit" + @params[:status] = status.to_json make_request(url, 'post') end @@ -531,6 +545,7 @@ class Retailcrm def stores_edit(store) code = store[:code] url = "#{@url}reference/stores/#{code}/edit" + @params[:store] = store.to_json make_request(url, 'post') end @@ -558,17 +573,14 @@ class Retailcrm elsif method == 'get' request = Net::HTTP::Get.new(uri.path) request.set_form_data(@params) + data = "#{request.body}" - if @filter.nil? - data = "#{request.body}" - else - data = "#{request.body}&#{@filter}" + unless @filter.nil? + data = data + "&#{@filter}" end - if @ids.nil? - data = "#{request.body}" - else - data = "#{request.body}&#{@ids}" + unless @ids.nil? + data = data + "&#{@ids}" end request = Net::HTTP::Get.new("#{uri.path}?#{data}") diff --git a/retailcrm.gemspec b/retailcrm.gemspec index 986d3c6..1246c69 100644 --- a/retailcrm.gemspec +++ b/retailcrm.gemspec @@ -1,6 +1,6 @@ Gem::Specification.new do |s| s.name = 'retailcrm' - s.version = '1.1.0' + s.version = '1.1.1' s.date = '2015-04-15' s.summary = 'RetailCRM Rest API client' s.description = 'Library for interact with RetailCRM API' diff --git a/spec/bootstrap-dist.rb b/spec/bootstrap-dist.rb new file mode 100644 index 0000000..34ceaee --- /dev/null +++ b/spec/bootstrap-dist.rb @@ -0,0 +1,207 @@ +# encoding: utf-8 + +class Bootstrap + + attr_reader :api_key, :api_url, :order, :customer, :refs_get, :refs_edit + + def initialize + @api_url = 'https://demo.retailcrm.ru' + @api_key = 'YourAPIKeyRightHere' + + id = Time.now.to_i + time = Time.now.strftime('%F %T') + + @customer = { + :externalId => id, + :createdAt => time, + :firstName => 'API', + :lastName => 'Test', + :email => 'pupkin@example.org', + :phones => [{:number => '+79099099090'}] + } + + @order = { + :create => { + :externalId => id, + :number => "#{id}", + :orderType => 'eshop-individual', + :orderMethod => 'phone', + :createdAt => time, + :discountPercent => 10, + :firstName => 'API', + :lastName => 'Test', + :customer => { + :firstName => 'Тестовый', + :lastName => 'Клиент', + :phones => [{:number => '+79099099090'}], + }, + :delivery => { + :code => 'courier', + :cost => 500, + :address => {:text => '344000, Ростов-на-Дону, пр. Буденовский, 13'} + }, + :status => 'new', + :items => [ + { + :productName => 'Товар 1', + :initialPrice => 500, + :quantity => 2 + }, + { + :productName => 'Товар 2', + :initialPrice => 1300, + :quantity => 1 + } + ] + }, + :edit => { + :externalId => 1428877985, + :email => 'spec@example.org', + :phone => '+79999999999', + :status => 'cancel-other', + :delivery => { + :code => 'ems', + :cost => 300, + } + }, + :mass => [ + { + :externalId => id, + :number => "#{id}", + :orderType => 'eshop-individual', + :orderMethod => 'phone', + :createdAt => time, + :discountPercent => 10, + :firstName => 'API', + :lastName => 'Test', + :customer => { + :firstName => 'Тестовый', + :lastName => 'Клиент', + :phones => [{:number => '+79099099090'}], + }, + :delivery => { + :code => 'courier', + :cost => 500, + :address => {:text => '344000, Ростов-на-Дону, пр. Буденовский, 13'} + }, + :status => 'new', + :items => [ + { + :productName => 'Товар 1', + :initialPrice => 500, + :quantity => 2 + }, + { + :productName => 'Товар 2', + :initialPrice => 1300, + :quantity => 1 + } + ] + }, + { + :externalId => id+1, + :number => "#{id+1}", + :orderType => 'eshop-individual', + :createdAt => time, + :discount => 200, + :firstName => 'API2', + :lastName => 'Test2', + :customer => { + :firstName => 'Тестовый2', + :lastName => 'Клиент2', + :phones => [{:number => '+79099099000'}], + }, + :delivery => { + :code => 'ems', + :cost => 500, + :address => {:text => '344000, Ростов-на-Дону, пр. Буденовский, 15'} + }, + :status => 'availability-confirmed', + :items => [ + { + :productName => 'Товар 3', + :initialPrice => 500, + :quantity => 2 + }, + { + :productName => 'Товар 4', + :initialPrice => 1300, + :quantity => 1 + } + ] + } + ] + } + + @refs_get = %w( + delivery_services + delivery_types + order_methods + order_types + payment_statuses + payment_types + product_statuses + status_groups + statuses + sites + stores + ) + + @refs_edit = { + delivery_types_edit: { + name: 'Rake delivery type', + code: 'rake-delivery-type', + defaultCost: 300, + defaultNetCost: 0, + description: 'Test delivery type through Rake test task' + }, + delivery_services_edit: { + name: 'Rake delivery service', + code: 'rake-delivery-service', + deliveryType: 'rake-delivery-type' + }, + order_methods_edit: { + name: 'Rake order method', + code: 'rake-order-method', + }, + order_types_edit: { + name: 'Rake order type', + code: 'rake-order-type', + }, + payment_statuses_edit: { + name: 'Rake payment status', + code: 'rake-payment-status', + description: 'Test payment status through Rake test task' + }, + payment_types_edit: { + name: 'Rake payment type', + code: 'rake-payment-type', + description: 'Test payment type through Rake test task' + }, + product_statuses_edit: { + ordering: 65, + name: 'Rake product status', + code: 'rake-payment-type', + cancelStatus: true, + orderStatusByProductStatus: 'cancel-other', + }, + statuses_edit: { + name: 'Rake status', + code: 'rake-status', + group: 'new', + }, + sites_edit: { + name: 'Rake Shop', + url: 'http://yandex.ru', + code: 'api-client-all', + loadFromYml: false, + }, + stores_edit: { + type: 'store-type-online', + code: 'rake-store', + name: 'Rake Store' + } + } + end +end + diff --git a/spec/spec_customers.rb b/spec/spec_customers.rb new file mode 100644 index 0000000..0896bd7 --- /dev/null +++ b/spec/spec_customers.rb @@ -0,0 +1,30 @@ +# encoding: utf-8 + +require 'minitest/autorun' +require 'minitest/spec' +require 'minitest-spec-context' +require 'pp' + +require_relative '../lib/retailcrm' +require_relative './bootstrap' + +describe Retailcrm do + + before do + @bootstrap = Bootstrap.new + @api = Retailcrm.new(@bootstrap.api_url, @bootstrap.api_key) + @customer = @bootstrap.customer + end + + describe '#customers' do + it 'responds with 200' do + res = @api.customers({:email => 'test@example.com'}, 50, 1) + res.status.to_i.must_equal 200 + res.response['customers'].must_be_instance_of Array + res.response['customers'].each do |x| + x['email'].must_equal 'test@example.com' + end + end + end + +end diff --git a/spec/spec_references.rb b/spec/spec_references.rb new file mode 100644 index 0000000..f71171f --- /dev/null +++ b/spec/spec_references.rb @@ -0,0 +1,39 @@ +# encoding: utf-8 + +require 'minitest/autorun' +require 'minitest/spec' +require 'minitest-spec-context' +require 'pp' + +require_relative '../lib/retailcrm' +require_relative './bootstrap' + +describe Retailcrm do + + before do + @bootstrap = Bootstrap.new + @api = Retailcrm.new(@bootstrap.api_url, @bootstrap.api_key) + @ref = @bootstrap.refs_edit + end + + # describe '#reference get' do + # it 'responds with 200' do + # @bootstrap.refs_get.each { |method| + # res = @api.public_send(method) + # res.status.to_i.must_equal 200 + # sleep 0.3 + # } + # end + # end + # + # describe '#reference edit' do + # it 'responds with 200 or 201' do + # @bootstrap.refs_edit.each do |method, data| + # res = @api.public_send(method, data) + # res.status.to_i.must_be :<, 202 + # sleep 0.3 + # end + # end + # end + +end diff --git a/spec/spec_retailcrm.rb b/spec/spec_retailcrm.rb new file mode 100644 index 0000000..bed4bfb --- /dev/null +++ b/spec/spec_retailcrm.rb @@ -0,0 +1,16 @@ +# encoding: utf-8 + +require 'minitest/autorun' +require 'minitest/spec' + +require_relative '../lib/retailcrm' +require_relative './bootstrap' + +describe Retailcrm do + + before do + @bootstrap = Bootstrap.new + @api = Retailcrm.new(@bootstrap.api_url, @bootstrap.api_key) + end + +end diff --git a/test/bootstrap.rb.dist b/test/bootstrap.rb.dist deleted file mode 100644 index a7b1fff..0000000 --- a/test/bootstrap.rb.dist +++ /dev/null @@ -1,133 +0,0 @@ -# encoding: utf-8 - -class Bootstrap - - attr_reader :api_key, :api_url, :order, :order_edit, :orders, :customer - - def initialize - @api_url = 'https://demo.retailcrm.ru' - @api_key = '' - - id = Time.now.to_i - time = Time.now.strftime('%F %T') - - @customer = { - :externalId => id, - :createdAt => time, - :firstName => 'API', - :lastName => 'Test', - :email => 'pupkin@example.org', - :phones => [{:number => '+79099099090'}] - } - - @order = { - :externalId => id, - :number => "#{id}", - :orderType => 'eshop-individual', - :orderMethod => 'phone', - :createdAt => time, - :discountPercent => 10, - :firstName => 'API', - :lastName => 'Test', - :customer => { - :firstName => 'Тестовый', - :lastName => 'Клиент', - :phones => [{:number => '+79099099090'}], - }, - :delivery => { - :code => 'courier', - :cost => 500, - :address => {:text => '344000, Ростов-на-Дону, пр. Буденовский, 13'} - }, - :status => 'new', - :items => [ - { - :productName => 'Товар 1', - :initialPrice => 500, - :quantity => 2 - }, - { - :productName => 'Товар 2', - :initialPrice => 1300, - :quantity => 1 - } - ] - } - - @order_edit = { - :externalId => 1428877985, - :email => 'test@example.org', - :phone => '+79999999999', - :status => 'cancel-other', - } - - @orders = [ - { - :externalId => id, - :number => "#{id}", - :orderType => 'eshop-individual', - :orderMethod => 'phone', - :createdAt => time, - :discountPercent => 10, - :firstName => 'API', - :lastName => 'Test', - :customer => { - :firstName => 'Тестовый', - :lastName => 'Клиент', - :phones => [{:number => '+79099099090'}], - }, - :delivery => { - :code => 'courier', - :cost => 500, - :address => {:text => '344000, Ростов-на-Дону, пр. Буденовский, 13'} - }, - :status => 'new', - :items => [ - { - :productName => 'Товар 1', - :initialPrice => 500, - :quantity => 2 - }, - { - :productName => 'Товар 2', - :initialPrice => 1300, - :quantity => 1 - } - ] - }, - { - :externalId => id+1, - :number => "#{id+1}", - :orderType => 'eshop-individual', - :createdAt => time, - :discount => 200, - :firstName => 'API2', - :lastName => 'Test2', - :customer => { - :firstName => 'Тестовый2', - :lastName => 'Клиент2', - :phones => [{:number => '+79099099000'}], - }, - :delivery => { - :code => 'ems', - :cost => 500, - :address => {:text => '344000, Ростов-на-Дону, пр. Буденовский, 15'} - }, - :status => 'availability-confirmed', - :items => [ - { - :productName => 'Товар 3', - :initialPrice => 500, - :quantity => 2 - }, - { - :productName => 'Товар 4', - :initialPrice => 1300, - :quantity => 1 - } - ] - } - ] - end -end - diff --git a/test/spec_retailcrm.rb b/test/spec_retailcrm.rb deleted file mode 100644 index 9913c8f..0000000 --- a/test/spec_retailcrm.rb +++ /dev/null @@ -1,26 +0,0 @@ -# encoding: utf-8 - -require 'minitest/autorun' -require 'minitest/spec' - -require_relative '../lib/retailcrm' -require_relative './bootstrap' - -describe Retailcrm do - before do - @boot = Bootstrap.new - @api = Retailcrm.new(@boot.api_url, @boot.api_key) - end - - describe '#delivery services status code' do - it 'checks successful status code of delivery services' do - @api.delivery_services.status.to_i.must_be :<, 400 - end - end - - describe '#delivery types status code' do - it 'checks successful status code of delivery types' do - @api.delivery_types.status.to_i.must_be :<, 400 - end - end -end diff --git a/test/test_retailcrm.rb b/test/test_retailcrm.rb deleted file mode 100644 index 6c13218..0000000 --- a/test/test_retailcrm.rb +++ /dev/null @@ -1,27 +0,0 @@ -# encoding: utf-8 - -require 'minitest/autorun' -require_relative '../lib/retailcrm' -require_relative './bootstrap' - -class RetailcrmTest < Minitest::Test - def setup - @boot = Bootstrap.new - @api = Retailcrm.new(@boot.api_url, @boot.api_key) - end - - def test_delivery_services - assert_equal true, - @api.delivery_services.is_successfull? - end - - def test_delivery_types - assert_equal true, - @api.delivery_types.is_successfull? - end - - def test_order_methods - assert_equal true, - @api.order_methods.is_successfull? - end -end