moysklad 0.2.1 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.github/workflows/rspec.yml +29 -0
- data/.gitignore +1 -0
- data/README.md +14 -3
- data/lib/moysklad/client/errors.rb +60 -30
- data/lib/moysklad/client.rb +39 -11
- data/lib/moysklad/entities/assortment.rb +4 -0
- data/lib/moysklad/entities/attribute.rb +111 -37
- data/lib/moysklad/entities/attribute_metadata.rb +7 -31
- data/lib/moysklad/entities/attribute_value.rb +18 -0
- data/lib/moysklad/entities/base.rb +23 -8
- data/lib/moysklad/entities/bundle.rb +39 -0
- data/lib/moysklad/entities/characteristic.rb +4 -0
- data/lib/moysklad/entities/characteristic_metadata.rb +4 -0
- data/lib/moysklad/entities/collection.rb +27 -7
- data/lib/moysklad/entities/collection_meta.rb +11 -0
- data/lib/moysklad/entities/company_settings_metadata.rb +26 -0
- data/lib/moysklad/entities/consignment.rb +16 -17
- data/lib/moysklad/entities/context.rb +7 -0
- data/lib/moysklad/entities/counterparty.rb +20 -0
- data/lib/moysklad/entities/country.rb +2 -8
- data/lib/moysklad/entities/currency.rb +27 -0
- data/lib/moysklad/entities/custom_entity.rb +17 -10
- data/lib/moysklad/entities/customer_order.rb +109 -23
- data/lib/moysklad/entities/customer_order_position.rb +6 -18
- data/lib/moysklad/entities/employee.rb +5 -0
- data/lib/moysklad/entities/entity.rb +39 -0
- data/lib/moysklad/entities/group.rb +10 -0
- data/lib/moysklad/entities/image.rb +16 -0
- data/lib/moysklad/entities/meta.rb +12 -0
- data/lib/moysklad/entities/organization.rb +17 -0
- data/lib/moysklad/entities/owner.rb +4 -0
- data/lib/moysklad/entities/price.rb +4 -8
- data/lib/moysklad/entities/price_type.rb +0 -8
- data/lib/moysklad/entities/product.rb +47 -0
- data/lib/moysklad/entities/productfolder.rb +11 -0
- data/lib/moysklad/entities/rate.rb +6 -0
- data/lib/moysklad/entities/resource_metadata.rb +8 -0
- data/lib/moysklad/entities/service.rb +23 -0
- data/lib/moysklad/entities/shortcut.rb +9 -0
- data/lib/moysklad/entities/store.rb +9 -0
- data/lib/moysklad/entities/time.rb +13 -0
- data/lib/moysklad/entities/uom.rb +5 -0
- data/lib/moysklad/entities/variant.rb +47 -0
- data/lib/moysklad/entities.rb +42 -39
- data/lib/moysklad/error.rb +1 -0
- data/lib/moysklad/resources/assortments.rb +13 -0
- data/lib/moysklad/resources/base.rb +35 -46
- data/lib/moysklad/resources/custom_entities.rb +29 -0
- data/lib/moysklad/resources/custom_entity_metadata.rb +12 -8
- data/lib/moysklad/resources/embedded_entity_metadata.rb +2 -3
- data/lib/moysklad/resources/indexed.rb +21 -37
- data/lib/moysklad/resources/indexed_cache.rb +36 -0
- data/lib/moysklad/resources/load_all.rb +21 -0
- data/lib/moysklad/resources/positions.rb +15 -0
- data/lib/moysklad/resources/products.rb +10 -0
- data/lib/moysklad/resources/stock.rb +1 -1
- data/lib/moysklad/resources/subresource.rb +0 -1
- data/lib/moysklad/resources/where_filter.rb +0 -1
- data/lib/moysklad/resources.rb +26 -11
- data/lib/moysklad/universe.rb +16 -2
- data/lib/moysklad/version.rb +1 -1
- data/lib/moysklad.rb +12 -7
- data/moysklad.gemspec +5 -3
- data/scripts/rest.sh +5 -2
- data/spec/lib/moysklad/entities/customer_order_position_spec.rb +4 -17
- data/spec/lib/moysklad/entities/customer_order_spec.rb +8 -33
- data/spec/lib/moysklad/resources/base_spec.rb +0 -54
- data/spec/lib/moysklad/resources_spec.rb +0 -2
- data/spec/lib/moysklad/universe_spec.rb +0 -23
- data/spec/lib/moysklad_spec.rb +0 -1
- data/spec/spec_helper.rb +2 -1
- data/spec/support/resource.rb +1 -66
- data/test.rb +177 -0
- metadata +89 -172
- data/.travis.yml +0 -4
- data/lib/moysklad/entities/barcode.rb +0 -13
- data/lib/moysklad/entities/common.rb +0 -15
- data/lib/moysklad/entities/common_object.rb +0 -29
- data/lib/moysklad/entities/company.rb +0 -20
- data/lib/moysklad/entities/company_concern.rb +0 -20
- data/lib/moysklad/entities/contact.rb +0 -15
- data/lib/moysklad/entities/custom_entity_metadata.rb +0 -36
- data/lib/moysklad/entities/embedded_entity_metadata.rb +0 -18
- data/lib/moysklad/entities/error.rb +0 -13
- data/lib/moysklad/entities/feature.rb +0 -20
- data/lib/moysklad/entities/good.rb +0 -34
- data/lib/moysklad/entities/good_folder.rb +0 -9
- data/lib/moysklad/entities/good_ref.rb +0 -12
- data/lib/moysklad/entities/my_company.rb +0 -12
- data/lib/moysklad/entities/sale_price.rb +0 -18
- data/lib/moysklad/entities/sale_prices.rb +0 -10
- data/lib/moysklad/entities/slot.rb +0 -10
- data/lib/moysklad/entities/stock_to.rb +0 -33
- data/lib/moysklad/entities/warehouse.rb +0 -15
- data/lib/moysklad/entities/xml_fix.rb +0 -15
- data/spec/fixtures/302.raw +0 -15
- data/spec/fixtures/400.raw +0 -8
- data/spec/fixtures/401.html +0 -1
- data/spec/fixtures/401_2.html +0 -1
- data/spec/fixtures/405.raw +0 -9
- data/spec/fixtures/500.raw +0 -8
- data/spec/fixtures/502.raw +0 -8
- data/spec/fixtures/Consignment_list_0.raw +0 -14
- data/spec/fixtures/Consignment_list_1000.raw +0 -14
- data/spec/fixtures/Country_list.raw +0 -14
- data/spec/fixtures/CustomEntityMetadata_list.raw +0 -14
- data/spec/fixtures/CustomEntity_list.raw +0 -14
- data/spec/fixtures/CustomEntity_list_bad_chars.raw +0 -17
- data/spec/fixtures/EmbeddedEntityMetadata_list.raw +0 -15
- data/spec/fixtures/Feature_list.raw +0 -14
- data/spec/fixtures/Feature_list_1000.raw +0 -14
- data/spec/fixtures/Good_e932ebe1-2e22-11e4-9406-002590a28eca.raw +0 -12
- data/spec/fixtures/Good_eb77ad57-2e22-11e4-4030-002590a28eca.raw +0 -12
- data/spec/fixtures/Goods_list.raw +0 -14
- data/spec/fixtures/Metadata_list.raw +0 -14
- data/spec/fixtures/PriceType_list.raw +0 -14
- data/spec/fixtures/Stock.raw +0 -11
- data/spec/fixtures/Stock_showConsignments.raw +0 -11
- data/spec/fixtures/Uom_list.raw +0 -14
- data/spec/fixtures/Warehouse_list.raw +0 -14
- data/spec/fixtures/attribute_boolean.xml +0 -6
- data/spec/fixtures/attribute_dictionary.xml +0 -5
- data/spec/fixtures/attribute_double.xml +0 -6
- data/spec/fixtures/attribute_file.xml +0 -12
- data/spec/fixtures/attribute_long.xml +0 -6
- data/spec/fixtures/attribute_text.xml +0 -5
- data/spec/fixtures/attribute_time.xml +0 -6
- data/spec/fixtures/attribute_url.xml +0 -6
- data/spec/fixtures/bad_chars.xml +0 -6
- data/spec/fixtures/customer_order.xml +0 -28
- data/spec/fixtures/good_with_attributes.raw +0 -12
- data/spec/fixtures/resource_forbidden.raw +0 -9
- data/spec/fixtures/wrong_password.raw +0 -9
- data/spec/fixtures/wrong_type.raw +0 -70
- data/spec/fixtures/wrong_user.raw +0 -9
- data/spec/lib/moysklad/client/errors_spec.rb +0 -102
- data/spec/lib/moysklad/entities/attribute_metadata_spec.rb +0 -30
- data/spec/lib/moysklad/entities/attribute_spec.rb +0 -36
- data/spec/lib/moysklad/entities/country_spec.rb +0 -25
- data/spec/lib/moysklad/entities/custom_entity_metadata_spec.rb +0 -34
- data/spec/lib/moysklad/entities/custom_entity_spec.rb +0 -34
- data/spec/lib/moysklad/entities/good_spec.rb +0 -46
- data/spec/lib/moysklad/resources/countries_spec.rb +0 -22
- data/spec/lib/moysklad/resources/custom_entity_metadata_spec.rb +0 -13
- data/spec/lib/moysklad/resources/embedded_entity_metadata_indexed_spec.rb +0 -20
- data/spec/lib/moysklad/resources/embedded_entity_metadata_spec.rb +0 -11
- data/spec/lib/moysklad/resources/good_folders_spec.rb +0 -17
- data/spec/lib/moysklad/resources/indexed_spec.rb +0 -61
- data/spec/lib/moysklad/resources/stock_spec.rb +0 -63
- data/spec/lib/moysklad/resources/warehouses_spec.rb +0 -20
@@ -0,0 +1,47 @@
|
|
1
|
+
module Moysklad::Entities
|
2
|
+
class Variant < Entity
|
3
|
+
|
4
|
+
attribute :accountId, String
|
5
|
+
|
6
|
+
attribute :characteristics, Array[Characteristic]
|
7
|
+
attribute :minPrice, Float
|
8
|
+
attribute :buyPrice, Float
|
9
|
+
attribute :salePrices, Array[Price]
|
10
|
+
attribute :product, Shortcut
|
11
|
+
attribute :things, Array[String]
|
12
|
+
|
13
|
+
# Когда загружаем через ассортименты эти поля устанавливаются
|
14
|
+
attribute :stock, Float
|
15
|
+
attribute :reserve, Float
|
16
|
+
attribute :inTransit, Float
|
17
|
+
attribute :quantity, Float
|
18
|
+
|
19
|
+
#characteristics - Характеристики Модификации Необходимое
|
20
|
+
#minPrice - Минимальная цена
|
21
|
+
#buyPrice - Закупочная цена
|
22
|
+
#salePrices - Цены продажи
|
23
|
+
#product - Метаданные, представляющие с собой ссылку на товар, к которому привязана Модификация. Необходимое
|
24
|
+
#things - Серийные номера Только для чтения
|
25
|
+
|
26
|
+
#attribute :charactideristics,
|
27
|
+
#attribute :minPrice
|
28
|
+
|
29
|
+
#tag 'feature'
|
30
|
+
|
31
|
+
#attribute :goodUuid, String
|
32
|
+
|
33
|
+
#attribute :archived, Boolean
|
34
|
+
#attribute :isDefault, Boolean
|
35
|
+
|
36
|
+
#element :shared, Boolean
|
37
|
+
|
38
|
+
#has_many :attributes, Moysklad::Entities::Attribute
|
39
|
+
#has_many :barcodes, Moysklad::Entities::Barcode
|
40
|
+
|
41
|
+
#def good universe
|
42
|
+
#cache :good, universe do
|
43
|
+
#universe.good.find goodUuid
|
44
|
+
#end
|
45
|
+
#end
|
46
|
+
end
|
47
|
+
end
|
data/lib/moysklad/entities.rb
CHANGED
@@ -1,46 +1,49 @@
|
|
1
1
|
module Moysklad
|
2
2
|
module Entities
|
3
|
-
extend ActiveSupport::Autoload
|
4
|
-
|
5
|
-
autoload :XmlFix
|
6
|
-
autoload :CommonObject
|
7
|
-
autoload :CompanyConcern
|
8
|
-
autoload :Common
|
9
|
-
|
10
3
|
%w{
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
4
|
+
base
|
5
|
+
meta
|
6
|
+
entity
|
7
|
+
country
|
8
|
+
assortment
|
9
|
+
attribute_value
|
10
|
+
attribute
|
11
|
+
attribute_metadata
|
12
|
+
owner
|
13
|
+
group
|
14
|
+
productfolder
|
15
|
+
uom
|
16
|
+
currency
|
17
|
+
price
|
18
|
+
image
|
19
|
+
employee
|
20
|
+
context
|
21
|
+
product
|
22
|
+
bundle
|
23
|
+
characteristic
|
24
|
+
characteristic_metadata
|
25
|
+
collection_meta
|
26
|
+
collection
|
27
|
+
company_settings_metadata
|
28
|
+
consignment
|
29
|
+
contract
|
30
|
+
counterparty
|
31
|
+
custom_entity
|
32
|
+
rate
|
33
|
+
customer_order
|
34
|
+
customer_order_position
|
35
|
+
organization
|
36
|
+
page
|
37
|
+
price_type
|
38
|
+
rate
|
39
|
+
resource_metadata
|
40
|
+
service
|
41
|
+
shortcut
|
42
|
+
store
|
43
|
+
time
|
44
|
+
variant
|
41
45
|
}.each do |m|
|
42
|
-
|
46
|
+
require_relative "entities/#{m}"
|
43
47
|
end
|
44
48
|
end
|
45
|
-
|
46
49
|
end
|
@@ -0,0 +1 @@
|
|
1
|
+
Moysklad::Error = Class.new StandardError
|
@@ -1,7 +1,9 @@
|
|
1
1
|
class Moysklad::Resources::Base
|
2
|
-
|
2
|
+
include Moysklad::Resources::Positions
|
3
3
|
|
4
|
-
|
4
|
+
PREFIX_PATH = 'entity/'
|
5
|
+
|
6
|
+
def self.inherited superclass
|
5
7
|
super
|
6
8
|
Moysklad::Resources.register_resource superclass
|
7
9
|
end
|
@@ -9,35 +11,33 @@ class Moysklad::Resources::Base
|
|
9
11
|
# Возвращает этот-же ресурс только индексированный
|
10
12
|
#
|
11
13
|
# @return [Moysklad::Resources::Indexed]
|
12
|
-
def self.indexed
|
13
|
-
Moysklad::Resources::Indexed.new new(
|
14
|
+
def self.indexed client: nil, list_path: nil
|
15
|
+
Moysklad::Resources::Indexed.new new(client: client, list_path: list_path)
|
14
16
|
end
|
15
17
|
|
16
18
|
# https://support.moysklad.ru/hc/ru/articles/203404253-REST-сервис-синхронизации-данных
|
17
|
-
def initialize client: nil
|
19
|
+
def initialize client: nil, list_path: nil
|
20
|
+
@list_path = list_path
|
18
21
|
raise "Должен быть Moysklad::Client" unless client.is_a? Moysklad::Client
|
19
22
|
@client = client
|
20
23
|
end
|
21
24
|
|
25
|
+
def metadata
|
26
|
+
Moysklad::Entities::ResourceMetadata.build client.get(metadata_path), self
|
27
|
+
end
|
28
|
+
|
22
29
|
# Возвращает список элементов как есть
|
23
30
|
#
|
24
31
|
# @return [Array of Moysklad::Entities::Base]
|
25
32
|
def list params={}
|
26
|
-
|
27
|
-
end
|
28
|
-
|
29
|
-
# Возвращает страницу со списком элементов
|
30
|
-
#
|
31
|
-
# @return [Moysklad::Entities::Page]
|
32
|
-
def page params={}
|
33
|
-
parse_page client.get list_path, params
|
33
|
+
load_collection client.get list_path, params
|
34
34
|
end
|
35
35
|
|
36
36
|
# Забираем элемент по uuid
|
37
37
|
#
|
38
38
|
# @return [Moysklad::Entities::Base]
|
39
|
-
def get uuid
|
40
|
-
|
39
|
+
def get uuid, params = {}
|
40
|
+
parse_get client.get item_path(uuid), params
|
41
41
|
end
|
42
42
|
|
43
43
|
# Модифицируем элемент по uuid
|
@@ -56,14 +56,14 @@ class Moysklad::Resources::Base
|
|
56
56
|
# @return [Moysklad::Entities::Base]
|
57
57
|
def create model
|
58
58
|
raise "Должна быть модель типа Moysklad::Entities::Base" unless model.is_a? Moysklad::Entities::Base
|
59
|
-
|
59
|
+
parse_get client.post create_path, model.to_json
|
60
60
|
end
|
61
61
|
|
62
62
|
# Удаляем запись по uuid
|
63
63
|
#
|
64
64
|
# @param uuid
|
65
65
|
def delete uuid
|
66
|
-
client.delete item_path uuid
|
66
|
+
client.delete item_path uuid
|
67
67
|
end
|
68
68
|
|
69
69
|
def self.type
|
@@ -74,38 +74,16 @@ class Moysklad::Resources::Base
|
|
74
74
|
ActiveSupport::Inflector.underscore ActiveSupport::Inflector.pluralize type
|
75
75
|
end
|
76
76
|
|
77
|
-
def self.entity_class
|
78
|
-
ActiveSupport::Inflector.constantize "Moysklad::Entities::#{type.to_s}"
|
79
|
-
end
|
80
|
-
|
81
77
|
private
|
82
78
|
|
83
79
|
attr_reader :client
|
84
80
|
|
85
|
-
def
|
86
|
-
|
87
|
-
resource.to_xml.to_s
|
88
|
-
else
|
89
|
-
resource.to_s
|
90
|
-
end
|
81
|
+
def parse_get data
|
82
|
+
entity_class.build data, self
|
91
83
|
end
|
92
84
|
|
93
|
-
def
|
94
|
-
|
95
|
-
end
|
96
|
-
|
97
|
-
def parse_content content
|
98
|
-
Nokogiri::XML content
|
99
|
-
end
|
100
|
-
|
101
|
-
def parse_page content
|
102
|
-
col = Moysklad::Entities::Collection.parse parse_content content
|
103
|
-
|
104
|
-
# TODO Парсится два раза. Оптимизировать. Например сделать динамические CollectionFeature
|
105
|
-
# и парсить через них
|
106
|
-
|
107
|
-
items = parse content
|
108
|
-
Moysklad::Entities::Page.new items, col.total, col.start, col.count
|
85
|
+
def load_collection data
|
86
|
+
collection_class.build data, self
|
109
87
|
end
|
110
88
|
|
111
89
|
def item_path uuid
|
@@ -117,11 +95,22 @@ class Moysklad::Resources::Base
|
|
117
95
|
end
|
118
96
|
|
119
97
|
def list_path
|
120
|
-
|
98
|
+
@list_path ||= prefix_path
|
121
99
|
end
|
122
100
|
|
123
|
-
def
|
124
|
-
|
101
|
+
def metadata_path
|
102
|
+
prefix_path + '/metadata'
|
103
|
+
end
|
104
|
+
|
105
|
+
def collection_class
|
106
|
+
Moysklad::Entities::Collection
|
107
|
+
end
|
108
|
+
|
109
|
+
def entity_class
|
110
|
+
ActiveSupport::Inflector.constantize "Moysklad::Entities::#{self.class.type.to_s}"
|
125
111
|
end
|
126
112
|
|
113
|
+
def prefix_path
|
114
|
+
PREFIX_PATH + self.class.type.downcase
|
115
|
+
end
|
127
116
|
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module Moysklad::Resources
|
2
|
+
# Элементы словаря
|
3
|
+
class CustomEntities < Base
|
4
|
+
# custom_entity_meta_id - id словаря, элементы которого хотим получить
|
5
|
+
def initialize(custom_entity_meta_id: nil, client: nil, list_path: nil)
|
6
|
+
raise 'Client empty' if client.nil? # support ruby 2.0.0
|
7
|
+
|
8
|
+
@custom_entity_meta_id = custom_entity_meta_id
|
9
|
+
super client: client, list_path: list_path
|
10
|
+
end
|
11
|
+
|
12
|
+
# Публичный чтобы был доступен из индекса
|
13
|
+
def cache_key
|
14
|
+
[list_path, custom_entity_meta_id].join(':')
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
attr_reader :custom_entity_meta_id
|
20
|
+
|
21
|
+
def item_path
|
22
|
+
raise
|
23
|
+
end
|
24
|
+
|
25
|
+
def list_path
|
26
|
+
@list_path || (prefix_path + '/' + custom_entity_meta_id.to_s)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -1,18 +1,22 @@
|
|
1
1
|
module Moysklad::Resources
|
2
|
-
# https://online.moysklad.ru/exchange/rest/ms/xml/CustomEntityMetadata/list
|
3
2
|
# Список аттрибутов, которые используются как свойства товара
|
3
|
+
# https://online.moysklad.ru/api/remap/1.1/entity/companysettings/metadata/
|
4
|
+
# https://online.moysklad.ru/api/remap/1.1/entity/variant/metadata/
|
5
|
+
# https://online.moysklad.ru/api/remap/1.1/entity/product/metadata/
|
4
6
|
class CustomEntityMetadata < Base
|
5
|
-
def self.entity_class
|
6
|
-
Moysklad::Entities::CustomEntityMetadata
|
7
|
-
end
|
8
7
|
|
9
|
-
def
|
10
|
-
|
8
|
+
def list(_params)
|
9
|
+
raise
|
11
10
|
end
|
12
11
|
|
13
|
-
|
14
|
-
|
12
|
+
private
|
13
|
+
|
14
|
+
def prefix_path
|
15
|
+
'entity/companysettings/metadata/customEntities/'
|
15
16
|
end
|
16
17
|
|
18
|
+
def list_path
|
19
|
+
raise 'Список напрямую получить не возможно'
|
20
|
+
end
|
17
21
|
end
|
18
22
|
end
|
@@ -12,9 +12,8 @@ module Moysklad::Resources
|
|
12
12
|
:embedded_entity_metadata
|
13
13
|
end
|
14
14
|
|
15
|
-
def self.indexed
|
16
|
-
Moysklad::Resources::EmbeddedEntityMetadataIndexed.new new(
|
15
|
+
def self.indexed client: nil, list_path: nil
|
16
|
+
Moysklad::Resources::EmbeddedEntityMetadataIndexed.new new(client: client, list_path: list_path)
|
17
17
|
end
|
18
|
-
|
19
18
|
end
|
20
19
|
end
|
@@ -1,30 +1,35 @@
|
|
1
|
-
require_relative 'where_filter'
|
2
|
-
|
3
1
|
module Moysklad::Resources
|
4
2
|
class Indexed < SimpleDelegator
|
3
|
+
WrongEntriesCountError = Class.new StandardError
|
4
|
+
NoIdInEntity = Class.new StandardError
|
5
|
+
|
5
6
|
include WhereFilter
|
7
|
+
include LoadAll
|
8
|
+
include IndexedCache
|
6
9
|
|
7
10
|
def initialize resource
|
8
|
-
raise 'resource должен быть Moysklad::Resources::Base' unless resource.is_a? Moysklad::Resources::Base
|
11
|
+
raise TypeError, 'resource должен быть Moysklad::Resources::Base' unless resource.is_a? Moysklad::Resources::Base
|
9
12
|
super resource
|
10
13
|
end
|
11
14
|
|
12
15
|
# Автоматически подгружает постранично данныез из API и возвращает их все сразу.
|
13
16
|
#
|
14
17
|
# @return [Array of Moysklad::Entities::Base]
|
15
|
-
def all
|
16
|
-
|
18
|
+
def all(params = {})
|
19
|
+
cache_fetch do
|
20
|
+
pull_list(params)
|
21
|
+
end
|
17
22
|
end
|
18
23
|
|
19
24
|
# Возвращает запрашивемую запись из кеша.
|
20
25
|
# Предварительно подгружает все записи через метод `all`
|
21
26
|
#
|
22
27
|
# @return Moyskald::Entities::Base
|
23
|
-
def find
|
24
|
-
index[
|
28
|
+
def find id
|
29
|
+
index[id]
|
25
30
|
end
|
26
31
|
|
27
|
-
# Перечень
|
32
|
+
# Перечень id-ов всех элементов в ресуресе
|
28
33
|
#
|
29
34
|
# @return [Array of uuids]
|
30
35
|
def uuids
|
@@ -45,42 +50,21 @@ module Moysklad::Resources
|
|
45
50
|
end
|
46
51
|
|
47
52
|
def index
|
48
|
-
pull_list unless
|
49
|
-
|
50
|
-
end
|
51
|
-
|
52
|
-
def pull_list
|
53
|
-
@cached_list = load_full_list
|
54
|
-
@_index = prepare_index @cached_list
|
55
|
-
@cached_list
|
53
|
+
pull_list unless cached_index
|
54
|
+
cached_index
|
56
55
|
end
|
57
56
|
|
58
|
-
def
|
59
|
-
|
60
|
-
list = []
|
61
|
-
|
62
|
-
_page = nil
|
63
|
-
|
64
|
-
begin
|
65
|
-
_page = page start: start
|
66
|
-
list += _page.items
|
67
|
-
break if _page.items.empty?
|
68
|
-
start = list.count
|
69
|
-
end while start<_page.total
|
70
|
-
|
71
|
-
raise "При загрузке коллекции в результате колиество не совпадает с total: #{list.count}<>#{_page.total}" unless list.count==_page.total
|
72
|
-
|
73
|
-
list
|
57
|
+
def pull_list(params={})
|
58
|
+
load_all(params)
|
74
59
|
end
|
75
60
|
|
76
|
-
def prepare_index
|
61
|
+
def prepare_index list
|
77
62
|
i={}
|
78
|
-
|
79
|
-
raise "У объекта нет
|
80
|
-
i[r.
|
63
|
+
list.each do |r|
|
64
|
+
raise NoIdInEntity, "У объекта нет id: #{r}" unless r.respond_to?(:id) && r.id
|
65
|
+
i[r.id]=r
|
81
66
|
end
|
82
67
|
return i
|
83
68
|
end
|
84
|
-
|
85
69
|
end
|
86
70
|
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module IndexedCache
|
2
|
+
def cache!
|
3
|
+
all.count
|
4
|
+
end
|
5
|
+
|
6
|
+
private
|
7
|
+
|
8
|
+
def cache_fetch
|
9
|
+
return cache[:list] if cache[:list]
|
10
|
+
cache[:list] = yield
|
11
|
+
cache[:index] = prepare_index cache[:list]
|
12
|
+
|
13
|
+
cache[:list]
|
14
|
+
end
|
15
|
+
|
16
|
+
def cache
|
17
|
+
@cache ||= {}
|
18
|
+
@cache[cache_key] ||= { index: nil, list: nil}
|
19
|
+
end
|
20
|
+
|
21
|
+
def cached_index
|
22
|
+
cache[:index]
|
23
|
+
end
|
24
|
+
|
25
|
+
def cache_index(index)
|
26
|
+
cache[:index] = index
|
27
|
+
end
|
28
|
+
|
29
|
+
def cache_key
|
30
|
+
if __getobj__.respond_to? :cache_key
|
31
|
+
__getobj__.send :cache_key
|
32
|
+
else
|
33
|
+
:default
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Moysklad::Resources
|
2
|
+
module LoadAll
|
3
|
+
def load_all(params = {})
|
4
|
+
limit = 100
|
5
|
+
offset = 0
|
6
|
+
rows = []
|
7
|
+
collection = nil
|
8
|
+
|
9
|
+
begin
|
10
|
+
collection = list params.merge(limit: limit, offset: offset)
|
11
|
+
rows += collection.rows
|
12
|
+
break if collection.rows.empty?
|
13
|
+
offset = rows.count
|
14
|
+
end while offset<collection.meta.size
|
15
|
+
|
16
|
+
raise WrongEntriesCountError, "При загрузке коллекции в результате колиество не совпадает с total: #{rows.count}<>#{collection.meta.size}" unless rows.count==collection.meta.size
|
17
|
+
|
18
|
+
rows
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# API сервиса МойСклад позволяет оперировать с такими документами как Отгрузка, Заказ покупателя, Счёт покупателю, Розничная продажа.
|
2
|
+
# Перечисленные документы содержат позиции, работать с которыми можно как в составе отдельного документа, так и с помощью специальных ресурсов для управления позициями документа.
|
3
|
+
|
4
|
+
module Moysklad::Resources::Positions
|
5
|
+
# Создаем позицию документа по uuid
|
6
|
+
#
|
7
|
+
# @return [Moysklad::Entities::Base]
|
8
|
+
def create_position model_uuid, position
|
9
|
+
position.class.build client.post(create_position_path(model_uuid), position.to_json)
|
10
|
+
end
|
11
|
+
|
12
|
+
def create_position_path(model_uuid)
|
13
|
+
[prefix_path, model_uuid, 'positions'].join('/')
|
14
|
+
end
|
15
|
+
end
|
data/lib/moysklad/resources.rb
CHANGED
@@ -1,24 +1,39 @@
|
|
1
1
|
module Moysklad::Resources
|
2
2
|
mattr_accessor :resources
|
3
3
|
|
4
|
-
extend ActiveSupport::Autoload
|
5
|
-
|
6
4
|
def self.register_resource resource_class
|
7
5
|
self.resources ||= []
|
8
6
|
self.resources << resource_class
|
9
7
|
end
|
10
8
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
9
|
+
%w{
|
10
|
+
positions
|
11
|
+
base
|
12
|
+
load_all
|
13
|
+
assortments
|
14
|
+
custom_entities
|
15
|
+
custom_entity_metadata
|
16
|
+
embedded_entity_metadata
|
17
|
+
where_filter
|
18
|
+
indexed_cache
|
19
|
+
indexed
|
20
|
+
embedded_entity_metadata_indexed
|
21
|
+
products
|
22
|
+
stock
|
23
|
+
subresource
|
24
|
+
}.each do |m|
|
25
|
+
require_relative "resources/#{m}"
|
26
|
+
end
|
18
27
|
|
19
28
|
# Простые ресурсы, которые создаются автоматически
|
20
|
-
%w{
|
29
|
+
%w{Productfolders Uoms PriceType Countries Variants
|
30
|
+
Counterparty
|
31
|
+
Organization
|
32
|
+
Service
|
33
|
+
Bundle
|
34
|
+
CustomerOrders Store
|
35
|
+
Currency
|
36
|
+
Consignments Group}.each do |klass_name|
|
21
37
|
const_set klass_name, Class.new( Base )
|
22
38
|
end
|
23
|
-
|
24
39
|
end
|