amo_crm 0.2.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.
- checksums.yaml +7 -0
- data/.gitignore +28 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/CHANGELOG.md +22 -0
- data/Gemfile +4 -0
- data/Guardfile +29 -0
- data/LICENSE.txt +22 -0
- data/README.md +1 -0
- data/Rakefile +14 -0
- data/amo_crm.gemspec +41 -0
- data/dump_amocrm_account.rb +7 -0
- data/lib/amo_crm.rb +31 -0
- data/lib/amo_crm/client.rb +98 -0
- data/lib/amo_crm/client/error.rb +18 -0
- data/lib/amo_crm/client/errors.rb +16 -0
- data/lib/amo_crm/entities.rb +14 -0
- data/lib/amo_crm/entities/account.rb +7 -0
- data/lib/amo_crm/entities/attachment_document.rb +16 -0
- data/lib/amo_crm/entities/attribute.rb +71 -0
- data/lib/amo_crm/entities/attribute_metadata.rb +38 -0
- data/lib/amo_crm/entities/barcode.rb +19 -0
- data/lib/amo_crm/entities/base.rb +5 -0
- data/lib/amo_crm/entities/collection.rb +12 -0
- data/lib/amo_crm/entities/common.rb +15 -0
- data/lib/amo_crm/entities/common_object.rb +27 -0
- data/lib/amo_crm/entities/company.rb +30 -0
- data/lib/amo_crm/entities/company_concern.rb +20 -0
- data/lib/amo_crm/entities/consignment.rb +22 -0
- data/lib/amo_crm/entities/contact.rb +29 -0
- data/lib/amo_crm/entities/contract.rb +2 -0
- data/lib/amo_crm/entities/country.rb +10 -0
- data/lib/amo_crm/entities/custom_entity.rb +18 -0
- data/lib/amo_crm/entities/custom_entity_metadata.rb +36 -0
- data/lib/amo_crm/entities/custom_field.rb +6 -0
- data/lib/amo_crm/entities/custom_field_definition.rb +13 -0
- data/lib/amo_crm/entities/custom_field_value.rb +5 -0
- data/lib/amo_crm/entities/custom_fields.rb +24 -0
- data/lib/amo_crm/entities/custom_fields_definition.rb +4 -0
- data/lib/amo_crm/entities/customer_order.rb +51 -0
- data/lib/amo_crm/entities/customer_order_position.rb +22 -0
- data/lib/amo_crm/entities/embedded_entity_metadata.rb +18 -0
- data/lib/amo_crm/entities/error.rb +13 -0
- data/lib/amo_crm/entities/feature.rb +23 -0
- data/lib/amo_crm/entities/good.rb +34 -0
- data/lib/amo_crm/entities/good_folder.rb +10 -0
- data/lib/amo_crm/entities/good_ref.rb +12 -0
- data/lib/amo_crm/entities/image.rb +21 -0
- data/lib/amo_crm/entities/images.rb +10 -0
- data/lib/amo_crm/entities/lead.rb +28 -0
- data/lib/amo_crm/entities/my_company.rb +12 -0
- data/lib/amo_crm/entities/page.rb +5 -0
- data/lib/amo_crm/entities/price.rb +15 -0
- data/lib/amo_crm/entities/price_type.rb +13 -0
- data/lib/amo_crm/entities/sale_price.rb +18 -0
- data/lib/amo_crm/entities/sale_prices.rb +10 -0
- data/lib/amo_crm/entities/slot.rb +10 -0
- data/lib/amo_crm/entities/stock_to.rb +33 -0
- data/lib/amo_crm/entities/warehouse.rb +15 -0
- data/lib/amo_crm/entities/workflow.rb +11 -0
- data/lib/amo_crm/entities/workflow_state.rb +8 -0
- data/lib/amo_crm/entities/xml_fix.rb +14 -0
- data/lib/amo_crm/errors.rb +1 -0
- data/lib/amo_crm/resources.rb +18 -0
- data/lib/amo_crm/resources/account.rb +30 -0
- data/lib/amo_crm/resources/base.rb +149 -0
- data/lib/amo_crm/resources/companies.rb +27 -0
- data/lib/amo_crm/resources/contacts.rb +23 -0
- data/lib/amo_crm/resources/custom_entity_metadata.rb +18 -0
- data/lib/amo_crm/resources/custom_field.rb +28 -0
- data/lib/amo_crm/resources/embedded_entity_metadata.rb +20 -0
- data/lib/amo_crm/resources/embedded_entity_metadata_indexed.rb +7 -0
- data/lib/amo_crm/resources/indexed.rb +89 -0
- data/lib/amo_crm/resources/leads.rb +31 -0
- data/lib/amo_crm/resources/stock.rb +36 -0
- data/lib/amo_crm/resources/subresource.rb +21 -0
- data/lib/amo_crm/resources/where_filter.rb +26 -0
- data/lib/amo_crm/response.rb +7 -0
- data/lib/amo_crm/universe.rb +44 -0
- data/lib/amo_crm/version.rb +3 -0
- data/scripts/rest.sh +25 -0
- data/spec/fixtures/302.raw +15 -0
- data/spec/fixtures/400.raw +8 -0
- data/spec/fixtures/401.html +1 -0
- data/spec/fixtures/401_2.html +1 -0
- data/spec/fixtures/405.raw +9 -0
- data/spec/fixtures/500.raw +8 -0
- data/spec/fixtures/502.raw +8 -0
- data/spec/fixtures/Consignment_list_0.raw +14 -0
- data/spec/fixtures/Consignment_list_1000.raw +14 -0
- data/spec/fixtures/Country_list.raw +14 -0
- data/spec/fixtures/CustomEntityMetadata_list.raw +14 -0
- data/spec/fixtures/CustomEntity_list.raw +14 -0
- data/spec/fixtures/CustomEntity_list_bad_chars.raw +17 -0
- data/spec/fixtures/EmbeddedEntityMetadata_list.raw +15 -0
- data/spec/fixtures/Feature_list.raw +14 -0
- data/spec/fixtures/Feature_list_1000.raw +14 -0
- data/spec/fixtures/Good_WithManyAttributes.raw +13 -0
- data/spec/fixtures/Good_e932ebe1-2e22-11e4-9406-002590a28eca.raw +12 -0
- data/spec/fixtures/Good_eb77ad57-2e22-11e4-4030-002590a28eca.raw +12 -0
- data/spec/fixtures/Goods_list.raw +14 -0
- data/spec/fixtures/Metadata_list.raw +14 -0
- data/spec/fixtures/PriceType_list.raw +14 -0
- data/spec/fixtures/Stock.raw +11 -0
- data/spec/fixtures/Stock_showConsignments.raw +11 -0
- data/spec/fixtures/Uom_list.raw +14 -0
- data/spec/fixtures/Warehouse_list.raw +14 -0
- data/spec/fixtures/Workflow_list.raw +15 -0
- data/spec/fixtures/attribute_boolean.xml +6 -0
- data/spec/fixtures/attribute_dictionary.xml +5 -0
- data/spec/fixtures/attribute_double.xml +6 -0
- data/spec/fixtures/attribute_file.xml +12 -0
- data/spec/fixtures/attribute_long.xml +6 -0
- data/spec/fixtures/attribute_text.xml +5 -0
- data/spec/fixtures/attribute_time.xml +6 -0
- data/spec/fixtures/attribute_url.xml +6 -0
- data/spec/fixtures/bad_chars.xml +6 -0
- data/spec/fixtures/customer_order.xml +28 -0
- data/spec/fixtures/good_with_attributes.raw +12 -0
- data/spec/fixtures/good_with_image.xml +39 -0
- data/spec/fixtures/resource_forbidden.raw +9 -0
- data/spec/fixtures/wrong_password.raw +9 -0
- data/spec/fixtures/wrong_type.raw +70 -0
- data/spec/fixtures/wrong_user.raw +9 -0
- data/spec/lib/amocrm/client/errors_spec.rb +102 -0
- data/spec/lib/amocrm/entities/attribute_metadata_spec.rb +30 -0
- data/spec/lib/amocrm/entities/attribute_spec.rb +36 -0
- data/spec/lib/amocrm/entities/country_spec.rb +25 -0
- data/spec/lib/amocrm/entities/custom_entity_metadata_spec.rb +34 -0
- data/spec/lib/amocrm/entities/custom_entity_spec.rb +34 -0
- data/spec/lib/amocrm/entities/customer_order_position_spec.rb +35 -0
- data/spec/lib/amocrm/entities/customer_order_spec.rb +58 -0
- data/spec/lib/amocrm/entities/good_spec.rb +55 -0
- data/spec/lib/amocrm/resources/base_spec.rb +61 -0
- data/spec/lib/amocrm/resources/countries_spec.rb +22 -0
- data/spec/lib/amocrm/resources/custom_entity_metadata_spec.rb +13 -0
- data/spec/lib/amocrm/resources/embedded_entity_metadata_indexed_spec.rb +20 -0
- data/spec/lib/amocrm/resources/embedded_entity_metadata_spec.rb +11 -0
- data/spec/lib/amocrm/resources/good_folders_spec.rb +17 -0
- data/spec/lib/amocrm/resources/indexed_spec.rb +61 -0
- data/spec/lib/amocrm/resources/stock_spec.rb +63 -0
- data/spec/lib/amocrm/resources/subresource_spec.rb +13 -0
- data/spec/lib/amocrm/resources/warehouses_spec.rb +20 -0
- data/spec/lib/amocrm/resources/workflows_spec.rb +21 -0
- data/spec/lib/amocrm/resources_spec.rb +10 -0
- data/spec/lib/amocrm/universe_spec.rb +53 -0
- data/spec/lib/amocrm_spec.rb +8 -0
- data/spec/spec_helper.rb +106 -0
- data/spec/support/resource.rb +72 -0
- metadata +500 -0
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
module AmoCRM::Entities
|
|
2
|
+
class Feature < Base
|
|
3
|
+
include CommonObject
|
|
4
|
+
|
|
5
|
+
tag 'feature'
|
|
6
|
+
|
|
7
|
+
attribute :goodUuid, String
|
|
8
|
+
|
|
9
|
+
attribute :archived, Boolean
|
|
10
|
+
attribute :isDefault, Boolean
|
|
11
|
+
|
|
12
|
+
element :shared, Boolean
|
|
13
|
+
|
|
14
|
+
has_many :attributes, AmoCRM::Entities::Attribute
|
|
15
|
+
has_many :barcodes, AmoCRM::Entities::Barcode
|
|
16
|
+
|
|
17
|
+
def good universe
|
|
18
|
+
cache :good, universe do
|
|
19
|
+
universe.good.find goodUuid
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
module AmoCRM::Entities
|
|
2
|
+
class Good < Base
|
|
3
|
+
include CommonObject
|
|
4
|
+
include AmoCRM::Entities::XmlFix
|
|
5
|
+
|
|
6
|
+
tag 'good'
|
|
7
|
+
|
|
8
|
+
attribute :isSerialTrackable, Boolean
|
|
9
|
+
attribute :buyPrice, Float
|
|
10
|
+
attribute :buyCurrencyUuid, String
|
|
11
|
+
attribute :minPrice, Float
|
|
12
|
+
attribute :salePrice, Float
|
|
13
|
+
attribute :saleCurrencyUuid, String
|
|
14
|
+
attribute :weight, Float
|
|
15
|
+
attribute :volume, Float
|
|
16
|
+
attribute :parentUuid, String
|
|
17
|
+
attribute :productCode, String
|
|
18
|
+
attribute :uomUuid, String
|
|
19
|
+
|
|
20
|
+
element :code, String
|
|
21
|
+
element :salePrices, AmoCRM::Entities::SalePrices
|
|
22
|
+
|
|
23
|
+
has_many :attributes, Attribute
|
|
24
|
+
element :images, AmoCRM::Entities::Images
|
|
25
|
+
element :barcode, AmoCRM::Entities::Barcode
|
|
26
|
+
# preferences
|
|
27
|
+
|
|
28
|
+
def features universe
|
|
29
|
+
cache :features, universe do
|
|
30
|
+
universe.features.where goodUuid: uuid
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
module AmoCRM::Entities
|
|
2
|
+
class Image < Base
|
|
3
|
+
include HappyMapper
|
|
4
|
+
include AmoCRM::Entities::XmlFix
|
|
5
|
+
|
|
6
|
+
tag 'image'
|
|
7
|
+
|
|
8
|
+
attribute :created, Time
|
|
9
|
+
attribute :updated, Time
|
|
10
|
+
attribute :updatedBy, String
|
|
11
|
+
attribute :name, String
|
|
12
|
+
attribute :tinyUuid, String
|
|
13
|
+
attribute :filename, String
|
|
14
|
+
attribute :miniatureUuid, String
|
|
15
|
+
|
|
16
|
+
attribute :accountUuid, String
|
|
17
|
+
attribute :accountId, String
|
|
18
|
+
attribute :uuid, String
|
|
19
|
+
attribute :externalcode, String
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
module AmoCRM::Entities
|
|
2
|
+
class Lead
|
|
3
|
+
include Virtus.model
|
|
4
|
+
attribute :name, String
|
|
5
|
+
attribute :date_create, Time
|
|
6
|
+
attribute :last_modified, Time
|
|
7
|
+
attribute :status_id, Integer
|
|
8
|
+
attribute :price, Integer
|
|
9
|
+
attribute :request_id, Integer
|
|
10
|
+
attribute :tags, Array[String]
|
|
11
|
+
# responsible_user_id
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def attributes_for_update
|
|
15
|
+
{
|
|
16
|
+
name: name,
|
|
17
|
+
date_create: date_create.to_i,
|
|
18
|
+
last_modified: last_modified.to_i,
|
|
19
|
+
request_id: request_id,
|
|
20
|
+
# status_id: status_id,
|
|
21
|
+
price: price,
|
|
22
|
+
custom_fields: custom_fields,
|
|
23
|
+
# tags: tags,
|
|
24
|
+
visitor_uid: visitor_uid,
|
|
25
|
+
}
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
module AmoCRM::Entities
|
|
2
|
+
# https://online.moysklad.ru/exchange/rest/ms/xml/MyCompany/list
|
|
3
|
+
class MyCompany < Base
|
|
4
|
+
include AmoCRM::Entities::CompanyConcern
|
|
5
|
+
|
|
6
|
+
tag 'myCompany'
|
|
7
|
+
|
|
8
|
+
attribute :director, String
|
|
9
|
+
attribute :chiefAccountant, String
|
|
10
|
+
|
|
11
|
+
end
|
|
12
|
+
end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
module AmoCRM::Entities
|
|
2
|
+
class Price < Base
|
|
3
|
+
include HappyMapper
|
|
4
|
+
include AmoCRM::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,13 @@
|
|
|
1
|
+
module AmoCRM::Entities
|
|
2
|
+
class PriceType < Base
|
|
3
|
+
include CommonObject
|
|
4
|
+
include AmoCRM::Entities::XmlFix
|
|
5
|
+
tag 'priceType'
|
|
6
|
+
|
|
7
|
+
attribute :updated, Time
|
|
8
|
+
attribute :updatedBy, String
|
|
9
|
+
attribute :name, String
|
|
10
|
+
|
|
11
|
+
attribute :index, Integer
|
|
12
|
+
end
|
|
13
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
module AmoCRM::Entities
|
|
2
|
+
class SalePrice < Base
|
|
3
|
+
include HappyMapper
|
|
4
|
+
include AmoCRM::Entities::XmlFix
|
|
5
|
+
|
|
6
|
+
tag 'price'
|
|
7
|
+
|
|
8
|
+
attribute :currencyUuid, String
|
|
9
|
+
attribute :priceTypeUuid, String
|
|
10
|
+
attribute :value, Float
|
|
11
|
+
|
|
12
|
+
def priceType universe
|
|
13
|
+
cache :priceType, universe do
|
|
14
|
+
universe.price_types.find priceTypeUuid
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
module AmoCRM::Entities
|
|
2
|
+
class StockTO < Base
|
|
3
|
+
include HappyMapper
|
|
4
|
+
include AmoCRM::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, AmoCRM::Entities::GoodRef
|
|
30
|
+
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
module AmoCRM::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
|
+
attribute :code, String
|
|
11
|
+
|
|
12
|
+
element :contact, Contact
|
|
13
|
+
has_many :slots, Slot
|
|
14
|
+
end
|
|
15
|
+
end
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
module AmoCRM::Entities::XmlFix
|
|
2
|
+
extend ::ActiveSupport::Concern
|
|
3
|
+
|
|
4
|
+
def to_xml builder = nil,default_namespace = nil,tag_from_parent = nil
|
|
5
|
+
unless builder
|
|
6
|
+
builder = Nokogiri::XML::Builder.new(encoding: 'utf-8')
|
|
7
|
+
write_out_to_xml = true
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
result = super builder, default_namespace, tag_from_parent
|
|
11
|
+
|
|
12
|
+
write_out_to_xml ? result.to_xml : result
|
|
13
|
+
end
|
|
14
|
+
end
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# nothing
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
module AmoCRM::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
|
+
require_relative 'resources/base'
|
|
12
|
+
require_relative 'resources/indexed'
|
|
13
|
+
require_relative 'resources/companies'
|
|
14
|
+
require_relative 'resources/leads'
|
|
15
|
+
require_relative 'resources/contacts'
|
|
16
|
+
require_relative 'resources/account'
|
|
17
|
+
require_relative 'resources/custom_field'
|
|
18
|
+
end
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
module AmoCRM::Resources
|
|
2
|
+
class Account < Base
|
|
3
|
+
|
|
4
|
+
def current
|
|
5
|
+
get
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
def get
|
|
9
|
+
parse client.get 'accounts/current'
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def self.entity_class
|
|
13
|
+
AmoCRM::Entities::Account
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def response_key
|
|
17
|
+
'account'
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def self.resource_name
|
|
21
|
+
:account
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
private
|
|
25
|
+
|
|
26
|
+
def parse data
|
|
27
|
+
self.class.entity_class.new data[response_key]
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
module AmoCRM::Resources
|
|
2
|
+
class Base
|
|
3
|
+
LIMIT_ROWS = 500
|
|
4
|
+
|
|
5
|
+
def self.inherited superclass
|
|
6
|
+
super
|
|
7
|
+
AmoCRM::Resources.register_resource superclass
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
# Возвращает этот-же ресурс только индексированный
|
|
11
|
+
#
|
|
12
|
+
# @return [AmoCRM::Resources::Indexed]
|
|
13
|
+
def self.indexed *args
|
|
14
|
+
# AmoCRM::Resources::Indexed.new new(*args)
|
|
15
|
+
new(*args)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def initialize client: nil
|
|
19
|
+
raise "Должен быть AmoCRM::Client" unless client.is_a? AmoCRM::Client
|
|
20
|
+
@client = client
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def all
|
|
24
|
+
buffer = []
|
|
25
|
+
offset = 0
|
|
26
|
+
list = nil
|
|
27
|
+
|
|
28
|
+
while offset==0 || list.present?
|
|
29
|
+
list = client.get(list_path, limit_offset: offset, limit_rows: LIMIT_ROWS).contacts
|
|
30
|
+
offset += LIMIT_ROWS
|
|
31
|
+
buffer += list
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
rescue AmoCRM::Client::Error => err
|
|
35
|
+
raise err unless err.state == 204
|
|
36
|
+
buffer
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# Возвращает список элементов как есть
|
|
40
|
+
#
|
|
41
|
+
# @return [Array of AmoCRM::Entities::Base]
|
|
42
|
+
def list params={}
|
|
43
|
+
parse_list client.get list_path, params
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# Возвращает страницу со списком элементов
|
|
47
|
+
#
|
|
48
|
+
# @return [AmoCRM::Entities::Page]
|
|
49
|
+
def page params={}
|
|
50
|
+
parse_page client.get list_path, params
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# Забираем элемент по id
|
|
54
|
+
#
|
|
55
|
+
# @return [AmoCRM::Entities::Base]
|
|
56
|
+
def get id=nil
|
|
57
|
+
parse client.get get_path, id: id
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# Модифицируем элемент по id
|
|
61
|
+
#
|
|
62
|
+
# @return [AmoCRM::Entities::Base]
|
|
63
|
+
def update model
|
|
64
|
+
raise "Должна быть модель типа AmoCRM::Entities::Base" unless model.is_a? AmoCRM::Entities::Base
|
|
65
|
+
client.post set_path, model_to_request(model)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# Создаем запись
|
|
69
|
+
#
|
|
70
|
+
# @param [AmoCRM::Entities::Base]
|
|
71
|
+
#
|
|
72
|
+
# Возвращается созданная на сервере сущность
|
|
73
|
+
#
|
|
74
|
+
# @return [AmoCRM::Entities::Base]
|
|
75
|
+
def create model
|
|
76
|
+
raise "Должна быть модель типа AmoCRM::Entities::Base" unless model.is_a? AmoCRM::Entities::Base
|
|
77
|
+
client.post set_path, model_to_request(model, :add)
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# Удаляем запись по id
|
|
81
|
+
#
|
|
82
|
+
# @param id
|
|
83
|
+
def delete id
|
|
84
|
+
client.delete get_path id
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def self.type
|
|
88
|
+
ActiveSupport::Inflector.singularize name.split('::').last.to_sym
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def self.resource_name
|
|
92
|
+
ActiveSupport::Inflector.underscore ActiveSupport::Inflector.pluralize type
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def self.entity_class
|
|
96
|
+
ActiveSupport::Inflector.constantize "AmoCRM::Entities::#{type.to_s}"
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
private
|
|
100
|
+
|
|
101
|
+
attr_reader :client
|
|
102
|
+
|
|
103
|
+
def model_to_request(model, command=:update)
|
|
104
|
+
{
|
|
105
|
+
request: {
|
|
106
|
+
response_key => {
|
|
107
|
+
command => [model.attributes_for_update]
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def parse data
|
|
114
|
+
self.class.entity_class.new data[response_key].first
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def parse_list content
|
|
118
|
+
content[response_key].map do |data|
|
|
119
|
+
self.class.entity_class.new data
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
def parse_content content
|
|
124
|
+
content
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
def parse_page content
|
|
128
|
+
col = AmoCRM::Entities::Collection.parse parse_content content
|
|
129
|
+
|
|
130
|
+
# TODO Парсится два раза. Оптимизировать. Например сделать динамические CollectionFeature
|
|
131
|
+
# и парсить через них
|
|
132
|
+
|
|
133
|
+
items = parse content
|
|
134
|
+
AmoCRM::Entities::Page.new items, col.total, col.start, col.count
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
def get_path
|
|
138
|
+
list_path
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
def set_path
|
|
142
|
+
raise 'not implemented'
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
def list_path
|
|
146
|
+
raise 'not implemented'
|
|
147
|
+
end
|
|
148
|
+
end
|
|
149
|
+
end
|