eco-helpers 2.7.13 → 2.7.15

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.
@@ -27,6 +27,7 @@ module Eco
27
27
  # @param attr_map [nil, Eco::Data::Mapper] attribute names mapper
28
28
  # to translate external names into internal ones and _vice versa_.
29
29
  def initialize(e, schema:, person_parser: nil, default_parser: nil, attr_map: nil)
30
+ super(e)
30
31
  msg = "Constructor needs a PersonSchema. Given: #{schema.class}"
31
32
  fatal msg unless schema.is_a?(Ecoportal::API::V1::PersonSchema)
32
33
 
@@ -35,7 +36,6 @@ module Eco
35
36
 
36
37
  msg = "Expecting Mapper object. Given: #{attr_map.class}"
37
38
  fatal msg if attr_map && !attr_map.is_a?(Eco::Data::Mapper)
38
- super(e)
39
39
 
40
40
  @schema = Ecoportal::API::V1::PersonSchema.new(JSON.parse(schema.doc.to_json))
41
41
  @source_person_parser = person_parser
@@ -2,9 +2,8 @@ module Eco
2
2
  module API
3
3
  class Session
4
4
  class Batch < Common::Session::BaseSession
5
-
6
5
  DEFAULT_BATCH_BLOCK = 50
7
- VALID_METHODS = [:get, :create, :update, :upsert, :delete]
6
+ VALID_METHODS = %i[get create update upsert delete].freeze
8
7
 
9
8
  class << self
10
9
  # @return [Boolean] `true` if the method is supported, `false` otherwise.
@@ -25,7 +24,8 @@ module Eco
25
24
  # @return [Array<People>] all the people based on `params`
26
25
  def get_people(people = nil, params: {}, silent: false)
27
26
  return launch(people, method: :get, params: params, silent: silent).people if people.is_a?(Enumerable)
28
- return get(params: params, silent: silent)
27
+
28
+ get(params: params, silent: silent)
29
29
  end
30
30
 
31
31
  # launches a batch of `method` type using `people` and the specified `params`
@@ -37,11 +37,11 @@ module Eco
37
37
  # @param params [Hash] api request options.
38
38
  # @option params [String] :per_page the number of people included per each batch api request.
39
39
  # @return [Batch::Status] the `status` of this batch launch.
40
- def launch(people, method:, params: {} , silent: false)
40
+ def launch(people, method:, params: {}, silent: false)
41
41
  batch_from(people, method: method, params: params, silent: silent)
42
42
  end
43
43
 
44
- def search(data, silent: false, params: {})
44
+ def search(data, silent: false, params: {}) # rubocop:disable Metrics/AbcSize
45
45
  params = {per_page: DEFAULT_BATCH_BLOCK}.merge(params)
46
46
 
47
47
  launch(data, method: :get, params: params, silent: silent).tap do |status|
@@ -50,36 +50,35 @@ module Eco
50
50
  entries = status.queue
51
51
  puts "\n"
52
52
  entries.each_with_index do |entry, i|
53
- if (i % 10 == 0)
53
+ if (i % 10).zero?
54
54
  percent = i * 100 / entries.length
55
55
  print "Searching: #{percent.round}% (#{i}/#{entries.length} entries)\r"
56
56
  $stdout.flush
57
57
  end
58
58
 
59
- unless status.success?(entry)
60
- email = nil
61
- case
62
- when entry.respond_to?(:email)
63
- email = entry.email
64
- when entry.respond_to?(:to_h)
65
- email = entry.to_h["email"]
66
- end
59
+ next if status.success?(entry)
67
60
 
68
- people_matching = []
69
- email = email.to_s.strip.downcase
70
- unless email.empty?
71
- people_matching = get(params: params.merge(q: email), silent: silent).select do |person|
72
- person.email == email
73
- end
74
- end
61
+ email = nil
62
+ if entry.respond_to?(:email)
63
+ email = entry.email
64
+ elsif entry.respond_to?(:to_h)
65
+ email = entry.to_h["email"]
66
+ end
75
67
 
76
- case people_matching.length
77
- when 1
78
- status.set_person_match(entry, people_matching.first)
79
- when 2..Float::INFINITY
80
- status.set_people_match(entry, people_matching)
68
+ people_matching = []
69
+ email = email.to_s.strip.downcase
70
+ unless email.empty?
71
+ people_matching = get(params: params.merge(q: email), silent: silent).select do |person|
72
+ person.email == email
81
73
  end
82
74
  end
75
+
76
+ case people_matching.length
77
+ when 1
78
+ status.set_person_match(entry, people_matching.first)
79
+ when 2..Float::INFINITY
80
+ status.set_people_match(entry, people_matching)
81
+ end
83
82
  end
84
83
  end
85
84
  end
@@ -87,42 +86,61 @@ module Eco
87
86
  private
88
87
 
89
88
  def get(params: {}, silent: false)
90
- fatal "cannot batch get without api connnection, please provide a valid api connection!" unless people_api = api&.people
89
+ msg = "cannot batch get without api connnection, please provide a valid api connection!"
90
+ fatal msg unless (people_api = api&.people)
91
+
91
92
  params = {per_page: DEFAULT_BATCH_BLOCK}.merge(params)
92
- return people_api.get_all(params: params, silent: silent)
93
+ people_api.get_all(params: params, silent: silent)
93
94
  end
94
95
 
95
-
96
96
  def batch_from(data, method:, params: {}, silent: false)
97
- fatal "Invalid batch method: #{method}." if !self.class.valid_method?(method)
97
+ fatal "Invalid batch method: #{method}." unless self.class.valid_method?(method)
98
98
  return nil if !data || !data.is_a?(Enumerable)
99
- fatal "cannot batch #{method} without api connnection, please provide a valid api connection!" unless people_api = api&.people
99
+
100
+ msg = "cannot batch #{method} without api connnection, please provide a valid api connection!"
101
+ fatal msg unless (people_api = api&.people)
100
102
 
101
103
  # param q does not make sense here, even for GET method
102
104
  params = {per_page: DEFAULT_BATCH_BLOCK}.merge(params)
103
105
  per_page = params[:per_page] || DEFAULT_BATCH_BLOCK
104
106
 
105
- launch_batch(data,
106
- method: method,
107
- per_page: per_page,
107
+ launch_batch(
108
+ data,
109
+ method: method,
110
+ per_page: per_page,
108
111
  people_api: people_api,
109
- silent: silent
112
+ silent: silent
110
113
  )
111
114
  end
112
115
 
113
- def launch_batch(data, method:, status: nil, job_mode: true, per_page: DEFAULT_BATCH_BLOCK, people_api: api&.people, silent: false)
114
- iteration = 1; done = 0
116
+ def launch_batch( # rubocop:disable Metrics/AbcSize
117
+ data,
118
+ method:,
119
+ status: nil,
120
+ job_mode: true, # rubocop:disable Lint/UnusedMethodArgument
121
+ per_page: DEFAULT_BATCH_BLOCK,
122
+ people_api: api&.people,
123
+ silent: false
124
+ )
125
+ iteration = 1
126
+ done = 0
115
127
  iterations = (data.length.to_f / per_page).ceil
116
128
 
117
- status ||= Eco::API::Session::Batch::Status.new(enviro, queue: data, method: method)
118
- status.tap do |status|
129
+ status ||= Eco::API::Session::Batch::Status.new(
130
+ enviro,
131
+ queue: data,
132
+ method: method
133
+ )
134
+
135
+ status.tap do
119
136
  start_time = Time.now
120
- start_slice = Time.now; slice = []
121
- pending_for_server_error = data.to_a[0..-1]
137
+ start_slice = Time.now
138
+
139
+ pending_for_server_error = data.to_a[0..]
122
140
  data.each_slice(per_page) do |slice|
123
141
  msg = "starting batch '#{method}' iteration #{iteration}/#{iterations},"
124
- msg += " with #{slice.length} entries of #{data.length} -- #{done} done"
125
- msg += " (last: #{str_stats(start_slice, slice.length)}; total: #{str_stats(start_time, done)})"
142
+ msg << " with #{slice.length} entries of #{data.length} -- #{done} done"
143
+ msg << " (last: #{str_stats(start_slice, slice.length)}; total: #{str_stats(start_time, done)})"
126
144
  logger.info(msg) unless silent
127
145
 
128
146
  start_slice = Time.now
@@ -130,11 +148,12 @@ module Eco
130
148
  people_api.batch(job_mode: false) do |batch|
131
149
  slice.each do |person|
132
150
  batch.public_send(method, person) do |response|
133
- faltal("Request with no response") unless !!response
134
- unless server_error?(response)
135
- pending_for_server_error.delete(person)
136
- status[person] = response
137
- end
151
+ faltal("Request with no response") unless response
152
+
153
+ next if server_error?(response)
154
+
155
+ pending_for_server_error.delete(person)
156
+ status[person] = response
138
157
  end
139
158
  end
140
159
  end # end batch
@@ -148,13 +167,15 @@ module Eco
148
167
  unless pending_for_server_error.empty?
149
168
  msg = "Going to re-try #{pending_for_server_error.count} due to server errors"
150
169
  logger.info(msg) unless silent
151
- launch_batch(pending_for_server_error,
152
- status: status,
153
- method: method,
154
- job_mode: false,
155
- per_page: per_page,
170
+
171
+ launch_batch(
172
+ pending_for_server_error,
173
+ status: status,
174
+ method: method,
175
+ job_mode: false,
176
+ per_page: per_page,
156
177
  people_api: people_api,
157
- silent: silent
178
+ silent: silent
158
179
  )
159
180
  end
160
181
  end
@@ -164,24 +185,23 @@ module Eco
164
185
  res_status = response.status
165
186
  server_error = !res_status || res_status.server_error?
166
187
  other_error = !server_error && (!res_status.code || res_status.code < 100)
167
- no_body = !server_error && !other_error && !response.body
188
+ no_body = !server_error && !other_error && !response.body
168
189
  server_error || other_error || no_body
169
190
  end
170
191
 
171
192
  def offer_retry_on(error_type, retries_left = 3, &block)
172
- begin
173
- block.call
174
- rescue error_type => e
175
- raise unless retries_left > 0
176
- explanation = "Batch TimeOut. You have #{retries_left} retries left."
177
- prompt_user(" Do you want to retry (y/N)?", default: "Y", explanation: explanation, timeout: 10) do |response|
178
- if response.upcase.start_with?("Y")
179
- puts "\nOkay... let's retry!"
180
- offer_retry_on(error_type, retries_left - 1, &block)
181
- else
182
- raise
183
- end
184
- end
193
+ block.call
194
+ rescue error_type
195
+ raise unless retries_left.positive?
196
+
197
+ explanation = "Batch TimeOut. You have #{retries_left} retries left."
198
+ question = " Do you want to retry (y/N)?"
199
+
200
+ prompt_user(question, default: "Y", explanation: explanation, timeout: 10) do |response|
201
+ raise unless response.upcase.start_with?("Y")
202
+
203
+ puts "\nOkay... let's retry!"
204
+ offer_retry_on(error_type, retries_left - 1, &block)
185
205
  end
186
206
  end
187
207
 
@@ -195,7 +215,6 @@ module Eco
195
215
  " -- "
196
216
  end
197
217
  end
198
-
199
218
  end
200
219
  end
201
220
  end
@@ -50,7 +50,9 @@ class Eco::API::UseCases::GraphQL::Samples::Location
50
50
  msg = "Expecting CommandResults object. Given: #{results.class}"
51
51
  raise msg unless results.is_a?(request_results_class)
52
52
 
53
- results.applied.each do |result|
53
+ target = simulate?? results.results : results.applied
54
+
55
+ target.each do |result|
54
56
  prev_id, new_id = result.command_input_data.values_at(:nodeId, :newId)
55
57
  next if new_id.nil? # not an id change
56
58
  next if prev_id == new_id
@@ -244,12 +244,12 @@ class Eco::API::UseCases::OozeSamples::RegisterUpdateCase < Eco::API::UseCases::
244
244
  end
245
245
 
246
246
  def conf_filters
247
- return filters if respond_to?(:filters)
247
+ filters if respond_to?(:filters)
248
248
  []
249
249
  end
250
250
 
251
251
  def conf_search
252
- return search if respond_to?(:search)
252
+ search if respond_to?(:search)
253
253
  end
254
254
 
255
255
  def register_id
data/lib/eco/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Eco
2
- VERSION = '2.7.13'.freeze
2
+ VERSION = '2.7.15'.freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eco-helpers
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.7.13
4
+ version: 2.7.15
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oscar Segura
@@ -534,6 +534,7 @@ extensions: []
534
534
  extra_rdoc_files: []
535
535
  files:
536
536
  - ".gitignore"
537
+ - ".markdownlint.jsonc"
537
538
  - ".rspec"
538
539
  - ".rubocop.yml"
539
540
  - ".yardopts"