fix references edit methods, fix make request to proper collect of ids & filters, referenses tests added

This commit is contained in:
Alex Lushpai 2015-07-16 18:00:51 +03:00
parent a0ebbe5b38
commit d21553e681
11 changed files with 324 additions and 205 deletions

3
.gitignore vendored
View File

@ -1,2 +1,3 @@
*.gem
/test/bootstrap.rb
spec/bootstrap.rb

View File

@ -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

View File

@ -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)
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}")

View File

@ -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'

207
spec/bootstrap-dist.rb Normal file
View File

@ -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

30
spec/spec_customers.rb Normal file
View File

@ -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

39
spec/spec_references.rb Normal file
View File

@ -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

16
spec/spec_retailcrm.rb Normal file
View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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