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.
Files changed (150) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +28 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +4 -0
  5. data/CHANGELOG.md +22 -0
  6. data/Gemfile +4 -0
  7. data/Guardfile +29 -0
  8. data/LICENSE.txt +22 -0
  9. data/README.md +1 -0
  10. data/Rakefile +14 -0
  11. data/amo_crm.gemspec +41 -0
  12. data/dump_amocrm_account.rb +7 -0
  13. data/lib/amo_crm.rb +31 -0
  14. data/lib/amo_crm/client.rb +98 -0
  15. data/lib/amo_crm/client/error.rb +18 -0
  16. data/lib/amo_crm/client/errors.rb +16 -0
  17. data/lib/amo_crm/entities.rb +14 -0
  18. data/lib/amo_crm/entities/account.rb +7 -0
  19. data/lib/amo_crm/entities/attachment_document.rb +16 -0
  20. data/lib/amo_crm/entities/attribute.rb +71 -0
  21. data/lib/amo_crm/entities/attribute_metadata.rb +38 -0
  22. data/lib/amo_crm/entities/barcode.rb +19 -0
  23. data/lib/amo_crm/entities/base.rb +5 -0
  24. data/lib/amo_crm/entities/collection.rb +12 -0
  25. data/lib/amo_crm/entities/common.rb +15 -0
  26. data/lib/amo_crm/entities/common_object.rb +27 -0
  27. data/lib/amo_crm/entities/company.rb +30 -0
  28. data/lib/amo_crm/entities/company_concern.rb +20 -0
  29. data/lib/amo_crm/entities/consignment.rb +22 -0
  30. data/lib/amo_crm/entities/contact.rb +29 -0
  31. data/lib/amo_crm/entities/contract.rb +2 -0
  32. data/lib/amo_crm/entities/country.rb +10 -0
  33. data/lib/amo_crm/entities/custom_entity.rb +18 -0
  34. data/lib/amo_crm/entities/custom_entity_metadata.rb +36 -0
  35. data/lib/amo_crm/entities/custom_field.rb +6 -0
  36. data/lib/amo_crm/entities/custom_field_definition.rb +13 -0
  37. data/lib/amo_crm/entities/custom_field_value.rb +5 -0
  38. data/lib/amo_crm/entities/custom_fields.rb +24 -0
  39. data/lib/amo_crm/entities/custom_fields_definition.rb +4 -0
  40. data/lib/amo_crm/entities/customer_order.rb +51 -0
  41. data/lib/amo_crm/entities/customer_order_position.rb +22 -0
  42. data/lib/amo_crm/entities/embedded_entity_metadata.rb +18 -0
  43. data/lib/amo_crm/entities/error.rb +13 -0
  44. data/lib/amo_crm/entities/feature.rb +23 -0
  45. data/lib/amo_crm/entities/good.rb +34 -0
  46. data/lib/amo_crm/entities/good_folder.rb +10 -0
  47. data/lib/amo_crm/entities/good_ref.rb +12 -0
  48. data/lib/amo_crm/entities/image.rb +21 -0
  49. data/lib/amo_crm/entities/images.rb +10 -0
  50. data/lib/amo_crm/entities/lead.rb +28 -0
  51. data/lib/amo_crm/entities/my_company.rb +12 -0
  52. data/lib/amo_crm/entities/page.rb +5 -0
  53. data/lib/amo_crm/entities/price.rb +15 -0
  54. data/lib/amo_crm/entities/price_type.rb +13 -0
  55. data/lib/amo_crm/entities/sale_price.rb +18 -0
  56. data/lib/amo_crm/entities/sale_prices.rb +10 -0
  57. data/lib/amo_crm/entities/slot.rb +10 -0
  58. data/lib/amo_crm/entities/stock_to.rb +33 -0
  59. data/lib/amo_crm/entities/warehouse.rb +15 -0
  60. data/lib/amo_crm/entities/workflow.rb +11 -0
  61. data/lib/amo_crm/entities/workflow_state.rb +8 -0
  62. data/lib/amo_crm/entities/xml_fix.rb +14 -0
  63. data/lib/amo_crm/errors.rb +1 -0
  64. data/lib/amo_crm/resources.rb +18 -0
  65. data/lib/amo_crm/resources/account.rb +30 -0
  66. data/lib/amo_crm/resources/base.rb +149 -0
  67. data/lib/amo_crm/resources/companies.rb +27 -0
  68. data/lib/amo_crm/resources/contacts.rb +23 -0
  69. data/lib/amo_crm/resources/custom_entity_metadata.rb +18 -0
  70. data/lib/amo_crm/resources/custom_field.rb +28 -0
  71. data/lib/amo_crm/resources/embedded_entity_metadata.rb +20 -0
  72. data/lib/amo_crm/resources/embedded_entity_metadata_indexed.rb +7 -0
  73. data/lib/amo_crm/resources/indexed.rb +89 -0
  74. data/lib/amo_crm/resources/leads.rb +31 -0
  75. data/lib/amo_crm/resources/stock.rb +36 -0
  76. data/lib/amo_crm/resources/subresource.rb +21 -0
  77. data/lib/amo_crm/resources/where_filter.rb +26 -0
  78. data/lib/amo_crm/response.rb +7 -0
  79. data/lib/amo_crm/universe.rb +44 -0
  80. data/lib/amo_crm/version.rb +3 -0
  81. data/scripts/rest.sh +25 -0
  82. data/spec/fixtures/302.raw +15 -0
  83. data/spec/fixtures/400.raw +8 -0
  84. data/spec/fixtures/401.html +1 -0
  85. data/spec/fixtures/401_2.html +1 -0
  86. data/spec/fixtures/405.raw +9 -0
  87. data/spec/fixtures/500.raw +8 -0
  88. data/spec/fixtures/502.raw +8 -0
  89. data/spec/fixtures/Consignment_list_0.raw +14 -0
  90. data/spec/fixtures/Consignment_list_1000.raw +14 -0
  91. data/spec/fixtures/Country_list.raw +14 -0
  92. data/spec/fixtures/CustomEntityMetadata_list.raw +14 -0
  93. data/spec/fixtures/CustomEntity_list.raw +14 -0
  94. data/spec/fixtures/CustomEntity_list_bad_chars.raw +17 -0
  95. data/spec/fixtures/EmbeddedEntityMetadata_list.raw +15 -0
  96. data/spec/fixtures/Feature_list.raw +14 -0
  97. data/spec/fixtures/Feature_list_1000.raw +14 -0
  98. data/spec/fixtures/Good_WithManyAttributes.raw +13 -0
  99. data/spec/fixtures/Good_e932ebe1-2e22-11e4-9406-002590a28eca.raw +12 -0
  100. data/spec/fixtures/Good_eb77ad57-2e22-11e4-4030-002590a28eca.raw +12 -0
  101. data/spec/fixtures/Goods_list.raw +14 -0
  102. data/spec/fixtures/Metadata_list.raw +14 -0
  103. data/spec/fixtures/PriceType_list.raw +14 -0
  104. data/spec/fixtures/Stock.raw +11 -0
  105. data/spec/fixtures/Stock_showConsignments.raw +11 -0
  106. data/spec/fixtures/Uom_list.raw +14 -0
  107. data/spec/fixtures/Warehouse_list.raw +14 -0
  108. data/spec/fixtures/Workflow_list.raw +15 -0
  109. data/spec/fixtures/attribute_boolean.xml +6 -0
  110. data/spec/fixtures/attribute_dictionary.xml +5 -0
  111. data/spec/fixtures/attribute_double.xml +6 -0
  112. data/spec/fixtures/attribute_file.xml +12 -0
  113. data/spec/fixtures/attribute_long.xml +6 -0
  114. data/spec/fixtures/attribute_text.xml +5 -0
  115. data/spec/fixtures/attribute_time.xml +6 -0
  116. data/spec/fixtures/attribute_url.xml +6 -0
  117. data/spec/fixtures/bad_chars.xml +6 -0
  118. data/spec/fixtures/customer_order.xml +28 -0
  119. data/spec/fixtures/good_with_attributes.raw +12 -0
  120. data/spec/fixtures/good_with_image.xml +39 -0
  121. data/spec/fixtures/resource_forbidden.raw +9 -0
  122. data/spec/fixtures/wrong_password.raw +9 -0
  123. data/spec/fixtures/wrong_type.raw +70 -0
  124. data/spec/fixtures/wrong_user.raw +9 -0
  125. data/spec/lib/amocrm/client/errors_spec.rb +102 -0
  126. data/spec/lib/amocrm/entities/attribute_metadata_spec.rb +30 -0
  127. data/spec/lib/amocrm/entities/attribute_spec.rb +36 -0
  128. data/spec/lib/amocrm/entities/country_spec.rb +25 -0
  129. data/spec/lib/amocrm/entities/custom_entity_metadata_spec.rb +34 -0
  130. data/spec/lib/amocrm/entities/custom_entity_spec.rb +34 -0
  131. data/spec/lib/amocrm/entities/customer_order_position_spec.rb +35 -0
  132. data/spec/lib/amocrm/entities/customer_order_spec.rb +58 -0
  133. data/spec/lib/amocrm/entities/good_spec.rb +55 -0
  134. data/spec/lib/amocrm/resources/base_spec.rb +61 -0
  135. data/spec/lib/amocrm/resources/countries_spec.rb +22 -0
  136. data/spec/lib/amocrm/resources/custom_entity_metadata_spec.rb +13 -0
  137. data/spec/lib/amocrm/resources/embedded_entity_metadata_indexed_spec.rb +20 -0
  138. data/spec/lib/amocrm/resources/embedded_entity_metadata_spec.rb +11 -0
  139. data/spec/lib/amocrm/resources/good_folders_spec.rb +17 -0
  140. data/spec/lib/amocrm/resources/indexed_spec.rb +61 -0
  141. data/spec/lib/amocrm/resources/stock_spec.rb +63 -0
  142. data/spec/lib/amocrm/resources/subresource_spec.rb +13 -0
  143. data/spec/lib/amocrm/resources/warehouses_spec.rb +20 -0
  144. data/spec/lib/amocrm/resources/workflows_spec.rb +21 -0
  145. data/spec/lib/amocrm/resources_spec.rb +10 -0
  146. data/spec/lib/amocrm/universe_spec.rb +53 -0
  147. data/spec/lib/amocrm_spec.rb +8 -0
  148. data/spec/spec_helper.rb +106 -0
  149. data/spec/support/resource.rb +72 -0
  150. metadata +500 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4bcdfe457e805bcd13291af40d904d6a66164b3c
4
+ data.tar.gz: 9dc97f28498ace5952964d4841222a974526a39c
5
+ SHA512:
6
+ metadata.gz: ea06783bfd4d9fabaea1fce866275a2f8a8fa62f7d99e16a091aecd356ebf654e9455f4af2da6ca55e1e356f2b972afde20b4ded21f03cd512b191927ea57e3c
7
+ data.tar.gz: 31f5f2485883e27b060d8fda3b9c7666fb3a7e46d57b3b93355d597793ae3b895e6f4303890f122b980d03411a7f76961d00663ce8e3f26546efc98b474b3f7b
@@ -0,0 +1,28 @@
1
+ *.gem
2
+ *.rbc
3
+ test.*
4
+ tags
5
+ gems.tags
6
+ .bundle
7
+ .config
8
+ .yardoc
9
+ *.swp
10
+ Gemfile.lock
11
+ InstalledFiles
12
+ _yardoc
13
+ coverage
14
+ doc/
15
+ lib/bundler/man
16
+ pkg
17
+ rdoc
18
+ spec/reports
19
+ test/tmp
20
+ test/version_tmp
21
+ tmp
22
+ *~
23
+ *.bak
24
+ *.bundle
25
+ *.so
26
+ *.o
27
+ *.a
28
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ - 2.1.0
@@ -0,0 +1,22 @@
1
+ ## 0.2.1 - beta
2
+
3
+ * Классификация возмжных ошибок от API
4
+
5
+ ## 0.2.0 / 2014-12-20
6
+
7
+ * Добавил сущность PriceType и ресурс price_types
8
+ * Переименовал Metadata в EmbeddedEntityMetadata
9
+
10
+ ## 0.1.5 / 2014-12-05
11
+
12
+ * Больше документации.
13
+ * Зависимости перенесены из Gemfile в gemspec. Спасибо @wyde19
14
+
15
+ ## 0.1.4 / 2014-12-04
16
+
17
+ * Universe.build для ленивого создания вселенной
18
+
19
+ ## 0.1.3 / 2014-12-04
20
+
21
+ * Примеры использования и больше документации
22
+ * Сущности Country и Uom
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in amo_crm.gemspec
4
+ gemspec
@@ -0,0 +1,29 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ # Note: The cmd option is now required due to the increasing number of ways
5
+ # rspec may be run, below are examples of the most common uses.
6
+ # * bundler: 'bundle exec rspec'
7
+ # * bundler binstubs: 'bin/rspec'
8
+ # * spring: 'bin/rsspec' (This will use spring if running and you have
9
+ # installed the spring binstubs per the docs)
10
+ # * zeus: 'zeus rspec' (requires the server to be started separetly)
11
+ # * 'just' rspec: 'rspec'
12
+ guard 'ctags-bundler', :src_path => ["lib", "spec/support"] do
13
+ watch(/^(app|lib|spec\/support)\/.*\.rb$/)
14
+ watch('Gemfile.lock')
15
+ end
16
+
17
+ guard :rspec, cmd: 'bundle exec rspec' do
18
+ watch(%r{^spec/.+_spec\.rb$})
19
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
20
+ watch('spec/spec_helper.rb') { "spec" }
21
+
22
+ # Rails example
23
+ watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
24
+
25
+ # Turnip features and steps
26
+ watch(%r{^spec/acceptance/(.+)\.feature$})
27
+ watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }
28
+ end
29
+
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Danil Pismenny
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1 @@
1
+ # Ruby-клиент для REST XML API AmoCRM
@@ -0,0 +1,14 @@
1
+ require "bundler/gem_tasks"
2
+ begin
3
+ require 'rspec/core/rake_task'
4
+ RSpec::Core::RakeTask.new(:spec)
5
+ task :default => :spec
6
+
7
+ rescue LoadError
8
+ end
9
+
10
+ require 'yard'
11
+ YARD::Rake::YardocTask.new do |t|
12
+ t.files = ['lib/**/*.rb'] # optional
13
+ #t.options = ['--any', '--extra', '--opts'] # optional
14
+ end
@@ -0,0 +1,41 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'amo_crm/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "amo_crm"
8
+ spec.version = AmoCRM::VERSION
9
+ spec.authors = ["Danil Pismenny"]
10
+ spec.email = ["danil@brandymint.ru"]
11
+ spec.summary = %q{Клиент для работы с API amocrm.ru}
12
+ spec.description = %q{Правильный клиент для работы c REST XML API moyslad.ru, созданный по принципам SOLID}
13
+ spec.homepage = "https://github.com/brandymint/amo_crm"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.required_ruby_version = '>= 2.0.0'
22
+
23
+ spec.add_runtime_dependency 'faraday'
24
+ spec.add_runtime_dependency 'virtus'
25
+ spec.add_runtime_dependency 'hashie'
26
+ spec.add_runtime_dependency 'faraday-cookie_jar'
27
+ spec.add_runtime_dependency 'nokogiri-happymapper'
28
+ spec.add_runtime_dependency 'activesupport', '>=3.0.0'
29
+
30
+ spec.add_development_dependency "bundler", "~> 1.6"
31
+ spec.add_development_dependency "rake"
32
+ spec.add_development_dependency "rspec"
33
+ spec.add_development_dependency "rspec-collection_matchers"
34
+ spec.add_development_dependency "pry"
35
+ spec.add_development_dependency "pry-nav"
36
+ spec.add_development_dependency "webmock"
37
+ spec.add_development_dependency "guard"
38
+ spec.add_development_dependency "guard-rspec"
39
+ spec.add_development_dependency 'guard-ctags-bundler'
40
+ spec.add_development_dependency 'yard'
41
+ end
@@ -0,0 +1,7 @@
1
+ require 'json'
2
+
3
+ a = AmoCRM::Client.new url: ENV['AMOCRM_URL'] || 'https://kiosk.amocrm.ru'
4
+ a.authorize user_login: ENV['AMOCRM_LOGIN'], user_hash: ENV['AMOCRM_APIKEY']
5
+ account = a.get('accounts/current')['account']
6
+
7
+ File.write 'account.json', JSON.pretty_generate(account.as_json)
@@ -0,0 +1,31 @@
1
+ require 'happymapper'
2
+ require 'active_support'
3
+ require 'active_model'
4
+ ActiveModel::Serializers::JSON
5
+ require "amo_crm/version"
6
+
7
+ require 'logger'
8
+
9
+ module AmoCRM
10
+ mattr_accessor :custom_logger
11
+ extend ActiveSupport::Autoload
12
+
13
+ autoload :Entities
14
+ autoload :Resources
15
+ autoload :Client
16
+ autoload :Universe
17
+
18
+ def self.logger= value
19
+ self.custom_logger = value
20
+ end
21
+
22
+ def self.logger
23
+ return custom_logger if custom_logger
24
+
25
+ if defined? Rails
26
+ Rails.logger
27
+ else
28
+ Logger.new STDERR
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,98 @@
1
+ require 'faraday'
2
+ require 'json'
3
+ require 'hashie'
4
+
5
+ require_relative 'client/errors'
6
+
7
+ class AmoCRM::Client
8
+ PREFIX_PATH = '/private/api/v2/json/'
9
+
10
+ SESSION_TIMEOUT = 15*60 # 15 minutes - https://developers.amocrm.ru/rest_api/auth.php
11
+
12
+ def initialize url:
13
+ @connection = Faraday.new url
14
+ # 'private/api/v2/json/'
15
+ # connection.use :cookie_jar
16
+ # connection.response :logger
17
+ connection.request :url_encoded
18
+ end
19
+
20
+ def authorize(user_login: , user_hash: )
21
+ logger.debug "Client: Authorize #{user_login}"
22
+ path = "private/api/auth.php?type=json"
23
+ data = { USER_LOGIN: user_login, USER_HASH: user_hash }
24
+ @user_login = user_login
25
+ @user_hash = user_hash
26
+ response = connection.post path, data
27
+ # Success reponse header example:
28
+ # "set-cookie"=> "session_id=nic2a7t2mauuooc8a45pbm3p85nlf1nm9tr4cltpfl3r5kfk4um0; path=/; domain=.amocrm.ru, BITRIX_SM_LOGIN=danil%40brandymint.ru; expires=Thu, 26-Aug-2021 13:34:47 GMT; Max-
29
+ # "expires"=>"Thu, 19 Nov 1981 08:52:00 GMT",
30
+ #
31
+ # Success response body example:
32
+ # "{\"response\":{\"auth\":true,\"accounts\":[{\"id\":\"11413681\",\"name\":\"Kiosk\",\"subdomain\":\"kiosk\",\"language\":\"ru\",\"timezone\":\"Europe\\/Moscow\"}],\"server_time\":1474464887}}"
33
+ @authorized_at = Time.zone.now
34
+ @cookie = response.headers['set-cookie']
35
+ logger.debug "Client: Authorization response #{response.body}"
36
+ validate response
37
+ end
38
+
39
+ def authorize!(user_login: , user_hash: )
40
+ res = authorize user_login: user_login, user_hash: user_hash
41
+ raise "AmoCRM is not authorized #{res}" unless res.auth
42
+
43
+ res
44
+ end
45
+
46
+ def get path, params={}
47
+ logger.debug "Client: GET #{path} #{params}"
48
+ check_authorization!
49
+ validate connection.get PREFIX_PATH + path, params, { Cookie: cookie }
50
+ end
51
+
52
+ def post path, data=nil
53
+ data = JSON.generate data.as_json
54
+ logger.debug "Client: POST #{path}: #{data}"
55
+ check_authorization!
56
+ validate connection.post PREFIX_PATH + path, data, { 'Content-Type' => 'application/json', Cookie: cookie }
57
+ end
58
+
59
+ def put path, data
60
+ data = JSON.generate data.as_json
61
+ logger.debug "Client: PUT #{path}: #{data}"
62
+ check_authorization!
63
+ validate connection.put PREFIX_PATH + path, data, { 'Content-Type' => 'application/json', Cookie: cookie }
64
+ end
65
+
66
+ def delete path
67
+ check_authorization!
68
+ validate connection.delete PREFIX_PATH path
69
+ end
70
+
71
+ private
72
+
73
+ attr_reader :connection, :cookie
74
+
75
+ def check_authorization!
76
+ return true unless session_expired?
77
+ raise 'No user_login to reauthorize' unless @user_login
78
+ res = authorize user_login: @user_login, user_hash: @user_hash
79
+ raise "AmoCRM is not reauthorized #{res}" unless res.auth
80
+
81
+ res
82
+ end
83
+
84
+ def session_expired?
85
+ return !cookie || !@authorized_at || @authorized_at + SESSION_TIMEOUT <= Time.zone.now
86
+ end
87
+
88
+ def logger
89
+ AmoCRM.logger
90
+ end
91
+
92
+ def validate res
93
+ AmoCRM::Client::Errors.build res unless res.status == 200
94
+
95
+ data = JSON.parse(res.body)['response']
96
+ Hashie::Mash.new data
97
+ end
98
+ end
@@ -0,0 +1,18 @@
1
+ class AmoCRM::Client
2
+ class Error < StandardError
3
+ attr_reader :state, :body
4
+
5
+ def initialize state, body
6
+ @state = state
7
+ @body = body
8
+ end
9
+
10
+ def message
11
+ to_s
12
+ end
13
+
14
+ def to_s
15
+ "#{state}: #{body}"
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,16 @@
1
+ require_relative 'error'
2
+
3
+ class AmoCRM::Client
4
+ class Errors
5
+ def self.build res
6
+ if res.try(:body).present?
7
+ AmoCRM.logger.error "AmoCRM::Client: #{res.class} #{res.headers['Content-Type']} #{res.status}: #{res.env.url.to_s}\n#{res.body}"
8
+ body = JSON.parse(res.body) rescue { 'response' => body }
9
+ raise Error.new res.status, body['response']
10
+ else
11
+ AmoCRM.logger.error "AmoCRM::Client: #{res.class} #{res}"
12
+ raise Error.new 999, res.to_json
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,14 @@
1
+ module AmoCRM
2
+ module Entities
3
+ extend ActiveSupport::Autoload
4
+
5
+ %w{
6
+ Base
7
+ Account
8
+ CustomField
9
+ Company
10
+ }.each do |m|
11
+ autoload m
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,7 @@
1
+ module AmoCRM::Entities
2
+ class Account < Base
3
+ def self.parse(response)
4
+ new response
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,16 @@
1
+ module AmoCRM::Entities
2
+ class AttachmentDocument < Base
3
+ include AmoCRM::Entities::Common
4
+
5
+ tag 'file'
6
+
7
+ attribute :filename, String
8
+ attribute :miniatureUuid, String
9
+ attribute :name, String
10
+
11
+ #def self.parse node, options
12
+ ## TODO create AttachmentDocument
13
+ #''
14
+ #end
15
+ end
16
+ end
@@ -0,0 +1,71 @@
1
+ module AmoCRM::Entities
2
+ class Attribute < Base
3
+ include AmoCRM::Entities::Common
4
+
5
+ tag 'attribute'
6
+
7
+ #
8
+ attribute :metadataUuid, String
9
+ attribute :valueString, String
10
+ attribute :valueText, String
11
+ attribute :doubleValue, Float
12
+ attribute :longValue, Integer
13
+ attribute :booleanValue, Boolean
14
+ attribute :timeValue, String
15
+
16
+ attribute :agentValueUuid, String
17
+ attribute :goodValueUuid, String
18
+ attribute :placeValueUuid, String
19
+ attribute :consignmentValueUuid, String
20
+ attribute :employeeValueUuid, String
21
+ attribute :projectValueUuid, String
22
+ attribute :contractValueUuid, String
23
+
24
+ attribute :file, AmoCRM::Entities::AttachmentDocument
25
+
26
+ attribute :entityValueUuid, String
27
+
28
+ # Объект описывающий тип атритуба
29
+ #
30
+ # @return AmoCRM::Entities::AttributeMetadata
31
+ #
32
+ def metadata universe
33
+ universe.embedded_entity_metadata.subresource_by_name(:GoodFolder).find metadataUuid
34
+ end
35
+
36
+ # Название свойства, полученное из AttributeMetadata
37
+ #
38
+ # @return String
39
+ #
40
+ def attributeName universe
41
+ metadata(universe).name
42
+ end
43
+
44
+ # Значение свойства согласно его типу
45
+ #
46
+ # @return [String | AmoCRM::Entities::CustomEntity | Integer | Decimal ]
47
+ def get_value universe
48
+ md = metadata universe
49
+ case md.attrType
50
+ when 'ID_CUSTOM'
51
+ universe.custom_entities.find( entityValueUuid ).name
52
+ when 'LINK'
53
+ valueText
54
+ when 'TEXT'
55
+ valueText
56
+ when 'STRING'
57
+ valueString
58
+ when 'DOUBLE'
59
+ doubleValue
60
+ when 'LONG'
61
+ longValue
62
+ when 'TIME'
63
+ timeValue
64
+ when 'FILE'
65
+ file
66
+ else
67
+ raise "Не известный тип мета-аттрибута #{md.uuid}: #{md.attrType}"
68
+ end
69
+ end
70
+ end
71
+ end