eco-helpers 2.0.31 → 2.0.35

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: dd31d354667802423a4003fe347af67916015eaeac0d1682cbe511d7afc11b40
4
- data.tar.gz: d0f9d23f2ad6db33861441d9f865a9e5925eec7a1e4042f7246bbded8f61ce6f
3
+ metadata.gz: b4d6a312dda42afc3e6ba2becccf69d42121024d9b81e2c37276719089521971
4
+ data.tar.gz: a3536e8fba79c1600135b4c4fd523a9c3a5262d605c39c751a3dc278d096b96e
5
5
  SHA512:
6
- metadata.gz: fafda0f2b9f6866499df3a641598800ea5dd2c2536710a759bbd0d30a307f36da8cc1cb30056de965134aea509ce1700df152d233d322b41c590048569ecd23e
7
- data.tar.gz: d2f953274727fa35d8474da766dabee715f21b4ae227be9e0b4f31363d58fdcc1b0e410d10976b3f8608e14335d5f42c97dccd6a3d2e55ea37fb4a945d02d091
6
+ metadata.gz: 8fcb26823bc3b6e3d35522c13d410094ea056269c14dea3b47bcaf16991ff36cde9537507a6a21c952aa15f590fb1f5b1fc594e55d08f04782a8e2802c695d0c
7
+ data.tar.gz: fb2768c6e345da3dec034e7793876ecf25ae1cff76a059d98a38d9328c57884efb2a2f0b449eb28aea686ba074e477875fd8ba30201773170935f1c766fd45ae
data/CHANGELOG.md CHANGED
@@ -1,11 +1,58 @@
1
1
  # Change Log
2
2
  All notable changes to this project will be documented in this file.
3
3
 
4
- ## [2.0.31] - 2021-07-xx
4
+ ## [2.0.35] - 2021-08-30
5
5
 
6
6
  ### Added
7
+ - `Eco::API::UseCases::OozeSamples::OozeBaseCase`
8
+ - `#new_target` optional warning when switching `target`
9
+ - `block` that is called when switching target and there were unsaved changes
10
+ - `#dirty?` helper method to check if there are changes
11
+ - `#before_loading_new_target` hook to allow child classes to anticipate when the `target` is going to change
7
12
 
8
13
  ### Changed
14
+ - `Eco::API::UseCases::OozeSamples::Helpers::Shortcuts#object_reference` improved reference text
15
+ - `Eco::API::UseCases::OozeSamples::RegisterUpdateCase`
16
+ - added resilience when switching target (i.e. from one stage to another stage in same entry)
17
+ - added `batch_queue`
18
+
19
+ ### Fixed
20
+ - `Eco::API::UseCases::OozeSamples::RegisterUpdateCase`
21
+ - actual `batch_size` updates
22
+ - actual safe switch of target (i.e. against another stage of same ooze)
23
+
24
+ ## [2.0.34] - 2021-08-28
25
+
26
+ ### Added
27
+ - `Eco::API::UseCases::OozeSamples::OozeBaseCase` refactored ooze base case
28
+ - `Eco::API::UseCases::OozeSamples::RegisterUpdateCase` new case for full register update
29
+
30
+ ### Changed
31
+ - `Eco::API::UseCases::OozeSamples::OozeRunBaseCase` made child of `OozeBaseCase` and simplified to its essential
32
+ - `Eco::API::UseCases::OozeSamples::OozeUpdateCase` remove unnecessary code and helpers that are now inherited from `OoozeBaseCase`
33
+ - `Eco::API::UseCases::OozeSamples::OozeFromDocCase` remove unnecessary code
34
+
35
+
36
+ ## [2.0.33] - 2021-08-16
37
+
38
+ ### Added
39
+ - `Eco::API::UseCases::OozeSamples::OozeFromDocCase` use case to abstract form from doc
40
+ - `docx` dependency
41
+ - A couple more of helpers for the Ooze Cases: `#titleize` & `#normalize_string`
42
+
43
+ ### Changed
44
+ - `ecoporta-api-v2` **gem** (upgrade dependency)
45
+
46
+ ### Fixed
47
+ - `Eco::API::UseCases::OozeSamples::OozeUpdateCase`, helper `with_stage`
48
+
49
+ ## [2.0.32] - 2021-07-14
50
+
51
+ ### Fixed
52
+ - `Eco::API::UseCases::OozeSamples::OozeUpdateCase` prompting user, fixed typo
53
+ - `Eco::API::Session::Batch::Job`: `include-excluded` again... typo.
54
+
55
+ ## [2.0.31] - 2021-07-13
9
56
 
10
57
  ### Fixed
11
58
  - `Eco::API::Session::Batch::Job`: `include-excluded` was triggering a type error.
data/eco-helpers.gemspec CHANGED
@@ -31,7 +31,7 @@ Gem::Specification.new do |spec|
31
31
  spec.add_development_dependency "redcarpet", ">= 3.5.1", "< 3.6"
32
32
 
33
33
  spec.add_dependency 'ecoportal-api', '>= 0.8.3', '< 0.9'
34
- spec.add_dependency 'ecoportal-api-v2', '>= 0.8.7', '< 0.9'
34
+ spec.add_dependency 'ecoportal-api-v2', '>= 0.8.12', '< 0.9'
35
35
  spec.add_dependency 'aws-sdk-s3', '>= 1.83.0', '< 2'
36
36
  spec.add_dependency 'aws-sdk-ses', '>= 1.36.0', '< 2'
37
37
  spec.add_dependency 'dotenv', '>= 2.7.6', '< 2.8'
@@ -42,4 +42,5 @@ Gem::Specification.new do |spec|
42
42
  spec.add_dependency 'jaro_winkler', '>= 1.5.4', '< 1.6'
43
43
  spec.add_dependency 'roo', '>= 2.8.3', '< 2.9'
44
44
  spec.add_dependency 'roo-xls', '>= 1.2.0', '< 1.3'
45
+ spec.add_dependency 'docx', '>= 0.6.2', '< 0.7'
45
46
  end
@@ -252,14 +252,14 @@ module Eco
252
252
  # - filter out excluded entries from the api update
253
253
  def api_included(full_queue)
254
254
  return full_queue if type == :create
255
- return full_queue unless excluded = session.config.people.api_excluded
255
+ return full_queue unless excluded_callback = session.config.people.api_excluded
256
256
  excluded = options.dig(:include, :excluded)
257
257
  if excluded.is_a?(Hash) && excluded[:only]
258
- full_queue.select {|entry| excluded.call(entry, session, options, self)}
258
+ full_queue.select {|entry| excluded_callback.call(entry, session, options, self)}
259
259
  elsif options.dig(:include, :excluded)
260
260
  full_queue
261
261
  else
262
- full_queue.select {|entry| !excluded.call(entry, session, options, self)}
262
+ full_queue.select {|entry| !excluded_callback.call(entry, session, options, self)}
263
263
  end
264
264
  end
265
265
 
@@ -139,6 +139,7 @@ module Eco
139
139
  explanation = "Batch TimeOut. You have #{retries_left} retries left."
140
140
  prompt_user(" Do you want to retry (y/N)?", default: "Y", explanation: explanation, timeout: 10) do |response|
141
141
  if response.upcase.start_with?("Y")
142
+ puts "\nOkay... let's retry!"
142
143
  offer_retry_on(error_type, retries_left - 1, &block)
143
144
  else
144
145
  raise
@@ -3,7 +3,7 @@ class Eco::API::UseCases::DefaultCases::UpsertCase < Eco::API::Common::Loaders::
3
3
  type :sync
4
4
 
5
5
  attr_reader :options
6
-
6
+
7
7
  def main(entries, people, session, options, usecase)
8
8
  @options = options
9
9
  micro = session.micro
@@ -0,0 +1,77 @@
1
+ module Eco
2
+ module API
3
+ class UseCases
4
+ class OozeSamples
5
+ module Helpers
6
+ module Shortcuts
7
+
8
+ # Offers multiple ways to compare two strings
9
+ def same_string?(value1, value2, exact: false)
10
+ case
11
+ when value1.is_a?(String) && value2.is_a?(String)
12
+ if exact
13
+ value1 == value2
14
+ else
15
+ value1.to_s.strip.downcase == value2.to_s.strip.downcase
16
+ end
17
+ when value1.is_a?(Regexp) && value2.is_a?(String)
18
+ value2 =~ value1
19
+ when value1.is_a?(String) && value2.is_a?(Regexp)
20
+ value1 =~ value2
21
+ else
22
+ value1 == value2
23
+ end
24
+ end
25
+
26
+ def titleize(str)
27
+ return nil unless str
28
+ return str if str.strip.empty?
29
+ str.split(/\s+/).map do |part|
30
+ part[0] = part[0].upcase
31
+ part[1..-1] = part[1..-1].downcase
32
+ part
33
+ end.join(" ")
34
+ end
35
+
36
+ def normalize_string(str)
37
+ return nil unless str
38
+ str.gsub(/[^[:print:]]/, '')
39
+ .gsub(/[[:space:]]+/, ' ')
40
+ .gsub(/[[:space:]]$/, '')
41
+ .gsub(/[-\u2011\u2012\u2013]/, '-').yield_self do |str|
42
+ str = yield(str) if block_given?
43
+ str
44
+ end
45
+ end
46
+
47
+ def clean_question(str)
48
+ return nil unless str
49
+ normalize_string(str) do |str|
50
+ str.gsub(/\r\n/, ' ').yield_self do |str|
51
+ str = yield(str) if block_given?
52
+ str
53
+ end
54
+ end
55
+ end
56
+
57
+ def object_reference(obj)
58
+ return "No reference" unless obj
59
+ "".tap do |ref|
60
+ case obj
61
+ when Ecoportal::API::V2::Page::Stage
62
+ ref << "Stage"
63
+ when Ecoportal::API::V2::Pages::PageStage
64
+ ref << "Page (#{obj.id}) (#{object_reference(obj.current_stage)})"
65
+ when Ecoportal::API::V2::Page
66
+ ref << "Page"
67
+ end
68
+ ref << " '#{obj.name}'" if obj.respond_to?(:name)
69
+ end
70
+ end
71
+
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,13 @@
1
+ require_relative 'helpers/shortcuts.rb'
2
+
3
+ module Eco
4
+ module API
5
+ class UseCases
6
+ class OozeSamples
7
+ module Helpers
8
+ include OozeSamples::Helpers::Shortcuts
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,142 @@
1
+ # @attr_reader target [Ecoportal::API::V2::Page] current target ooze.
2
+ class Eco::API::UseCases::OozeSamples::OozeBaseCase < Eco::API::Common::Loaders::UseCase
3
+ name "ooze-base"
4
+ type :other
5
+
6
+ include Eco::API::UseCases::OozeSamples::Helpers
7
+
8
+ attr_reader :session, :options, :usecase
9
+ attr_reader :target
10
+
11
+ SAVE_PATCH = "ooze_patch_update.json"
12
+ DRY_COUNT = 5
13
+
14
+ def main(session, options, usecase)
15
+ raise "You need to inherit from this class ('#{self.class}') and call super with a block" unless block_given?
16
+ @session = session; @options = options; @usecase = usecase
17
+ @target = nil
18
+ yield
19
+ end
20
+
21
+ # Write here your script
22
+ def process_ooze(ooze = target)
23
+ raise "You need to inherit from this class ('#{self.class}') and call super with a block" unless block_given?
24
+ yield(ooze)
25
+ update_ooze(ooze)
26
+ end
27
+
28
+ private
29
+
30
+ # Hook method to use before the target is switched
31
+ def before_loading_new_target(ooze_id)
32
+ end
33
+
34
+ def ooze(ooze_id = nil, stage_id: nil)
35
+ return target unless ooze_id
36
+ before_loading_new_target(ooze_id)
37
+ apiv2.pages.get(ooze_id, stage_id: stage_id).tap do |ooze|
38
+ if ooze
39
+ new_target(ooze)
40
+ logger.info("Got #{object_reference(ooze)}")
41
+ else
42
+ exit_error "Could not get ooze '#{ooze_id}'"
43
+ end
44
+ end
45
+ end
46
+
47
+ def stage(id_name = nil, ooze: target)
48
+ if ooze_id = ooze && ooze.id
49
+ exit_error "#{object_reference(ooze)} does not have stages!" unless ooze.stages?
50
+ else
51
+ exit_error "There's no target ooze to get retrieve stages from"
52
+ end
53
+
54
+ if stg = ooze.stages[id_name] || ooze.stages.get_by_name(id_name)
55
+ return ooze if ooze.respond_to?(:current_stage_id) && (ooze.current_stage_id == stg.id)
56
+ before_loading_new_target(ooze_id)
57
+ return apiv2.pages.get(ooze_id, stage_id: stg.id).tap do |stage|
58
+ if stage
59
+ new_target(stage)
60
+ logger.info("Got #{object_reference(stage)} from #{object_reference(ooze)}")
61
+ else
62
+ exit_error "Could not get stage '#{id_name}' in ooze '#{ooze_id}'"
63
+ end
64
+ end
65
+ end
66
+ exit_error "Stage '#{id_name}' doesn't exist in ooze '#{ooze_id}'"
67
+ end
68
+
69
+ def update_ooze(ooze = target)
70
+ if !options[:simulate]
71
+ return unless dirty?(ooze)
72
+ apiv2.pages.update(ooze).tap do |response|
73
+ if response.success?
74
+ ooze.consolidate!
75
+ logger.info("Successful update of #{object_reference(ooze)}")
76
+ else
77
+ logger.error("Could not update #{object_reference(ooze)} (created_at: #{ooze.created_at}: #{response.body}")
78
+ end
79
+ end
80
+ else
81
+ unless options.dig(:feedback, :only_stats)
82
+ if patch = (patch_doc(ooze) || {})["page"]
83
+ pp patch
84
+ end
85
+ end
86
+ backup_patch!(ooze)
87
+ exit(0) if dry_count > DRY_COUNT
88
+ end
89
+ end
90
+
91
+ def new_target(object, warn_pending_changes: true)
92
+ if dirty?(target)
93
+ if warn_pending_changes
94
+ msg = "You you are switching to a new target #{object_reference(object)}"
95
+ msg += " after doing unsaved changes to #{object_reference(target)}"
96
+ logger.warn msg
97
+ end
98
+ yield(target) if block_given?
99
+ end
100
+ @target = object
101
+ end
102
+
103
+ def dirty?(object)
104
+ object && patch_doc(object)["page"]
105
+ end
106
+
107
+ def backup_patch!(ooze = target)
108
+ unless patch = (patch_doc(ooze) || {})["page"]
109
+ logger.info "No changes to update for #{object_reference(ooze)}."
110
+ return
111
+ end
112
+
113
+ patch = patch_doc(ooze)
114
+ # store the request
115
+ File.open(SAVE_PATCH, "w") do |file|
116
+ #file << (patch_doc || {}).to_json
117
+ file << JSON.pretty_generate(patch || {})
118
+ end
119
+ # TODO: ooze_requests folder with subfolders: for each run
120
+ puts "Saved patch at: #{File.expand_path(SAVE_PATCH)}"
121
+ end
122
+
123
+ def patch_doc(ooze = target)
124
+ apiv2.pages.get_body(ooze)
125
+ end
126
+
127
+ def dry_count
128
+ @dry_count ||= 0
129
+ @dry_count += 1
130
+ end
131
+
132
+ def apiv2
133
+ @apiv2 ||= session.api(version: :oozes)
134
+ end
135
+
136
+ def exit_error(msg)
137
+ logger.error(msg)
138
+ exit(1)
139
+ end
140
+
141
+
142
+ end
@@ -0,0 +1,44 @@
1
+ require 'docx'
2
+ # Use case to abstract FORM from word document
3
+ class Eco::API::UseCases::OozeSamples::OozeFromDocCase < Eco::API::UseCases::OozeSamples::OozeUpdateCase
4
+ name "ooze-forms-case"
5
+ type :other
6
+
7
+ private
8
+
9
+ def with_column(num = 0)
10
+ with_tables do |table, i|
11
+ raise "Column num (#{num}) is to big. Table '#{i}' only has #{table.column_count} columns." if table.column_count < num
12
+ table.columns[num].cells.each_with_index do |cell_row, j|
13
+ txt = normalize_string(cell_row.text)
14
+ yield(txt, i, j, table, cell_row)
15
+ end
16
+ end
17
+ end
18
+
19
+ def with_tables
20
+ raise "There are no tables in the doc" unless table_count > 0
21
+ i = 0
22
+ doc.tables.each do |table|
23
+ yield(table, i) if block_given?
24
+ i += 1
25
+ end
26
+ end
27
+
28
+ def table_count
29
+ doc.tables.count
30
+ end
31
+
32
+ def tables?
33
+ table_count > 0
34
+ end
35
+
36
+ def doc
37
+ @doc ||= Docx::Document.open(input_file)
38
+ end
39
+
40
+ def input_file
41
+ options.dig(:source, :file)
42
+ end
43
+
44
+ end
@@ -0,0 +1,60 @@
1
+ # Base class to run a script against a single ooze.
2
+ class Eco::API::UseCases::OozeSamples::OozeRunBaseCase < Eco::API::UseCases::OozeSamples::OozeBaseCase
3
+ name "ooze-run-base"
4
+ type :other
5
+
6
+ attr_reader :session, :options, :usecase
7
+
8
+ SAVE_PATCH = "ooze_patch_update.json"
9
+
10
+ def main(session, options, usecase, &block)
11
+ raise "You need to inherit from this class and call super with a block" unless block
12
+ @session = session; @options = options; @usecase = usecase
13
+ process_ooze(&block)
14
+ end
15
+
16
+ def process_ooze(ooze = target)
17
+ raise "You need to inherit from this class and call super with a block" unless block_given?
18
+ super(ooze) do
19
+ yield(ooze)
20
+ exit_if_no_changes!
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ def ooze
27
+ super(ooze_id, stage_id: stage_id)
28
+ end
29
+
30
+ def stage(stage_id_name = stage_id, ooze: target)
31
+ super(stage_id_name, ooze: ooze)
32
+ end
33
+
34
+ def ooze_id
35
+ options.dig(:source, :ooze_id)
36
+ end
37
+
38
+ def stage_id
39
+ options.dig(:source, :stage_id)
40
+ end
41
+
42
+ def update_ooze
43
+ prompt_to_confirm!
44
+ super
45
+ end
46
+
47
+ def exit_if_no_changes!
48
+ unless changes = !!patch_doc["page"]
49
+ logger.warn "No Changes!!"
50
+ exit(0)
51
+ end
52
+ end
53
+
54
+ def prompt_to_confirm!
55
+ micro.prompt_user("Do you want to proceed (y/N)?", default: "Y") do |response|
56
+ exit(1) unless response.upcase.start_with?("Y")
57
+ end
58
+ end
59
+
60
+ end
@@ -1,132 +1,54 @@
1
- class Eco::API::UseCases::OozeSamples::OozeUpdateCase < Eco::API::Common::Loaders::UseCase
1
+ class Eco::API::UseCases::OozeSamples::OozeUpdateCase < Eco::API::UseCases::OozeSamples::OozeRunBaseCase
2
2
  name "single-ooze-case"
3
3
  type :other
4
4
 
5
- attr_reader :session, :options, :usecase
6
-
7
- SAVE_PATCH = "ooze_patch_update.json"
8
-
9
- def main(session, options, usecase)
10
- @session = session; @options = options; @usecase = usecase
11
- yield
12
- end_script!
13
- end
14
-
15
5
  private
16
6
 
17
- def end_script!
18
- exit_if_no_changes!
19
- backup_patch!
20
- launch_request unless options[:simulate]
21
- exit(0)
22
- end
23
-
24
- def ooze_id
25
- options.dig(:source, :ooze_id)
26
- end
27
-
28
- def stage_id
29
- options.dig(:source, :stage_id)
30
- end
31
-
32
- def stage(id_name = stage_id)
33
- if stg = ooze.stages[id_name] || ooze.stages.get_by_name(id_name)
34
- return apiv2.pages.get(ooze_id, stage_id: stg.id).tap do |stage|
35
- if stage
36
- new_target(stage)
37
- logger.info("Got #{object_reference(stage)} from #{object_reference(ooze)}")
38
- else
39
- logger.error("Could not get stage '#{id_name}' in ooze '#{ooze_id}'")
40
- exit(1)
41
- end
42
- end
7
+ def with_fields(type: nil, label: nil)
8
+ flds = target.components
9
+ flds = flds.get_by_type(type) if type
10
+ flds = flds.select do |fld|
11
+ value = (label == :unnamed) ? nil : label
12
+ !label || same_string?(fld.label, value)
13
+ end.each do |field|
14
+ yield(field) if block_given?
43
15
  end
44
- raise "Stage '#{id_name}' doesn't exist in ooze '#{ooze_id}'"
45
16
  end
46
17
 
47
- def ooze
48
- @ooze ||= apiv2.pages.get(ooze_id).tap do |ooze|
49
- if ooze
50
- new_target(ooze)
51
- logger.info("Got #{object_reference(ooze)}")
52
- else
53
- logger.error("Could not get ooze '#{ooze_id}'")
54
- exit(1)
55
- end
18
+ def with_sections(type: nil, heading: nil)
19
+ secs = target.sections
20
+ secs = secs.get_by_type(type) if type
21
+ secs = secs.select do |sec|
22
+ value = (heading == :unnamed) ? nil : heading
23
+ !heading || same_string?(sec.heading, value)
24
+ end.each do |sec|
25
+ yield(sec) if block_given?
56
26
  end
57
27
  end
58
28
 
59
- def launch_request
60
- prompt_to_confirm!
61
-
62
- apiv2.pages.update(target).tap do |response|
63
- if response.success?
64
- logger.info("All went OK")
65
- else
66
- logger.error(response.body)
67
- end
29
+ def with_stage(name:)
30
+ if stage = target.stages.get_by_name(name)
31
+ yield(stage) if block_given?
68
32
  end
33
+ stage
69
34
  end
70
35
 
71
- def exit_if_no_changes!
72
- unless changes = !!patch_doc["page"]
73
- logger.warn "No Changes!!"
74
- exit(0)
36
+ def add_field(name, type, section, after: nil, side: :left)
37
+ raise "You need to specify a section for a new field. Given: #{section.class}" unless section.is_a?(Ecoportal::API::V2::Page::Section)
38
+ target.components.add(label: name, type: type) do |field|
39
+ section.add_component(field, after: after, side: side)
40
+ end.tap do |field|
41
+ yield(field) if block_given?
75
42
  end
76
43
  end
77
44
 
78
- def apiv2
79
- @apiv2 ||= session.api(version: :oozes)
45
+ def to_field(value)
46
+ fld = nil
47
+ fld ||= value if value.is_a?(Ecoportal::API::V2::Page::Component)
48
+ fld ||= target.components.get_by_id(value)
49
+ fld ||= target.components.select {|fld| same_name?(fld.label, value)}
50
+ #fld ||= value.reference if value.is_a?(Ecoportal::API::V2::Page::Force::Binding)
80
51
  end
81
52
 
82
- def target
83
- @target
84
- end
85
-
86
- def new_target(object)
87
- if @target && patch_doc["page"]
88
- logger.warn "You you are switching to a new target #{object_reference(object)} after doing unsaved changes to #{object_reference(@target)}"
89
- end
90
- @target = object
91
- end
92
-
93
- def object_reference(obj)
94
- return "No reference" unless obj
95
- "".tap do |ref|
96
- case obj
97
- when Ecoportal::API::V2::Page::Stage
98
- ref << "Stage "
99
- when Ecoportal::API::V2::Pages::PageStage
100
- ref << "StagePage "
101
- when Ecoportal::API::V2::Page
102
- ref << "Page "
103
- end
104
- ref << "'#{obj.name}' " if obj.respond_to?(:name)
105
- end
106
- end
107
-
108
-
109
- def patch_doc
110
- apiv2.pages.get_body(target)
111
- end
112
-
113
- def backup_patch!
114
- # store the request
115
- File.open(SAVE_PATCH, "w") do |file|
116
- #file << (patch_doc || {}).to_json
117
- file << JSON.pretty_generate(patch_doc || {})
118
- end
119
- puts "Saved patch at: #{File.expand_path(SAVE_PATCH)}"
120
- end
121
-
122
- def logger
123
- session.logger
124
- end
125
-
126
- def prompt_to_confirm!
127
- prompt_user("Do you want to proceed (y/N)?", default: "Y") do |response|
128
- exit(1) unless response.upcase.start_with?("Y")
129
- end
130
- end
131
53
 
132
54
  end
@@ -0,0 +1,160 @@
1
+ # Use case to update a register
2
+ # @note
3
+ # - You can define methods `filters` and `search` to change the target entries of the register
4
+ # - You need to define the `process_ooze` method
5
+ # This case expects `options[:source][:register_id]`
6
+ class Eco::API::UseCases::OozeSamples::RegisterUpdateCase < Eco::API::UseCases::OozeSamples::OozeBaseCase
7
+
8
+ class << self
9
+ # @return [Integer] the number of pages to be processed in each batch
10
+ def batch_size(size = nil)
11
+ @batch_size ||= 25
12
+ return @batch_size unless size
13
+ @batch_size = size
14
+ end
15
+ end
16
+
17
+ name "register-update-case"
18
+ type :other
19
+
20
+ def main(session, options, usecase, &block)
21
+ super(session, options, usecase) do
22
+ with_each_entry do |ooze|
23
+ process_ooze(&block)
24
+ end
25
+ end
26
+ end
27
+
28
+ def process_ooze(ooze = target)
29
+ raise "You need to define this method"
30
+ end
31
+
32
+ private
33
+
34
+ def before_loading_new_target(ooze_id)
35
+ if pending = queue_shift(ooze_id)
36
+ update_ooze(pending)
37
+ end
38
+ end
39
+
40
+ def new_target(object, warn_pending_changes: false)
41
+ enqueue(object)
42
+ super(object, warn_pending_changes: warn_pending_changes) do |pending|
43
+ #queue(pending)
44
+ end
45
+ end
46
+
47
+ def with_each_entry
48
+ batched_search_results do |page_results|
49
+ page_results.each do |page_result|
50
+ if pending = queue_shift(page_result.id)
51
+ if dirty?(pending)
52
+ msg = "Inconsistent search results. "
53
+ msg << "Launching update on '#{object_reference(pending)}' to be able to queue it back"
54
+ console.warn msg
55
+ update_ooze(pending)
56
+ end
57
+ end
58
+ if ooz = ooze(page_result.id)
59
+ yield(ooz)
60
+ end
61
+ end
62
+ update_oozes
63
+ end
64
+ end
65
+
66
+ def enqueue(object)
67
+ return unless object && object.respond_to?(:id)
68
+ if elem = queue_get(object.id)
69
+ msg = "Something is wrong. Native case 'RegisterUpdateCase' is "
70
+ if (elem != object) && dirty?(elem)
71
+ msg << "trying to queue different objects with same page id:\n"
72
+ msg << " • already queued (changes will go): #{object_reference(elem)}\n"
73
+ msg << " • tried to queue (lost changes): #{object_reference(object)}"
74
+ logger.warn msg
75
+ end
76
+ else
77
+ batch_queue << object unless batch_queue.include?(object)
78
+ end
79
+ end
80
+
81
+ def queue_shift(id = nil)
82
+ if id
83
+ batch_queue.delete(queue_get(id))
84
+ else
85
+ batch_queue.shift
86
+ end
87
+ end
88
+
89
+ def queue_get(id)
90
+ batch_queue.find {|e| e.id == id}
91
+ end
92
+
93
+ def batch_queue
94
+ @batch_queue ||= []
95
+ end
96
+
97
+ def batched_search_results
98
+ raise "Missing block. It yields in slices of #{self.class.batch_size} results" unless block_given?
99
+ results_preview
100
+ results = []
101
+ apiv2.registers.search(register_id, search_options) do |page_result|
102
+ results << page_result
103
+ if results.length >= self.class.batch_size
104
+ yield(results)
105
+ results = []
106
+ end
107
+ end
108
+ yield(results) unless results.empty?
109
+ end
110
+
111
+ def update_oozes(batched_oozes = batch_queue)
112
+ batched_oozes.each do |ooze|
113
+ update_ooze(ooze)
114
+ end
115
+ batched_oozes.clear
116
+ end
117
+
118
+ def backup_patch!(ooze = target)
119
+ unless patch = (patch_doc(ooze) || {})["page"]
120
+ logger.info "No changes to update for #{object_reference(ooze)}."
121
+ return
122
+ end
123
+ end
124
+
125
+ def results_preview
126
+ apiv2.registers.search(register_id, search_options.merge(only_first: true)).tap do |search_results|
127
+ str_results = "Total target entries: #{search_results.total} (out of #{search_results.total_before_filtering})"
128
+ session.prompt_user("Do you want to proceed (y/N):", explanation: str_results, default: "N", timeout: 10) do |res|
129
+ if res.upcase.start_with?("N")
130
+ puts "..."
131
+ logger.info "Aborting script..."
132
+ exit(0)
133
+ end
134
+ end
135
+ end
136
+ end
137
+
138
+ def search_options
139
+ @search_options ||= {}.tap do |opts|
140
+ opts.merge!(sort: "created_at")
141
+ opts.merge!(dir: "asc")
142
+ opts.merge!(query: conf_filters) if conf_search
143
+ opts.merge!(filters: conf_filters)
144
+ end
145
+ end
146
+
147
+ def conf_filters
148
+ return filters if self.respond_to?(:filters)
149
+ []
150
+ end
151
+
152
+ def conf_search
153
+ return search if self.respond_to?(:search)
154
+ end
155
+
156
+ def register_id
157
+ options.dig(:source, :register_id)
158
+ end
159
+
160
+ end
@@ -8,4 +8,9 @@ module Eco
8
8
  end
9
9
  end
10
10
 
11
+ require_relative 'ooze_samples/helpers'
12
+ require_relative 'ooze_samples/ooze_base_case'
13
+ require_relative 'ooze_samples/ooze_run_base_case'
11
14
  require_relative 'ooze_samples/ooze_update_case'
15
+ require_relative 'ooze_samples/ooze_from_doc_case'
16
+ require_relative 'ooze_samples/register_update_case'
data/lib/eco/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Eco
2
- VERSION = "2.0.31"
2
+ VERSION = "2.0.35"
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.0.31
4
+ version: 2.0.35
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oscar Segura
@@ -136,7 +136,7 @@ dependencies:
136
136
  requirements:
137
137
  - - ">="
138
138
  - !ruby/object:Gem::Version
139
- version: 0.8.7
139
+ version: 0.8.12
140
140
  - - "<"
141
141
  - !ruby/object:Gem::Version
142
142
  version: '0.9'
@@ -146,7 +146,7 @@ dependencies:
146
146
  requirements:
147
147
  - - ">="
148
148
  - !ruby/object:Gem::Version
149
- version: 0.8.7
149
+ version: 0.8.12
150
150
  - - "<"
151
151
  - !ruby/object:Gem::Version
152
152
  version: '0.9'
@@ -350,6 +350,26 @@ dependencies:
350
350
  - - "<"
351
351
  - !ruby/object:Gem::Version
352
352
  version: '1.3'
353
+ - !ruby/object:Gem::Dependency
354
+ name: docx
355
+ requirement: !ruby/object:Gem::Requirement
356
+ requirements:
357
+ - - ">="
358
+ - !ruby/object:Gem::Version
359
+ version: 0.6.2
360
+ - - "<"
361
+ - !ruby/object:Gem::Version
362
+ version: '0.7'
363
+ type: :runtime
364
+ prerelease: false
365
+ version_requirements: !ruby/object:Gem::Requirement
366
+ requirements:
367
+ - - ">="
368
+ - !ruby/object:Gem::Version
369
+ version: 0.6.2
370
+ - - "<"
371
+ - !ruby/object:Gem::Version
372
+ version: '0.7'
353
373
  description:
354
374
  email:
355
375
  - oscar@ecoportal.co.nz
@@ -537,7 +557,13 @@ files:
537
557
  - lib/eco/api/usecases/default_cases/update_details_case.rb
538
558
  - lib/eco/api/usecases/default_cases/upsert_case.rb
539
559
  - lib/eco/api/usecases/ooze_samples.rb
560
+ - lib/eco/api/usecases/ooze_samples/helpers.rb
561
+ - lib/eco/api/usecases/ooze_samples/helpers/shortcuts.rb
562
+ - lib/eco/api/usecases/ooze_samples/ooze_base_case.rb
563
+ - lib/eco/api/usecases/ooze_samples/ooze_from_doc_case.rb
564
+ - lib/eco/api/usecases/ooze_samples/ooze_run_base_case.rb
540
565
  - lib/eco/api/usecases/ooze_samples/ooze_update_case.rb
566
+ - lib/eco/api/usecases/ooze_samples/register_update_case.rb
541
567
  - lib/eco/api/usecases/use_case.rb
542
568
  - lib/eco/api/usecases/use_case_chain.rb
543
569
  - lib/eco/api/usecases/use_case_io.rb