eco-helpers 2.0.33 → 2.0.34

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: 84ad4121fdba090ce8585df6a999c91a0a1ba5c8fa67d52f6dc4ec5d39da18b9
4
- data.tar.gz: 96e540e97b6a54a92e246fd794821002ab015c9199d033bcb22abbc2bd729a69
3
+ metadata.gz: 420f5871108edb8c66a534f60a4f8303352245a5080c68398ccd1dc34974a606
4
+ data.tar.gz: c176a43c046adf47520d18af6db7a92225786625051f2982b6ffdd1540c722e7
5
5
  SHA512:
6
- metadata.gz: a6a3c9b2920974830e3d9ea1619f9f4e2f8652112d0b0683526888cd9d29c29c61e35afe53d94bd0927ddb4b8bd621827cdf5a8a18830143f6549f7b3d8fc37d
7
- data.tar.gz: ceaf87bc2b6190f70c91f9ad09da865ecb064bd1be9f783a75cb91f531a9918ee80a1d583ba74c21ab56ac9863eccd6d6c3321b78988961762335e80036f4ed6
6
+ metadata.gz: 1e1b2828dc5ba988364eb04f9433fff624694d6371ba5e3aa80bc4d4ee3c6cc62eb90cd2693a1e324a630043e19666d1499e5e8fdfff69c03adccd777e387494
7
+ data.tar.gz: 5b2d6ba34b14b4164512e3ffd5c65ab2c4edac3c0452ff4dcd422b32537fcff47c244f85b815c4f55b0d313b546cf786dabe49d76cc5feaf374b1039cdc7c266
data/CHANGELOG.md CHANGED
@@ -1,7 +1,21 @@
1
1
  # Change Log
2
2
  All notable changes to this project will be documented in this file.
3
3
 
4
- ## [2.0.33] - 2021-08-xx
4
+ ## [2.0.34] - 2021-08-xx
5
+
6
+ ### Added
7
+ - `Eco::API::UseCases::OozeSamples::OozeBaseCase` refactored ooze base case
8
+ - `Eco::API::UseCases::OozeSamples::RegisterUpdateCase` new case for full register update
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
5
19
 
6
20
  ### Added
7
21
  - `Eco::API::UseCases::OozeSamples::OozeFromDocCase` use case to abstract form from doc
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.9', '< 0.9'
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'
@@ -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
@@ -54,6 +54,21 @@ module Eco
54
54
  end
55
55
  end
56
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
+
57
72
  end
58
73
  end
59
74
  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
@@ -4,13 +4,6 @@ class Eco::API::UseCases::OozeSamples::OozeFromDocCase < Eco::API::UseCases::Ooz
4
4
  name "ooze-forms-case"
5
5
  type :other
6
6
 
7
- def main(session, options, usecase)
8
- raise "You need to inherit from this class and call super with a block" unless block_given?
9
- super(session, options, usecase) do
10
- yield
11
- end
12
- end
13
-
14
7
  private
15
8
 
16
9
  def with_column(num = 0)
@@ -1,4 +1,5 @@
1
- class Eco::API::UseCases::OozeSamples::OozeRunBaseCase < Eco::API::Common::Loaders::UseCase
1
+ # Base class to run a script against a single ooze.
2
+ class Eco::API::UseCases::OozeSamples::OozeRunBaseCase < Eco::API::UseCases::OozeSamples::OozeBaseCase
2
3
  name "ooze-run-base"
3
4
  type :other
4
5
 
@@ -6,20 +7,28 @@ class Eco::API::UseCases::OozeSamples::OozeRunBaseCase < Eco::API::Common::Loade
6
7
 
7
8
  SAVE_PATCH = "ooze_patch_update.json"
8
9
 
9
- def main(session, options, usecase)
10
- raise "You need to inherit from this class and call super with a block" unless block_given?
10
+ def main(session, options, usecase, &block)
11
+ raise "You need to inherit from this class and call super with a block" unless block
11
12
  @session = session; @options = options; @usecase = usecase
12
- yield
13
- end_script!
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
14
22
  end
15
23
 
16
24
  private
17
25
 
18
- def end_script!
19
- exit_if_no_changes!
20
- backup_patch!
21
- launch_request unless options[:simulate]
22
- exit(0)
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)
23
32
  end
24
33
 
25
34
  def ooze_id
@@ -30,54 +39,9 @@ class Eco::API::UseCases::OozeSamples::OozeRunBaseCase < Eco::API::Common::Loade
30
39
  options.dig(:source, :stage_id)
31
40
  end
32
41
 
33
- def target
34
- @target ||= ooze
35
- end
36
-
37
- def new_target(object)
38
- if @target && patch_doc["page"]
39
- logger.warn "You you are switching to a new target #{object_reference(object)} after doing unsaved changes to #{object_reference(@target)}"
40
- end
41
- @target = object
42
- end
43
-
44
- def stage(id_name = stage_id)
45
- if stg = ooze.stages[id_name] || ooze.stages.get_by_name(id_name)
46
- return apiv2.pages.get(ooze_id, stage_id: stg.id).tap do |stage|
47
- if stage
48
- new_target(stage)
49
- logger.info("Got #{object_reference(stage)} from #{object_reference(ooze)}")
50
- else
51
- logger.error("Could not get stage '#{id_name}' in ooze '#{ooze_id}'")
52
- exit(1)
53
- end
54
- end
55
- end
56
- raise "Stage '#{id_name}' doesn't exist in ooze '#{ooze_id}'"
57
- end
58
-
59
- def ooze
60
- @ooze ||= apiv2.pages.get(ooze_id, stage_id: stage_id).tap do |ooze|
61
- if ooze
62
- new_target(ooze)
63
- logger.info("Got #{object_reference(ooze)}")
64
- else
65
- logger.error("Could not get ooze '#{ooze_id}'")
66
- exit(1)
67
- end
68
- end
69
- end
70
-
71
- def launch_request
42
+ def update_ooze
72
43
  prompt_to_confirm!
73
-
74
- apiv2.pages.update(target).tap do |response|
75
- if response.success?
76
- logger.info("All went OK")
77
- else
78
- logger.error(response.body)
79
- end
80
- end
44
+ super
81
45
  end
82
46
 
83
47
  def exit_if_no_changes!
@@ -87,46 +51,10 @@ class Eco::API::UseCases::OozeSamples::OozeRunBaseCase < Eco::API::Common::Loade
87
51
  end
88
52
  end
89
53
 
90
- def apiv2
91
- @apiv2 ||= session.api(version: :oozes)
92
- end
93
-
94
- def patch_doc
95
- apiv2.pages.get_body(target)
96
- end
97
-
98
- def backup_patch!
99
- # store the request
100
- File.open(SAVE_PATCH, "w") do |file|
101
- #file << (patch_doc || {}).to_json
102
- file << JSON.pretty_generate(patch_doc || {})
103
- end
104
- puts "Saved patch at: #{File.expand_path(SAVE_PATCH)}"
105
- end
106
-
107
- def object_reference(obj)
108
- return "No reference" unless obj
109
- "".tap do |ref|
110
- case obj
111
- when Ecoportal::API::V2::Page::Stage
112
- ref << "Stage "
113
- when Ecoportal::API::V2::Pages::PageStage
114
- ref << "StagePage "
115
- when Ecoportal::API::V2::Page
116
- ref << "Page "
117
- end
118
- ref << "'#{obj.name}' " if obj.respond_to?(:name)
119
- end
120
- end
121
-
122
54
  def prompt_to_confirm!
123
55
  micro.prompt_user("Do you want to proceed (y/N)?", default: "Y") do |response|
124
56
  exit(1) unless response.upcase.start_with?("Y")
125
57
  end
126
58
  end
127
59
 
128
- def logger
129
- session.logger
130
- end
131
-
132
60
  end
@@ -2,15 +2,6 @@ class Eco::API::UseCases::OozeSamples::OozeUpdateCase < Eco::API::UseCases::Ooze
2
2
  name "single-ooze-case"
3
3
  type :other
4
4
 
5
- include Eco::API::UseCases::OozeSamples::Helpers
6
-
7
- def main(session, options, usecase)
8
- raise "You need to inherit from this class and call super with a block" unless block_given?
9
- super(session, options, usecase) do
10
- yield
11
- end
12
- end
13
-
14
5
  private
15
6
 
16
7
  def with_fields(type: nil, label: nil)
@@ -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
@@ -9,6 +9,8 @@ module Eco
9
9
  end
10
10
 
11
11
  require_relative 'ooze_samples/helpers'
12
+ require_relative 'ooze_samples/ooze_base_case'
12
13
  require_relative 'ooze_samples/ooze_run_base_case'
13
14
  require_relative 'ooze_samples/ooze_update_case'
14
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.33"
2
+ VERSION = "2.0.34"
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.33
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.9
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.9
149
+ version: 0.8.11
150
150
  - - "<"
151
151
  - !ruby/object:Gem::Version
152
152
  version: '0.9'
@@ -559,9 +559,11 @@ files:
559
559
  - lib/eco/api/usecases/ooze_samples.rb
560
560
  - lib/eco/api/usecases/ooze_samples/helpers.rb
561
561
  - lib/eco/api/usecases/ooze_samples/helpers/shortcuts.rb
562
+ - lib/eco/api/usecases/ooze_samples/ooze_base_case.rb
562
563
  - lib/eco/api/usecases/ooze_samples/ooze_from_doc_case.rb
563
564
  - lib/eco/api/usecases/ooze_samples/ooze_run_base_case.rb
564
565
  - lib/eco/api/usecases/ooze_samples/ooze_update_case.rb
566
+ - lib/eco/api/usecases/ooze_samples/register_update_case.rb
565
567
  - lib/eco/api/usecases/use_case.rb
566
568
  - lib/eco/api/usecases/use_case_chain.rb
567
569
  - lib/eco/api/usecases/use_case_io.rb