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
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2e7ae8b46365bd3f82f6e554164e65f31990c69b
4
+ data.tar.gz: b1a26c4014c818b508e4146cfb57ff08affb71d9
5
+ SHA512:
6
+ metadata.gz: 4624d904cdbeff5bb751bfc2bdf9cd0a0ba3cb17620602d7cb771a3062c61d840d66c5d938ec1c5cd50b145b597fd76a15743caadc71e19d69d26a80e30aee63
7
+ data.tar.gz: 5ccd7a4e1427e12166b2fc4f8f7b7c43856adcaf6b627e5c2637860346872f96f30f95cd52eb95923f2e2084e8963abc5cfc5ca3000e811ca1ffbf8645ce9555
data/.gitignore ADDED
@@ -0,0 +1,25 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ *.swp
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
19
+ *~
20
+ *.bak
21
+ *.bundle
22
+ *.so
23
+ *.o
24
+ *.a
25
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1 @@
1
+ language: ruby
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'nokogiri-happymapper'
4
+ gem 'faraday'
5
+ gem 'activesupport', '>=3.0.0'
6
+ # Specify your gem's dependencies in moysklad.gemspec
7
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,24 @@
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 :rspec, cmd: 'bundle exec rspec' do
13
+ watch(%r{^spec/.+_spec\.rb$})
14
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
15
+ watch('spec/spec_helper.rb') { "spec" }
16
+
17
+ # Rails example
18
+ watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
19
+
20
+ # Turnip features and steps
21
+ watch(%r{^spec/acceptance/(.+)\.feature$})
22
+ watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }
23
+ end
24
+
data/LICENSE.txt ADDED
@@ -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.
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # Правильный Ruby-клиент для API moysklad.ru
2
+
3
+ [![Build Status](https://travis-ci.org/BrandyMint/moysklad.svg)](https://travis-ci.org/BrandyMint/moysklad)
4
+
5
+ Отличительные черты:
6
+
7
+ * Модуль разработан согласно принципам SOLID. Легко расширяем.
8
+
9
+ ## Возможности
10
+
11
+ * Легкое внесение изменений (добавление новых моделей и ресурсов)
12
+ * Удобный доступ ко всем действиям с ресурсами.
13
+ * Кеширование ресурса со всеми записями (используем ресурс как локальную базу)
14
+ * Понятные исключения при ошибках с сетью.
15
+
16
+ ## Установка
17
+
18
+ Все, как обычно, добавляем в Gemfile:
19
+
20
+ gem 'moysklad'
21
+
22
+ ## Использование
23
+
24
+ > client = MS::Client.new login:'ВАШ ЛОГИН', password:'ВАШ ПАРОЛЬ'
25
+
26
+ ## Тестирование
27
+
28
+ > ./scripts/rest.sh Consignment list
29
+
30
+ ## Ссылочки
31
+
32
+ * Картапамяти по API http://www.mindmeister.com/246618635/online-moysklad-ru
33
+ * Документация для разработчииков https://support.moysklad.ru/hc/ru/sections/200561443-Разработчикам
34
+
35
+ ## Присылайте пул-реквесты )
data/Rakefile ADDED
@@ -0,0 +1,8 @@
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
data/lib/moysklad.rb ADDED
@@ -0,0 +1,28 @@
1
+ require 'happymapper'
2
+ require 'active_support'
3
+ require "moysklad/version"
4
+ require 'logger'
5
+
6
+ module Moysklad
7
+ mattr_accessor :custom_logger
8
+ extend ActiveSupport::Autoload
9
+
10
+ autoload :Entities
11
+ autoload :Resources
12
+ autoload :Client
13
+ autoload :Universe
14
+
15
+ def self.logger= value
16
+ self.custom_logger = value
17
+ end
18
+
19
+ def self.logger
20
+ return custom_logger if custom_logger
21
+
22
+ if defined? Rails
23
+ Rails.logger
24
+ else
25
+ Logger.new STDERR
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,46 @@
1
+ require_relative 'client/errors'
2
+
3
+ class Moysklad::Client
4
+
5
+ URL = 'https://online.moysklad.ru'
6
+
7
+ def initialize login: nil, password: nil
8
+ @client = Faraday.new URL
9
+ @client.basic_auth login, password
10
+ end
11
+
12
+ def get path, params={}
13
+ logger.debug "Client: GET #{path} #{params}"
14
+ validate client.get path, params
15
+ end
16
+
17
+ def put path, data
18
+ logger.debug "Client: PUT #{path}"
19
+ result = client.put do |req|
20
+ req.url path
21
+ req.headers['Content-Type'] = 'application/xml'
22
+ req.headers['Accept'] = '*/*'
23
+ req.body = data
24
+ end
25
+ validate result
26
+ end
27
+
28
+ def delete path
29
+ validate client.delete path
30
+ end
31
+
32
+ private
33
+
34
+ attr_reader :client
35
+
36
+ def logger
37
+ Moysklad.logger
38
+ end
39
+
40
+ def validate res
41
+ return res.body if res.status == 200
42
+
43
+ Moysklad::Client::Error.build res
44
+ end
45
+
46
+ end
@@ -0,0 +1,72 @@
1
+ class Moysklad::Client
2
+ class Errors
3
+ def self.build
4
+ Moysklad.logger.warn "Moyskad::Client: #{res.status}: #{res.env.url.to_s}\n#{res.body}"
5
+
6
+ case res.status
7
+ when 405
8
+ raise MethodNotAllowedError.new res
9
+ when 401
10
+ raise UnauthorizedError.new res
11
+ when 404
12
+ raise NoResourceFound.new res.body
13
+ when 500
14
+ raise ParsedError.new(res)
15
+ else
16
+ raise ParsedError.new(res)
17
+ end
18
+ end
19
+ end
20
+ end
21
+
22
+ class Moysklad::Client
23
+ class Error < StandardError
24
+ attr_reader :message
25
+
26
+ def initialize mess
27
+ @message = mess
28
+ end
29
+
30
+ def to_s
31
+ message
32
+ end
33
+ end
34
+
35
+ class NoResourceFound < Error; end
36
+
37
+ class HtmlParsedError < Error
38
+ def initialize res
39
+ @result = res
40
+ @message = parse_title res.body
41
+ end
42
+
43
+ private
44
+
45
+ def parse_title body
46
+ doc = Nokogiri::HTML body
47
+ doc.css('body').css('h1').text
48
+ rescue => err
49
+ Moysklad.logger.debug "Moyskad::Client parse error #{err}: #{res.body}"
50
+ body
51
+ end
52
+ end
53
+
54
+ class MethodNotAllowedError < HtmlParsedError
55
+ end
56
+
57
+ class UnauthorizedError < HtmlParsedError
58
+ end
59
+
60
+ class ParsedError < Error
61
+ def initialize result
62
+ @status = result.status
63
+ @result = result
64
+ @error = Moysklad::Entities::Error.parse result.body
65
+ @message = @error.message
66
+ rescue => err
67
+ @message = "error in init #{err}: #{result.body}"
68
+ end
69
+
70
+ attr_reader :error
71
+ end
72
+ end
@@ -0,0 +1,36 @@
1
+ module Moysklad
2
+ module Entities
3
+ extend ActiveSupport::Autoload
4
+
5
+ autoload :XmlFix
6
+ autoload :CommonObject
7
+ autoload :CompanyConcern
8
+ autoload :Common
9
+
10
+ %w{
11
+ Good
12
+ Barcode
13
+ SalePrices
14
+ SalePrice
15
+ Error
16
+ CustomerOrderPosition
17
+ Consignment
18
+ MyCompany
19
+ Attribute
20
+ GoodRef
21
+ Base
22
+ CustomerOrder
23
+ Feature
24
+ EmbeddedEntityMetadata
25
+ Price
26
+ StockTO
27
+ Company
28
+ Warehouse
29
+ Collection
30
+ Page
31
+ }.each do |m|
32
+ autoload m
33
+ end
34
+ end
35
+
36
+ end
@@ -0,0 +1,11 @@
1
+ module Moysklad::Entities
2
+ class Attribute < Base
3
+ include Moysklad::Entities::Common
4
+
5
+ tag 'attribute'
6
+
7
+ attribute :metadataUuid, String
8
+ attribute :valueString, String
9
+
10
+ end
11
+ end
@@ -0,0 +1,13 @@
1
+ module Moysklad::Entities
2
+ class Barcode < Base
3
+ include Common
4
+
5
+ tag 'barcode'
6
+
7
+ attribute :barcode, String
8
+ # EAN13
9
+ attribute :barcodeType, String
10
+
11
+ end
12
+ end
13
+
@@ -0,0 +1,6 @@
1
+ module Moysklad::Entities
2
+ class Base
3
+ #include Object
4
+
5
+ end
6
+ end
@@ -0,0 +1,15 @@
1
+ module Moysklad::Entities
2
+ class Collection < Base
3
+ # Типовое количество элементов которое возвращает мойсклад
4
+ DEFAULT_COUNT = 1000
5
+ include HappyMapper
6
+
7
+ attribute :total, Integer
8
+ attribute :start, Integer
9
+ attribute :count, Integer
10
+
11
+ #has_many :consignment, Consignment, read_only: true
12
+ #has_many :feature, Feature, read_only: true
13
+
14
+ end
15
+ end
@@ -0,0 +1,16 @@
1
+ module Moysklad::Entities::Common
2
+ extend ::ActiveSupport::Concern
3
+
4
+ included do
5
+ include HappyMapper
6
+ include Moysklad::Entities::XmlFix
7
+
8
+ attribute :readMode, String
9
+ attribute :changeMode, String
10
+
11
+ element :accountUuid, String
12
+ element :accountId, String
13
+ element :uuid, String
14
+ end
15
+
16
+ end
@@ -0,0 +1,29 @@
1
+ module Moysklad::Entities::CommonObject
2
+ extend ActiveSupport::Concern
3
+
4
+ # Используется в Good
5
+ # https://online.moysklad.ru/exchange/rest/ms/xml/Good/list
6
+ #
7
+ # В Metadata
8
+ # https://online.moysklad.ru/exchange/rest/ms/xml/Metadata/list
9
+ #
10
+ # Во Feature
11
+ # https://online.moysklad.ru/exchange/rest/ms/xml/Feature/list
12
+ #
13
+ # CustomerOrder
14
+ # https://online.moysklad.ru/exchange/rest/ms/xml/CustomerOrder/list
15
+ #
16
+ included do
17
+ include Moysklad::Entities::Common
18
+
19
+ attribute :updated, Time
20
+ attribute :updatedBy, String
21
+
22
+ attribute :name, String
23
+
24
+ element :externalcode, String
25
+ element :description, String
26
+ end
27
+
28
+ end
29
+
@@ -0,0 +1,20 @@
1
+ module Moysklad::Entities
2
+ class Company < Base
3
+ include CompanyConcern
4
+
5
+ tag 'company'
6
+
7
+ attribute :payerVat, Boolean
8
+
9
+ attribute :priceTypeUuid, String
10
+
11
+ attribute :discount, Float
12
+ attribute :autoDiscount, Float
13
+ attribute :discountCardNumber, String
14
+ attribute :discountCorrection, Float
15
+
16
+ element :code, String
17
+ end
18
+ end
19
+
20
+