eco-helpers 2.0.30 → 2.0.34
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +38 -2
- data/eco-helpers.gemspec +2 -1
- data/lib/eco/api/session/batch/job.rb +5 -4
- data/lib/eco/api/session/batch.rb +1 -0
- data/lib/eco/api/usecases/default_cases/upsert_case.rb +1 -1
- data/lib/eco/api/usecases/ooze_samples/helpers/shortcuts.rb +77 -0
- data/lib/eco/api/usecases/ooze_samples/helpers.rb +13 -0
- data/lib/eco/api/usecases/ooze_samples/ooze_base_case.rb +125 -0
- data/lib/eco/api/usecases/ooze_samples/ooze_from_doc_case.rb +44 -0
- data/lib/eco/api/usecases/ooze_samples/ooze_run_base_case.rb +60 -0
- data/lib/eco/api/usecases/ooze_samples/ooze_update_case.rb +33 -111
- data/lib/eco/api/usecases/ooze_samples/register_update_case.rb +104 -0
- data/lib/eco/api/usecases/ooze_samples.rb +5 -0
- data/lib/eco/version.rb +1 -1
- metadata +29 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 420f5871108edb8c66a534f60a4f8303352245a5080c68398ccd1dc34974a606
|
4
|
+
data.tar.gz: c176a43c046adf47520d18af6db7a92225786625051f2982b6ffdd1540c722e7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1e1b2828dc5ba988364eb04f9433fff624694d6371ba5e3aa80bc4d4ee3c6cc62eb90cd2693a1e324a630043e19666d1499e5e8fdfff69c03adccd777e387494
|
7
|
+
data.tar.gz: 5b2d6ba34b14b4164512e3ffd5c65ab2c4edac3c0452ff4dcd422b32537fcff47c244f85b815c4f55b0d313b546cf786dabe49d76cc5feaf374b1039cdc7c266
|
data/CHANGELOG.md
CHANGED
@@ -1,12 +1,48 @@
|
|
1
1
|
# Change Log
|
2
2
|
All notable changes to this project will be documented in this file.
|
3
3
|
|
4
|
-
## [2.0.
|
4
|
+
## [2.0.34] - 2021-08-xx
|
5
5
|
|
6
6
|
### Added
|
7
|
-
|
7
|
+
- `Eco::API::UseCases::OozeSamples::OozeBaseCase` refactored ooze base case
|
8
|
+
- `Eco::API::UseCases::OozeSamples::RegisterUpdateCase` new case for full register update
|
8
9
|
|
9
10
|
### Changed
|
11
|
+
- `Eco::API::UseCases::OozeSamples::OozeRunBaseCase` made child of `OozeBaseCase` and simplified to its essential
|
12
|
+
- `Eco::API::UseCases::OozeSamples::OozeUpdateCase` remove unnecessary code and helpers that are now inherited from `OoozeBaseCase`
|
13
|
+
- `Eco::API::UseCases::OozeSamples::OozeFromDocCase` remove unnecessary code
|
14
|
+
|
15
|
+
### Fixed
|
16
|
+
|
17
|
+
|
18
|
+
## [2.0.33] - 2021-08-16
|
19
|
+
|
20
|
+
### Added
|
21
|
+
- `Eco::API::UseCases::OozeSamples::OozeFromDocCase` use case to abstract form from doc
|
22
|
+
- `docx` dependency
|
23
|
+
- A couple more of helpers for the Ooze Cases: `#titleize` & `#normalize_string`
|
24
|
+
|
25
|
+
### Changed
|
26
|
+
- `ecoporta-api-v2` **gem** (upgrade dependency)
|
27
|
+
|
28
|
+
### Fixed
|
29
|
+
- `Eco::API::UseCases::OozeSamples::OozeUpdateCase`, helper `with_stage`
|
30
|
+
|
31
|
+
## [2.0.32] - 2021-07-14
|
32
|
+
|
33
|
+
### Fixed
|
34
|
+
- `Eco::API::UseCases::OozeSamples::OozeUpdateCase` prompting user, fixed typo
|
35
|
+
- `Eco::API::Session::Batch::Job`: `include-excluded` again... typo.
|
36
|
+
|
37
|
+
## [2.0.31] - 2021-07-13
|
38
|
+
|
39
|
+
### Fixed
|
40
|
+
- `Eco::API::Session::Batch::Job`: `include-excluded` was triggering a type error.
|
41
|
+
|
42
|
+
## [2.0.30] - 2021-07-08
|
43
|
+
|
44
|
+
### Added
|
45
|
+
* `Eco::API::Common::People::DefaultParsers::XLSParser` uses as default the `fields_map.json` to identify the headers.
|
10
46
|
|
11
47
|
### Fixed
|
12
48
|
* `Eco::API::Common::People::DefaultParsers::XLSParser` should return all `String` values
|
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.
|
34
|
+
spec.add_dependency 'ecoportal-api-v2', '>= 0.8.11', '< 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,13 +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
|
256
|
-
|
257
|
-
|
255
|
+
return full_queue unless excluded_callback = session.config.people.api_excluded
|
256
|
+
excluded = options.dig(:include, :excluded)
|
257
|
+
if excluded.is_a?(Hash) && excluded[:only]
|
258
|
+
full_queue.select {|entry| excluded_callback.call(entry, session, options, self)}
|
258
259
|
elsif options.dig(:include, :excluded)
|
259
260
|
full_queue
|
260
261
|
else
|
261
|
-
full_queue.select {|entry| !
|
262
|
+
full_queue.select {|entry| !excluded_callback.call(entry, session, options, self)}
|
262
263
|
end
|
263
264
|
end
|
264
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
|
@@ -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 << "StagePage "
|
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,125 @@
|
|
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
|
+
|
13
|
+
def main(session, options, usecase)
|
14
|
+
raise "You need to inherit from this class ('#{self.class}') and call super with a block" unless block_given?
|
15
|
+
@session = session; @options = options; @usecase = usecase
|
16
|
+
@target = nil
|
17
|
+
yield
|
18
|
+
end
|
19
|
+
|
20
|
+
# Write here your script
|
21
|
+
def process_ooze(ooze = target)
|
22
|
+
raise "You need to inherit from this class ('#{self.class}') and call super with a block" unless block_given?
|
23
|
+
yield(ooze)
|
24
|
+
update_ooze(ooze)
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def ooze(ooze_id = nil, stage_id: nil)
|
30
|
+
return target unless ooze_id
|
31
|
+
apiv2.pages.get(ooze_id, stage_id: stage_id).tap do |ooze|
|
32
|
+
if ooze
|
33
|
+
new_target(ooze)
|
34
|
+
logger.info("Got #{object_reference(ooze)}")
|
35
|
+
else
|
36
|
+
exit_error "Could not get ooze '#{ooze_id}'"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def stage(id_name = nil, ooze: target)
|
42
|
+
if ooze_id = ooze && ooze.id
|
43
|
+
exit_error "#{object_reference(ooze)} does not have stages!" unless ooze.stages?
|
44
|
+
else
|
45
|
+
exit_error "There's no target ooze to get retrieve stages from"
|
46
|
+
end
|
47
|
+
|
48
|
+
if stg = ooze.stages[id_name] || ooze.stages.get_by_name(id_name)
|
49
|
+
return ooze if ooze.respond_to?(:current_stage_id) && (ooze.current_stage_id == stg.id)
|
50
|
+
return apiv2.pages.get(ooze_id, stage_id: stg.id).tap do |stage|
|
51
|
+
if stage
|
52
|
+
new_target(stage)
|
53
|
+
logger.info("Got #{object_reference(stage)} from #{object_reference(ooze)}")
|
54
|
+
else
|
55
|
+
exit_error "Could not get stage '#{id_name}' in ooze '#{ooze_id}'"
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
exit_error "Stage '#{id_name}' doesn't exist in ooze '#{ooze_id}'"
|
60
|
+
end
|
61
|
+
|
62
|
+
def update_ooze(ooze = target)
|
63
|
+
if !options[:simulate]
|
64
|
+
apiv2.pages.update(ooze).tap do |response|
|
65
|
+
if response.success?
|
66
|
+
ooze.consolidate!
|
67
|
+
logger.info("Successful update of '#{ooze.name}' (#{ooze.id})")
|
68
|
+
else
|
69
|
+
logger.error("Could not update '#{ooze.name}' (#{ooze.id}): #{response.body}")
|
70
|
+
end
|
71
|
+
end
|
72
|
+
else
|
73
|
+
unless options.dig(:feedback, :only_stats)
|
74
|
+
if patch = (patch_doc(ooze) || {})["page"]
|
75
|
+
pp patch
|
76
|
+
end
|
77
|
+
end
|
78
|
+
backup_patch!(ooze)
|
79
|
+
exit(0) if dry_count > 5
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def new_target(object)
|
84
|
+
if target && patch_doc(target)["page"]
|
85
|
+
logger.warn "You you are switching to a new target #{object_reference(object)} after doing unsaved changes to #{object_reference(target)}"
|
86
|
+
end
|
87
|
+
@target = object
|
88
|
+
end
|
89
|
+
|
90
|
+
def backup_patch!(ooze = target)
|
91
|
+
unless patch = (patch_doc(ooze) || {})["page"]
|
92
|
+
logger.info "No changes to update for #{object_reference(ooze)}."
|
93
|
+
return
|
94
|
+
end
|
95
|
+
|
96
|
+
patch = patch_doc(ooze)
|
97
|
+
# store the request
|
98
|
+
File.open(SAVE_PATCH, "w") do |file|
|
99
|
+
#file << (patch_doc || {}).to_json
|
100
|
+
file << JSON.pretty_generate(patch || {})
|
101
|
+
end
|
102
|
+
# TODO: ooze_requests folder with subfolders: for each run
|
103
|
+
puts "Saved patch at: #{File.expand_path(SAVE_PATCH)}"
|
104
|
+
end
|
105
|
+
|
106
|
+
def patch_doc(ooze = target)
|
107
|
+
apiv2.pages.get_body(ooze)
|
108
|
+
end
|
109
|
+
|
110
|
+
def dry_count
|
111
|
+
@dry_count ||= 0
|
112
|
+
@dry_count += 1
|
113
|
+
end
|
114
|
+
|
115
|
+
def apiv2
|
116
|
+
@apiv2 ||= session.api(version: :oozes)
|
117
|
+
end
|
118
|
+
|
119
|
+
def exit_error(msg)
|
120
|
+
logger.error(msg)
|
121
|
+
exit(1)
|
122
|
+
end
|
123
|
+
|
124
|
+
|
125
|
+
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::
|
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
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
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
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
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
|
60
|
-
|
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
|
72
|
-
|
73
|
-
|
74
|
-
|
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
|
79
|
-
|
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,104 @@
|
|
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
|
+
attr_reader :batch_size
|
10
|
+
|
11
|
+
# @return [Integer] the number of pages to be processed in each batch
|
12
|
+
def batch_size
|
13
|
+
@batch_size || 25
|
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 with_each_entry
|
35
|
+
batched_search_results do |page_results|
|
36
|
+
batched_oozes = page_results.map do |page_result|
|
37
|
+
# TODO: Logics to get the correct PageStage
|
38
|
+
if ooze = new_target(ooze(page_result.id))
|
39
|
+
yield(ooze)
|
40
|
+
end
|
41
|
+
ooze
|
42
|
+
end
|
43
|
+
update_oozes(batched_oozes)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def batched_search_results
|
48
|
+
raise "Missing block. It yields in slices of #{self.class.batch_size} results" unless block_given?
|
49
|
+
results_preview
|
50
|
+
i = 0; results = []
|
51
|
+
apiv2.registers.search(register_id, search_options) do |page_result|
|
52
|
+
i += 1
|
53
|
+
results << page_result
|
54
|
+
if i <= self.class.batch_size
|
55
|
+
yield(results)
|
56
|
+
results = []
|
57
|
+
i = 0
|
58
|
+
end
|
59
|
+
end
|
60
|
+
yield(results) unless results.empty?
|
61
|
+
end
|
62
|
+
|
63
|
+
def update_oozes(batched_oozes)
|
64
|
+
batched_oozes.each do |ooze|
|
65
|
+
update_ooze(ooze)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def results_preview
|
70
|
+
apiv2.registers.search(register_id, search_options.merge(only_first: true)).tap do |search_results|
|
71
|
+
str_results = "Total target entries: #{search_results.total} (out of #{search_results.total_before_filtering})"
|
72
|
+
session.prompt_user("Do you want to proceed (y/N):", explanation: str_results, default: "N", timeout: 10) do |res|
|
73
|
+
if res.upcase.start_with?("N")
|
74
|
+
puts "..."
|
75
|
+
logger.info "Aborting script..."
|
76
|
+
exit(0)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def search_options
|
83
|
+
@search_options ||= {}.tap do |opts|
|
84
|
+
opts.merge!(sort: "created_at")
|
85
|
+
opts.merge!(dir: "asc")
|
86
|
+
opts.merge!(query: conf_filters) if conf_search
|
87
|
+
opts.merge!(filters: conf_filters)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
def conf_filters
|
92
|
+
return filters if self.respond_to?(:filters)
|
93
|
+
[]
|
94
|
+
end
|
95
|
+
|
96
|
+
def conf_search
|
97
|
+
return search if self.respond_to?(:search)
|
98
|
+
end
|
99
|
+
|
100
|
+
def register_id
|
101
|
+
options.dig(:source, :register_id)
|
102
|
+
end
|
103
|
+
|
104
|
+
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
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.
|
4
|
+
version: 2.0.34
|
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.
|
139
|
+
version: 0.8.11
|
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.
|
149
|
+
version: 0.8.11
|
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
|