erp_integration 0.7.0 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0b7edee1b77a371325210af94802cbd0d6c1df5d730bcfd8d9d326bc8a5eb2c3
4
- data.tar.gz: d59ecb758fde883746da4fec059e99c53b71f7a526f81d7fa272eb139f567ff4
3
+ metadata.gz: 2636d8881ff93450aabba350ae21870e98ebf72dbe104b952567a37f49685fde
4
+ data.tar.gz: 84d2fc64b31a928e93d10c9090f7f1ebbbe187b933889437c62f914b6dacc72a
5
5
  SHA512:
6
- metadata.gz: 0c34649c0ab233be00b9e9b35897faa934eb85824318528f7df4261b8f093319902d82636bd6e4cbba22d82afec604e6d4136b6f64ad7398ee2227aa5b17dca9
7
- data.tar.gz: 70cac86409b77043c0d0ec4f62946d01b48bd96b4abfaaf88c3b57f9600ada205c505b072ac91836ca693621c37302e2223b65b2769e8157ff2fe0b4c734eb10
6
+ metadata.gz: bc1fe8cf4bd8739773092308b849f78fa40e740d347850b887f5e33f1d5df873d07635b9b66249ed5c9297e1405f0058f03d99eb433e9d32fdf330686c4cd129
7
+ data.tar.gz: 43e155e350b9e3cf3be08a3c08770213b5cf06f81c24d579971a75d62d173f21a3a6d49cd316ff9508df9d3548a0df444621870416f951663eacfd63d26265b9
@@ -5,21 +5,26 @@ module ErpIntegration
5
5
  module Persistence
6
6
  # Allows creating new resources in Fulfil.
7
7
  #
8
- # @example
9
- # $ ErpIntegration::Product.create(code: 'MT100eu', variant: 10)
10
- # => <ErpIntegration::Product @id=2 @code="MT100eu" @variant=10 />
11
- #
12
- # @param attributes [Hash] A list of attributes.
13
- # @return [ErpIntegration::Resource] The newly created resource.
14
- def create(attributes = {})
8
+ # @param attributes [Hash] A list of attributes for the new resource.
9
+ # @return [Array|Hash] The response from the API
10
+ def create(attributes)
15
11
  client
16
12
  .post("model/#{model_name}", normalize_attributes(attributes))
17
- .map { |new_record_id| resource_klass.new(attributes.merge(id: new_record_id)) }
13
+ .map { |new_record_id| attributes.merge!(id: new_record_id) }
18
14
  .first
19
15
  rescue ErpIntegration::HttpError::BadRequest => e
20
- record = resource_klass.new(attributes)
21
- record.errors.add(extract_error_message(e))
22
- record
16
+ [attributes, [extract_error_message(e)]]
17
+ end
18
+
19
+ # Updates the resource with the given attributes.
20
+ #
21
+ # @param resource_id [Integer] The ID of the resource.
22
+ # @param attributes [Hash] A list of attributes to update for the resource.
23
+ # @return [Array|Hash] The response from the API
24
+ def update(resource_id, attributes)
25
+ client.put("model/#{model_name}/#{resource_id}", attributes)
26
+ rescue ErpIntegration::HttpError::BadRequest => e
27
+ [attributes, [extract_error_message(e)]]
23
28
  end
24
29
 
25
30
  private
@@ -29,22 +29,25 @@ module ErpIntegration
29
29
  # 5. Create a new adapter class prefixed with the adapter's name
30
30
  # (e.g. `FulfilOrder` for the `Order` resource in the `lib/erp_integration/orders` folder).
31
31
  class Resource
32
+ include Resources::Validations
33
+ include Resources::Persistence
34
+
32
35
  attr_accessor :raw_api_response
33
36
 
34
37
  def initialize(attributes = {})
35
38
  @raw_api_response = attributes
39
+ assign_attributes(attributes)
40
+ end
36
41
 
42
+ # Allows assigning attributes to an resource instance.
43
+ # @param attributes [Hash] A list of attributes for the resource.
44
+ # @return [Hash] The list of assigned attributes.
45
+ def assign_attributes(attributes = {})
37
46
  attributes.each_pair do |name, value|
38
47
  public_send(:"#{name}=", value) if respond_to?(:"#{name}=")
39
48
  end
40
49
  end
41
50
 
42
- # Exposes all errors related to an `ErpIntegration::Resource`.
43
- # @return [ErpIntegration::Resources::Errors] Encapsulation of all error messages.
44
- def errors
45
- @errors ||= Resources::Errors.new
46
- end
47
-
48
51
  class << self
49
52
  # Dynamically defines and loads the adapter for the class inheriting from
50
53
  # the `ErpIntegration::Resource`.
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ErpIntegration
4
+ module Resources
5
+ module Persistence
6
+ def self.included(base)
7
+ base.extend ClassMethods
8
+ end
9
+
10
+ module ClassMethods
11
+ # Creates a new resource in the ERP.
12
+ # @param attributes [Hash] A list of attributes for the new resource.
13
+ # @return [ErpIntegration::Resource] The (to be) created resource.
14
+ def create(attributes)
15
+ attrs, error_messages = adapter.create(**attributes)
16
+
17
+ new_resource = new(attrs)
18
+ new_resource.validate_with(error_messages)
19
+ new_resource
20
+ end
21
+ end
22
+
23
+ # Determines whether a `ErpIntegration::Resource` is considered to be persisted.
24
+ # @return [Boolean] Whether it's persisted or not.
25
+ def persisted?
26
+ !id.nil?
27
+ end
28
+
29
+ # Update an resource in the ERP.
30
+ # @param attributes [Hash] A list of attributes to update.
31
+ # @return [Boolean] Whether the update action was succcesful or not.
32
+ def update(attributes)
33
+ attrs, error_messages = self.class.adapter.update(id, **attributes)
34
+
35
+ assign_attributes(attrs)
36
+ validate_with(error_messages)
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ErpIntegration
4
+ module Resources
5
+ module Validations
6
+ # Exposes all errors related to an `ErpIntegration::Resource`.
7
+ #
8
+ # @return [ErpIntegration::Resources::Errors] Encapsulation of all error messages.
9
+ def errors
10
+ @errors ||= Resources::Errors.new
11
+ end
12
+
13
+ # Checks whether an resource is considered valid or not.
14
+ #
15
+ # @return [Boolean] Is the resource valid according to the ERP?
16
+ def valid?
17
+ errors.none?
18
+ end
19
+
20
+ # Validates the resource by checking the given error messages.
21
+ #
22
+ # @param error_messages [Array] A list of error messages.
23
+ # @return [Boolean] Whether the resource is valid or not.
24
+ def validate_with(*error_messages)
25
+ errors.clear
26
+
27
+ error_messages.flatten.compact.each do |error_message|
28
+ errors.add(error_message)
29
+ end
30
+
31
+ valid?
32
+ end
33
+ end
34
+ end
35
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ErpIntegration
4
- VERSION = '0.7.0'
4
+ VERSION = '0.8.0'
5
5
  end
@@ -36,5 +36,7 @@ module ErpIntegration
36
36
 
37
37
  module Resources
38
38
  autoload :Errors, 'erp_integration/resources/errors'
39
+ autoload :Persistence, 'erp_integration/resources/persistence'
40
+ autoload :Validations, 'erp_integration/resources/validations'
39
41
  end
40
42
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: erp_integration
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stefan Vermaas
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-01-11 00:00:00.000000000 Z
11
+ date: 2022-01-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -224,7 +224,7 @@ dependencies:
224
224
  - - '='
225
225
  - !ruby/object:Gem::Version
226
226
  version: 1.19.2
227
- description:
227
+ description:
228
228
  email:
229
229
  - stefan@knowndecimal.com
230
230
  executables: []
@@ -291,6 +291,8 @@ files:
291
291
  - lib/erp_integration/purchase_request.rb
292
292
  - lib/erp_integration/resource.rb
293
293
  - lib/erp_integration/resources/errors.rb
294
+ - lib/erp_integration/resources/persistence.rb
295
+ - lib/erp_integration/resources/validations.rb
294
296
  - lib/erp_integration/sales_order.rb
295
297
  - lib/erp_integration/sales_order_line.rb
296
298
  - lib/erp_integration/stock_move.rb
@@ -304,7 +306,7 @@ metadata:
304
306
  homepage_uri: https://www.github.com/mejuri-inc/erp-integration
305
307
  source_code_uri: https://www.github.com/mejuri-inc/erp-integration
306
308
  changelog_uri: https://www.github.com/mejuri-inc/erp-integration/blob/main/CHANGELOG.md
307
- post_install_message:
309
+ post_install_message:
308
310
  rdoc_options: []
309
311
  require_paths:
310
312
  - lib
@@ -320,7 +322,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
320
322
  version: '0'
321
323
  requirements: []
322
324
  rubygems_version: 3.2.22
323
- signing_key:
325
+ signing_key:
324
326
  specification_version: 4
325
327
  summary: Connects Mejuri with third-party ERP vendors
326
328
  test_files: []