moysklad 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +25 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +1 -0
  5. data/Gemfile +7 -0
  6. data/Guardfile +24 -0
  7. data/LICENSE.txt +22 -0
  8. data/README.md +35 -0
  9. data/Rakefile +8 -0
  10. data/lib/moysklad.rb +28 -0
  11. data/lib/moysklad/client.rb +46 -0
  12. data/lib/moysklad/client/errors.rb +72 -0
  13. data/lib/moysklad/entities.rb +36 -0
  14. data/lib/moysklad/entities/attribute.rb +11 -0
  15. data/lib/moysklad/entities/barcode.rb +13 -0
  16. data/lib/moysklad/entities/base.rb +6 -0
  17. data/lib/moysklad/entities/collection.rb +15 -0
  18. data/lib/moysklad/entities/common.rb +16 -0
  19. data/lib/moysklad/entities/common_object.rb +29 -0
  20. data/lib/moysklad/entities/company.rb +20 -0
  21. data/lib/moysklad/entities/company_concern.rb +20 -0
  22. data/lib/moysklad/entities/consignment.rb +22 -0
  23. data/lib/moysklad/entities/contract.rb +2 -0
  24. data/lib/moysklad/entities/customer_order.rb +38 -0
  25. data/lib/moysklad/entities/customer_order_position.rb +21 -0
  26. data/lib/moysklad/entities/embedded_entity_metadata.rb +16 -0
  27. data/lib/moysklad/entities/error.rb +13 -0
  28. data/lib/moysklad/entities/feature.rb +16 -0
  29. data/lib/moysklad/entities/good.rb +31 -0
  30. data/lib/moysklad/entities/good_ref.rb +12 -0
  31. data/lib/moysklad/entities/my_company.rb +12 -0
  32. data/lib/moysklad/entities/page.rb +5 -0
  33. data/lib/moysklad/entities/price.rb +15 -0
  34. data/lib/moysklad/entities/sale_price.rb +12 -0
  35. data/lib/moysklad/entities/sale_prices.rb +10 -0
  36. data/lib/moysklad/entities/stock_to.rb +33 -0
  37. data/lib/moysklad/entities/warehouse.rb +13 -0
  38. data/lib/moysklad/entities/xml_fix.rb +11 -0
  39. data/lib/moysklad/errors.rb +1 -0
  40. data/lib/moysklad/resources.rb +28 -0
  41. data/lib/moysklad/resources/base.rb +84 -0
  42. data/lib/moysklad/resources/indexed.rb +63 -0
  43. data/lib/moysklad/resources/stock.rb +24 -0
  44. data/lib/moysklad/universe.rb +29 -0
  45. data/lib/moysklad/version.rb +3 -0
  46. data/moysklad.gemspec +29 -0
  47. data/scripts/rest.sh +20 -0
  48. data/spec/fixtures/401.html +1 -0
  49. data/spec/fixtures/401_2.html +1 -0
  50. data/spec/fixtures/405.raw +9 -0
  51. data/spec/fixtures/505.html +7 -0
  52. data/spec/fixtures/Consignment_list_0.raw +14 -0
  53. data/spec/fixtures/Consignment_list_1000.raw +14 -0
  54. data/spec/fixtures/Feature_list.raw +14 -0
  55. data/spec/fixtures/Feature_list_1000.raw +14 -0
  56. data/spec/lib/moysklad/client/errors_spec.rb +10 -0
  57. data/spec/lib/moysklad/resources/base_spec.rb +36 -0
  58. data/spec/lib/moysklad/resources/indexed_spec.rb +33 -0
  59. data/spec/lib/moysklad/resources_spec.rb +9 -0
  60. data/spec/lib/moysklad/universe_spec.rb +19 -0
  61. data/spec/lib/moysklad_spec.rb +8 -0
  62. data/spec/spec_helper.rb +104 -0
  63. data/spec/support/resource.rb +52 -0
  64. metadata +236 -0
@@ -0,0 +1,20 @@
1
+ module Moysklad::Entities::CompanyConcern
2
+ extend ActiveSupport::Concern
3
+ included do
4
+ include HappyMapper
5
+ include Moysklad::Entities::CommonObject
6
+
7
+ attribute :created, Time
8
+ attribute :archived, HappyMapper::Boolean
9
+
10
+ attribute :companyType, String
11
+
12
+ attribute :discount, Float
13
+ attribute :autoDiscount, Float
14
+ attribute :discountCardNumber, String
15
+ attribute :discountCorrection, Float
16
+
17
+ element :code, String
18
+
19
+ end
20
+ end
@@ -0,0 +1,22 @@
1
+ module Moysklad::Entities
2
+ # https://online.moysklad.ru/exchange/rest/ms/xml/Consignment/list
3
+ class Consignment < Base
4
+ include Common
5
+
6
+ tag 'consignment'
7
+
8
+ attribute :updated, Time
9
+ attribute :updatedBy, String
10
+
11
+ attribute :name, String
12
+
13
+ attribute :goodUuid, String
14
+ attribute :isDefault, Boolean
15
+
16
+ attribute :archived, Boolean
17
+
18
+ element :externalcode, String
19
+
20
+ has_one :feature, Moysklad::Entities::Feature
21
+ end
22
+ end
@@ -0,0 +1,2 @@
1
+ # https://online.moysklad.ru/exchange/rest/ms/xml/Contract/list
2
+
@@ -0,0 +1,38 @@
1
+ module Moysklad::Entities
2
+ class CustomerOrder < Base
3
+ include CommonObject
4
+
5
+ tag 'customerOrder'
6
+
7
+ attribute :created, Time
8
+ attribute :createdBy, String
9
+
10
+ attribute :reservedSum, Float
11
+
12
+ attribute :sourceStoreUuid, String
13
+ attribute :sourceAccountUuid, String
14
+ attribute :sourceAgentUuid, String
15
+
16
+ attribute :targetAgentUuid, String
17
+ attribute :targetAccountUuid, String
18
+
19
+ attribute :applicable, Boolean
20
+ attribute :moment, Time
21
+
22
+ attribute :payerVat, Boolean
23
+
24
+ attribute :consignmentUuid, String
25
+
26
+ attribute :rate, Float
27
+
28
+ attribute :vatIncluded, Boolean
29
+ attribute :name, String
30
+
31
+ element :sum, Moysklad::Entities::Price, tag: :sum
32
+
33
+ element :deleted, Time
34
+
35
+ has_many :customerOrderPosition, Moysklad::Entities::CustomerOrderPosition
36
+
37
+ end
38
+ end
@@ -0,0 +1,21 @@
1
+ module Moysklad::Entities
2
+ class CustomerOrderPosition < Base
3
+ include HappyMapper
4
+
5
+ tag 'customerOrderPosition'
6
+
7
+ attribute :vat, Integer
8
+ attribute :goodUuid, String
9
+ attribute :consignmentUuid, String
10
+ attribute :quantity, Float
11
+ attribute :discount, Float
12
+ attribute :consignmentUuid, String
13
+
14
+ element :deleted, Time
15
+
16
+ element :basePrice, Moysklad::Entities::Price, tag: :basePrice
17
+ element :price, Moysklad::Entities::Price, tag: :price
18
+ element :reserve, Float
19
+ end
20
+
21
+ end
@@ -0,0 +1,16 @@
1
+ module Moysklad::Entities
2
+ # https://online.moysklad.ru/exchange/rest/ms/xml/Metadata/list
3
+ class EmbeddedEntityMetadata < Base
4
+ include CommonObject
5
+
6
+ tag 'embeddedEntityMetadata'
7
+
8
+ attribute :uniqueCode, Boolean
9
+ attribute :codeValueType, Integer
10
+ attribute :independentNameGenerator, Boolean
11
+ attribute :partialReserve, Boolean
12
+
13
+ element :code, String
14
+ end
15
+ end
16
+
@@ -0,0 +1,13 @@
1
+ module Moysklad::Entities
2
+ class Error < Base
3
+ include HappyMapper
4
+
5
+ tag 'error'
6
+
7
+ element :uid, String
8
+ element :moment, Time
9
+ element :message, String
10
+
11
+ element :stack, String
12
+ end
13
+ end
@@ -0,0 +1,16 @@
1
+ module Moysklad::Entities
2
+ class Feature < Base
3
+ include CommonObject
4
+
5
+ tag 'feature'
6
+
7
+ attribute :goodUuid, String
8
+
9
+ attribute :archived, Boolean
10
+
11
+ has_many :attribute, Moysklad::Entities::Attribute
12
+
13
+ has_many :barcode, Moysklad::Entities::Barcode
14
+
15
+ end
16
+ end
@@ -0,0 +1,31 @@
1
+ module Moysklad::Entities
2
+ class Good < Base
3
+ include CommonObject
4
+
5
+ tag 'good'
6
+
7
+ attribute :isSerialTrackable, Boolean
8
+
9
+ attribute :buyPrice, Float
10
+ attribute :buyCurrencyUuid, String
11
+
12
+ attribute :minPrice, Float
13
+
14
+ attribute :salePrice, Float
15
+ attribute :saleCurrencyUuid, String
16
+
17
+ attribute :weight, Float
18
+ attribute :volume, Float
19
+
20
+ attribute :parentUuid, String
21
+
22
+ attribute :productCode, String
23
+
24
+ attribute :uomUuid, String
25
+
26
+ element :code, String
27
+
28
+ element :salePrices, Moysklad::Entities::SalePrices
29
+
30
+ end
31
+ end
@@ -0,0 +1,12 @@
1
+ module Moysklad::Entities
2
+ class GoodRef < Base
3
+ include HappyMapper
4
+
5
+ tag 'goodRef'
6
+
7
+ attribute :uuid, String
8
+ attribute :name, String
9
+ attribute :code, String
10
+ attribute :objectType, String
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ module Moysklad::Entities
2
+ # https://online.moysklad.ru/exchange/rest/ms/xml/MyCompany/list
3
+ class MyCompany < Base
4
+ include Moysklad::Entities::CompanyConcern
5
+
6
+ tag 'myCompany'
7
+
8
+ attribute :director, String
9
+ attribute :chiefAccountant, String
10
+
11
+ end
12
+ end
@@ -0,0 +1,5 @@
1
+ module Moysklad::Entities
2
+ class Page < Struct.new(:items, :total, :start, :count)
3
+
4
+ end
5
+ end
@@ -0,0 +1,15 @@
1
+ module Moysklad::Entities
2
+ class Price < Base
3
+ include HappyMapper
4
+ include Moysklad::Entities::XmlFix
5
+
6
+ def self.from_money money
7
+ p = new
8
+ p.sumInCurrency = p.sum = money.cents
9
+ p
10
+ end
11
+
12
+ attribute :sumInCurrency, Float
13
+ attribute :sum, Float
14
+ end
15
+ end
@@ -0,0 +1,12 @@
1
+ module Moysklad::Entities
2
+ class SalePrice < Base
3
+ include HappyMapper
4
+ include Moysklad::Entities::XmlFix
5
+
6
+ tag 'price'
7
+
8
+ attribute :currencyUuid, String
9
+ attribute :priceTypeUuid, String
10
+ attribute :value, Float
11
+ end
12
+ end
@@ -0,0 +1,10 @@
1
+ module Moysklad::Entities
2
+ class SalePrices < Base
3
+ include HappyMapper
4
+ include Moysklad::Entities::XmlFix
5
+
6
+ tag 'salePrices'
7
+
8
+ has_many :price, Moysklad::Entities::SalePrice
9
+ end
10
+ end
@@ -0,0 +1,33 @@
1
+ module Moysklad::Entities
2
+ class StockTO < Base
3
+ include HappyMapper
4
+ include Moysklad::Entities::XmlFix
5
+
6
+ tag 'stockTO'
7
+
8
+ attribute :productCode, String
9
+ attribute :uomName, String
10
+
11
+ attribute :quantity, Float # Доступный остаток на склада
12
+ attribute :reserve, Float # Сколько в резерве
13
+ attribute :stock, Float # Остаток на складе не считая резерва
14
+
15
+ attribute :inTransit, Float
16
+ attribute :sumTotal, Float
17
+ attribute :saleAmount, Float
18
+ attribute :minimumBalance, Float
19
+ attribute :category, String
20
+ attribute :externalCode, String
21
+ attribute :parentUuid, String
22
+ attribute :defaultConsignment, Boolean
23
+ attribute :salePrice, Float
24
+
25
+ # Есть только при showConsignments
26
+ attribute :consignmentName, String
27
+ attribute :consignmentUuid, String
28
+
29
+ element :goodRef, Moysklad::Entities::GoodRef
30
+
31
+ end
32
+ end
33
+
@@ -0,0 +1,13 @@
1
+ module Moysklad::Entities
2
+ # https://online.moysklad.ru/exchange/rest/ms/xml/Warehouse/list
3
+ class Warehouse < Base
4
+ include CommonObject
5
+
6
+ tag 'warehouse'
7
+
8
+ attribute :agentUuid, String
9
+ attribute :archived, Boolean
10
+ element :code, String
11
+ end
12
+ end
13
+
@@ -0,0 +1,11 @@
1
+ module Moysklad::Entities::XmlFix
2
+ extend ::ActiveSupport::Concern
3
+
4
+ included do
5
+
6
+ def to_xml
7
+ super(Nokogiri::XML::Builder.new(encoding: 'utf-8')).to_xml
8
+ end
9
+ end
10
+ end
11
+
@@ -0,0 +1 @@
1
+ # nothing
@@ -0,0 +1,28 @@
1
+ module Moysklad::Resources
2
+ mattr_accessor :resources
3
+
4
+ extend ActiveSupport::Autoload
5
+
6
+ def self.register_resource resource_class
7
+ self.resources ||= []
8
+ self.resources << resource_class
9
+ end
10
+
11
+ autoload :Base
12
+ autoload :Cached
13
+ autoload :Indexed
14
+
15
+ require_relative 'resources/stock'
16
+
17
+ class Metadata < Base
18
+ def self.entity_class
19
+ Moysklad::Entities::EmbeddedEntityMetadata
20
+ end
21
+ end
22
+
23
+ # Простые ресурсы
24
+ %w{Goods Features CustomerOrders Warehouses Attributes Companies Consignments MyCompanies}.each do |klass_name|
25
+ const_set klass_name, Class.new( Base )
26
+ end
27
+
28
+ end
@@ -0,0 +1,84 @@
1
+ class Moysklad::Resources::Base
2
+ PREFIX_PATH = 'exchange/rest/ms/xml/'
3
+
4
+ def self.inherited superclass
5
+ super
6
+ Moysklad::Resources.register_resource superclass
7
+ end
8
+
9
+ # https://support.moysklad.ru/hc/ru/articles/203404253-REST-сервис-синхронизации-данных
10
+ def initialize client: nil
11
+ raise "Должен быть Moysklad::Client" unless client.is_a? Moysklad::Client
12
+ @client = client
13
+ end
14
+
15
+ def list params={}
16
+ parse client.get list_path, params
17
+ end
18
+
19
+ def collection params={}
20
+ parse_collection client.get list_path, params
21
+ end
22
+
23
+ def find uuid
24
+ parse client.get item_path uuid
25
+ end
26
+
27
+ def create resource
28
+ parse client.put create_path, prepare_resource(resource)
29
+ end
30
+
31
+ def delete uuid
32
+ client.delete item_path uuid
33
+ end
34
+
35
+ def self.type
36
+ ActiveSupport::Inflector.singularize name.split('::').last.to_sym
37
+ end
38
+
39
+ def self.pluralized_type
40
+ ActiveSupport::Inflector.underscore ActiveSupport::Inflector.pluralize type
41
+ end
42
+
43
+ def self.entity_class
44
+ ActiveSupport::Inflector.constantize "Moysklad::Entities::#{type.to_s}"
45
+ end
46
+ private
47
+
48
+ attr_reader :client
49
+
50
+ def prepare_resource resource
51
+ resource
52
+ end
53
+
54
+ def parse content
55
+ self.class.entity_class.parse content
56
+ end
57
+
58
+ def parse_collection content
59
+ col = Moysklad::Entities::Collection.parse content
60
+
61
+ # TODO Парсится два раза. Оптимизировать. Например сделать динамические CollectionFeature
62
+ # и парсить через них
63
+
64
+ items = parse content
65
+ Moysklad::Entities::Page.new items, col.total, col.start, col.count
66
+ end
67
+
68
+ def item_path uuid
69
+ prefix_path + '/' + uuid
70
+ end
71
+
72
+ def create_path
73
+ prefix_path
74
+ end
75
+
76
+ def list_path
77
+ prefix_path + '/list'
78
+ end
79
+
80
+ def prefix_path
81
+ PREFIX_PATH + self.class.type
82
+ end
83
+
84
+ end