erp_integration 0.16.0 → 0.18.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c7ff2e87acc7c4f7745cfcde8ba1d63782b62a299f5a45aff3c51432bda90573
4
- data.tar.gz: 77dea511053000c3906a188b81d7352257ceb00748b16f437f868b11e454f4e2
3
+ metadata.gz: 06ef5b266cf00ceea7a3f9edda9f8a97245126b091cdae782ba847b02db82d55
4
+ data.tar.gz: b03d2ba35b1d16d810d00c1e23b608277ed636d565ce320dd2db15149dee7d4d
5
5
  SHA512:
6
- metadata.gz: 21ddea6ca39edef5b14c05e6503fff43c0b6d91589ce45b9bb1694babeb1d7786bc08c93df899dff6898a643863b3d85414575092874b8af63ad848eeeaa105b
7
- data.tar.gz: fe029b567c7d4f50461dfabe03bc231a2eb7482dc3eff962ef16c0843c4ed8bf0b9049d8513e6bdb15e735a8b9633d1a00869799eb704584005b8141e8f4a12e
6
+ metadata.gz: 2210bedf6de54f4fcc4e215f6e7f9a357e4398b89555a1348c1baca68be74d763062b256b4f99ae08bd370646f23742f274d00ff3747370e06a54aca8747f2ff
7
+ data.tar.gz: 3b8a4877b3730a41b651ab6e2e637a14f2599b3718d3a9f3da8b2c2b1371478f8d520cae94a63835a45cfddf8cd5f175622bd61fdfad1502af99f201ff35ea29
@@ -2,6 +2,7 @@
2
2
 
3
3
  require_relative 'context'
4
4
  require_relative 'finder_methods'
5
+ require_relative 'pagination_methods'
5
6
  require_relative 'persistence'
6
7
  require_relative 'query_methods'
7
8
 
@@ -11,6 +12,7 @@ module ErpIntegration
11
12
  include Context
12
13
  include Enumerable
13
14
  include FinderMethods
15
+ include PaginationMethods
14
16
  include Persistence
15
17
  include QueryMethods
16
18
 
@@ -63,10 +65,35 @@ module ErpIntegration
63
65
  @results =
64
66
  client.put(
65
67
  api_resource_path,
66
- Query.new(selected_fields, where_clauses)
68
+ Query.new(
69
+ fields: selected_fields,
70
+ filters: where_clauses,
71
+ alternative_filters: or_clauses,
72
+ offset: offset_clause,
73
+ limit: limit_clause
74
+ )
67
75
  ).map { |item| resource_klass.new(item) }
68
76
  end
69
77
 
78
+ # As with the `all` method, the `query methods` lazyly build a search/read
79
+ # or search/count query for Fulfil.
80
+ # By calling `count`, the prepared search/count will actually be executed and
81
+ # the result will be fetched
82
+ # @return [Integer] The count of records that match with the query in Fulfil
83
+ def count
84
+ return @count if defined?(@count)
85
+
86
+ @count =
87
+ client.put(
88
+ "model/#{model_name}/search_count",
89
+ Query.new(
90
+ fields: nil,
91
+ filters: where_clauses,
92
+ alternative_filters: or_clauses
93
+ ).to_h.except(:fields, :offset, :limit)
94
+ )
95
+ end
96
+
70
97
  # The `each` method turns the `ApiResource` instance into an enumerable object.
71
98
  # For more information, see https://ruby-doc.org/core-3.0.2/Enumerable.html
72
99
  def each(&block)
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ErpIntegration
4
+ module Fulfil
5
+ class OrClause
6
+ LOGIC_OPERATOR = 'OR'
7
+
8
+ def initialize(where_clauses:)
9
+ @where_clauses = where_clauses
10
+ end
11
+
12
+ def to_filter
13
+ return [] unless @where_clauses.any?
14
+
15
+ @where_clauses.map(&:to_filter).unshift(LOGIC_OPERATOR)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ErpIntegration
4
+ module Fulfil
5
+ module PaginationMethods
6
+ DEFAULT_OFFSET = 0
7
+ MAX_LIMIT = 500 # Max limit set by FF
8
+
9
+ attr_accessor :offset_clause, :limit_clause
10
+
11
+ # Fulfil by default is returning us the first 500 records on every query that we made.
12
+ # The `offset` method, as the name sais, will allow us to move the offset of the results.
13
+ # @example
14
+ # $ ErpIntegration::SalesOrder.offset(10).all
15
+ # # => <ErpIntegration::Fulfil::Collection @items=[<ErpIntegration::SalesOrder @offset=10 />] />
16
+ def offset(offset)
17
+ self.offset_clause = offset
18
+
19
+ self
20
+ end
21
+
22
+ # Fulfil by default is returning us the first 500 records on every query that we made.
23
+ # The `limit` method will allow us to modify the amount of records that we want to receive.
24
+ # @example
25
+ # $ ErpIntegration::SalesOrder.limit(5).all
26
+ # # => <ErpIntegration::Fulfil::Collection @items=[<ErpIntegration::SalesOrder @limit=5 />] />
27
+ def limit(limit)
28
+ self.limit_clause = [limit, MAX_LIMIT].min
29
+
30
+ self
31
+ end
32
+ end
33
+ end
34
+ end
@@ -18,21 +18,32 @@ module ErpIntegration
18
18
  # @example
19
19
  # Faraday.post("/api-endpoint", Query.new([:id, 'product.id'], [WhereClause.new(key: :id, value: 100)]))
20
20
  class Query
21
- attr_reader :fields, :filters
21
+ delegate :to_json, to: :to_h
22
+
23
+ attr_reader :fields, :filters, :offset, :limit
24
+
22
25
  DEFAULT_FIELDS = %w[id].freeze
23
26
 
24
27
  # @param fields [Array<String|Symbol>] A list of fields for Fulfil.
25
28
  # @param filters [Array<WhereClause>] A list of where clauses for Fulfil.
26
- def initialize(fields, filters)
29
+ # @param alternative_filters [Array<OrClause>] A list of or clauses for Fulfil.
30
+ # @param offset [Integer] Offset for pagination.
31
+ # @param limit [Integer] Limit for pagination.
32
+ def initialize(fields:, filters:, alternative_filters: [], offset: nil, limit: nil)
27
33
  @fields = (fields || DEFAULT_FIELDS).map(&:to_s).uniq
28
34
  @filters = (filters || []).map(&:to_filter).uniq
35
+ @filters += alternative_filters.map(&:to_filter).uniq if alternative_filters&.any?
36
+ @offset = offset
37
+ @limit = limit
29
38
  end
30
39
 
31
- def to_json(*_object)
40
+ def to_h
32
41
  {
33
42
  fields: @fields,
34
- filters: @filters
35
- }.to_json
43
+ filters: @filters,
44
+ offset: @offset,
45
+ limit: @limit
46
+ }
36
47
  end
37
48
  end
38
49
  end
@@ -2,11 +2,12 @@
2
2
 
3
3
  require_relative 'query'
4
4
  require_relative 'where_clause'
5
+ require_relative 'or_clause'
5
6
 
6
7
  module ErpIntegration
7
8
  module Fulfil
8
9
  module QueryMethods
9
- attr_accessor :selected_fields, :where_clauses
10
+ attr_accessor :selected_fields, :where_clauses, :or_clauses
10
11
 
11
12
  # The `QueryMethods#select` works in two unique ways
12
13
  #
@@ -82,6 +83,47 @@ module ErpIntegration
82
83
  self
83
84
  end
84
85
 
86
+ # The `or` method introduces an interface to query resources in Fulfil
87
+ # Is quite similar to the `where` method but with one big difference.
88
+ # The `or` method allows us to use the OR operator with several conditions
89
+ #
90
+ # @example
91
+ # $ ErpIntegration::SalesOrder.or(id: 100, carrier: 12).all
92
+ # # => <ErpIntegration::Fulfil::Collection @items=[
93
+ # <ErpIntegration::SalesOrder:0x00007f8577cb6d40 @carrier=12, @id=3179663 />
94
+ # <ErpIntegration::SalesOrder:0x00007f8577cb6d40 @carrier=16, @id=100 /> ] />
95
+ #
96
+ # @example
97
+ # $ ErpIntegration::SalesOrder.or(id: [100, 200]).all
98
+ # # => <ErpIntegration::Fulfil::Collection @items=[
99
+ # <ErpIntegration::SalesOrder:0x00007f8577cb6d40 @id=100 />
100
+ # <ErpIntegration::SalesOrder:0x00007f8577cb6d40 @id=200 /> ] />
101
+ #
102
+ # Now we are just accepting the last 'or' method so if we have multiple
103
+ # we are only accepting the last one.
104
+ def or(*args)
105
+ clone.or!(*args)
106
+ end
107
+
108
+ def or!(args)
109
+ where_clauses = []
110
+ args.each_pair do |key, value_or_values|
111
+ [*value_or_values].each do |value|
112
+ where_clauses << WhereClause.new(
113
+ key: key, value: value
114
+ )
115
+ end
116
+ end
117
+
118
+ self.or_clauses = [
119
+ OrClause.new(
120
+ where_clauses: where_clauses
121
+ )
122
+ ]
123
+
124
+ self
125
+ end
126
+
85
127
  def where_ilike(args)
86
128
  where(args.merge(comparison_operator: 'ilike'))
87
129
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ErpIntegration
4
- VERSION = '0.16.0'
4
+ VERSION = '0.18.0'
5
5
  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.16.0
4
+ version: 0.18.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stefan Vermaas
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-12-14 00:00:00.000000000 Z
11
+ date: 2023-01-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -271,6 +271,8 @@ files:
271
271
  - lib/erp_integration/fulfil/client.rb
272
272
  - lib/erp_integration/fulfil/context.rb
273
273
  - lib/erp_integration/fulfil/finder_methods.rb
274
+ - lib/erp_integration/fulfil/or_clause.rb
275
+ - lib/erp_integration/fulfil/pagination_methods.rb
274
276
  - lib/erp_integration/fulfil/persistence.rb
275
277
  - lib/erp_integration/fulfil/query.rb
276
278
  - lib/erp_integration/fulfil/query_methods.rb
@@ -334,7 +336,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
334
336
  - !ruby/object:Gem::Version
335
337
  version: '0'
336
338
  requirements: []
337
- rubygems_version: 3.2.3
339
+ rubygems_version: 3.3.7
338
340
  signing_key:
339
341
  specification_version: 4
340
342
  summary: Connects Mejuri with third-party ERP vendors