eco-helpers 2.0.32 → 2.0.33

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: 0a1f04caab2cae362249d46b694021287a8904052f3851151b4cb6103e7f8790
4
- data.tar.gz: b050e7dd4626f331080cb6dc1437da74a793460bce5426ff3aa5a9b91bba5a53
3
+ metadata.gz: 84ad4121fdba090ce8585df6a999c91a0a1ba5c8fa67d52f6dc4ec5d39da18b9
4
+ data.tar.gz: 96e540e97b6a54a92e246fd794821002ab015c9199d033bcb22abbc2bd729a69
5
5
  SHA512:
6
- metadata.gz: 7eb83d0cd4b318f42e90065f772b0b9aa801afddd39d0f2e6fa9861446fd06d537cc436500f8cc9a2c852ce46d3ea2b00a2d3a7199cdba9bf244a8104aa63bfc
7
- data.tar.gz: ef8ab06e274156c0341212b9b72865e0dfe4e55e9c4f0f816b67554739b6fc2a1b15614a973bfae234dbdd03de7b429513ad19edcd9264b0123cb80374dc78b8
6
+ metadata.gz: a6a3c9b2920974830e3d9ea1619f9f4e2f8652112d0b0683526888cd9d29c29c61e35afe53d94bd0927ddb4b8bd621827cdf5a8a18830143f6549f7b3d8fc37d
7
+ data.tar.gz: ceaf87bc2b6190f70c91f9ad09da865ecb064bd1be9f783a75cb91f531a9918ee80a1d583ba74c21ab56ac9863eccd6d6c3321b78988961762335e80036f4ed6
data/CHANGELOG.md CHANGED
@@ -1,11 +1,20 @@
1
1
  # Change Log
2
2
  All notable changes to this project will be documented in this file.
3
3
 
4
- ## [2.0.32] - 2021-07-xx
4
+ ## [2.0.33] - 2021-08-xx
5
5
 
6
6
  ### Added
7
+ - `Eco::API::UseCases::OozeSamples::OozeFromDocCase` use case to abstract form from doc
8
+ - `docx` dependency
9
+ - A couple more of helpers for the Ooze Cases: `#titleize` & `#normalize_string`
7
10
 
8
11
  ### Changed
12
+ - `ecoporta-api-v2` **gem** (upgrade dependency)
13
+
14
+ ### Fixed
15
+ - `Eco::API::UseCases::OozeSamples::OozeUpdateCase`, helper `with_stage`
16
+
17
+ ## [2.0.32] - 2021-07-14
9
18
 
10
19
  ### Fixed
11
20
  - `Eco::API::UseCases::OozeSamples::OozeUpdateCase` prompting user, fixed typo
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.9', '< 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
@@ -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
@@ -8,4 +8,7 @@ module Eco
8
8
  end
9
9
  end
10
10
 
11
+ require_relative 'ooze_samples/helpers'
12
+ require_relative 'ooze_samples/ooze_run_base_case'
11
13
  require_relative 'ooze_samples/ooze_update_case'
14
+ require_relative 'ooze_samples/ooze_from_doc_case'
@@ -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,62 @@
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
+ end
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,51 @@
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
+ 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
+ private
15
+
16
+ def with_column(num = 0)
17
+ with_tables do |table, i|
18
+ raise "Column num (#{num}) is to big. Table '#{i}' only has #{table.column_count} columns." if table.column_count < num
19
+ table.columns[num].cells.each_with_index do |cell_row, j|
20
+ txt = normalize_string(cell_row.text)
21
+ yield(txt, i, j, table, cell_row)
22
+ end
23
+ end
24
+ end
25
+
26
+ def with_tables
27
+ raise "There are no tables in the doc" unless table_count > 0
28
+ i = 0
29
+ doc.tables.each do |table|
30
+ yield(table, i) if block_given?
31
+ i += 1
32
+ end
33
+ end
34
+
35
+ def table_count
36
+ doc.tables.count
37
+ end
38
+
39
+ def tables?
40
+ table_count > 0
41
+ end
42
+
43
+ def doc
44
+ @doc ||= Docx::Document.open(input_file)
45
+ end
46
+
47
+ def input_file
48
+ options.dig(:source, :file)
49
+ end
50
+
51
+ end
@@ -0,0 +1,132 @@
1
+ class Eco::API::UseCases::OozeSamples::OozeRunBaseCase < Eco::API::Common::Loaders::UseCase
2
+ name "ooze-run-base"
3
+ type :other
4
+
5
+ attr_reader :session, :options, :usecase
6
+
7
+ SAVE_PATCH = "ooze_patch_update.json"
8
+
9
+ def main(session, options, usecase)
10
+ raise "You need to inherit from this class and call super with a block" unless block_given?
11
+ @session = session; @options = options; @usecase = usecase
12
+ yield
13
+ end_script!
14
+ end
15
+
16
+ private
17
+
18
+ def end_script!
19
+ exit_if_no_changes!
20
+ backup_patch!
21
+ launch_request unless options[:simulate]
22
+ exit(0)
23
+ end
24
+
25
+ def ooze_id
26
+ options.dig(:source, :ooze_id)
27
+ end
28
+
29
+ def stage_id
30
+ options.dig(:source, :stage_id)
31
+ end
32
+
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
72
+ 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
81
+ end
82
+
83
+ def exit_if_no_changes!
84
+ unless changes = !!patch_doc["page"]
85
+ logger.warn "No Changes!!"
86
+ exit(0)
87
+ end
88
+ end
89
+
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
+ def prompt_to_confirm!
123
+ micro.prompt_user("Do you want to proceed (y/N)?", default: "Y") do |response|
124
+ exit(1) unless response.upcase.start_with?("Y")
125
+ end
126
+ end
127
+
128
+ def logger
129
+ session.logger
130
+ end
131
+
132
+ end
@@ -1,132 +1,63 @@
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"
5
+ include Eco::API::UseCases::OozeSamples::Helpers
8
6
 
9
7
  def main(session, options, usecase)
10
- @session = session; @options = options; @usecase = usecase
11
- yield
12
- end_script!
13
- end
14
-
15
- private
16
-
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
43
- end
44
- raise "Stage '#{id_name}' doesn't exist in ooze '#{ooze_id}'"
45
- end
46
-
47
- def ooze
48
- @ooze ||= apiv2.pages.get(ooze_id, stage_id: stage_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
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
56
11
  end
57
12
  end
58
13
 
59
- def launch_request
60
- prompt_to_confirm!
14
+ private
61
15
 
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
16
+ def with_fields(type: nil, label: nil)
17
+ flds = target.components
18
+ flds = flds.get_by_type(type) if type
19
+ flds = flds.select do |fld|
20
+ value = (label == :unnamed) ? nil : label
21
+ !label || same_string?(fld.label, value)
22
+ end.each do |field|
23
+ yield(field) if block_given?
68
24
  end
69
25
  end
70
26
 
71
- def exit_if_no_changes!
72
- unless changes = !!patch_doc["page"]
73
- logger.warn "No Changes!!"
74
- exit(0)
27
+ def with_sections(type: nil, heading: nil)
28
+ secs = target.sections
29
+ secs = secs.get_by_type(type) if type
30
+ secs = secs.select do |sec|
31
+ value = (heading == :unnamed) ? nil : heading
32
+ !heading || same_string?(sec.heading, value)
33
+ end.each do |sec|
34
+ yield(sec) if block_given?
75
35
  end
76
36
  end
77
37
 
78
- def apiv2
79
- @apiv2 ||= session.api(version: :oozes)
80
- end
81
-
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)}"
38
+ def with_stage(name:)
39
+ if stage = target.stages.get_by_name(name)
40
+ yield(stage) if block_given?
89
41
  end
90
- @target = object
42
+ stage
91
43
  end
92
44
 
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)
45
+ def add_field(name, type, section, after: nil, side: :left)
46
+ raise "You need to specify a section for a new field. Given: #{section.class}" unless section.is_a?(Ecoportal::API::V2::Page::Section)
47
+ target.components.add(label: name, type: type) do |field|
48
+ section.add_component(field, after: after, side: side)
49
+ end.tap do |field|
50
+ yield(field) if block_given?
105
51
  end
106
52
  end
107
53
 
108
-
109
- def patch_doc
110
- apiv2.pages.get_body(target)
54
+ def to_field(value)
55
+ fld = nil
56
+ fld ||= value if value.is_a?(Ecoportal::API::V2::Page::Component)
57
+ fld ||= target.components.get_by_id(value)
58
+ fld ||= target.components.select {|fld| same_name?(fld.label, value)}
59
+ #fld ||= value.reference if value.is_a?(Ecoportal::API::V2::Page::Force::Binding)
111
60
  end
112
61
 
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
- micro.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
62
 
132
63
  end
data/lib/eco/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Eco
2
- VERSION = "2.0.32"
2
+ VERSION = "2.0.33"
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.32
4
+ version: 2.0.33
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.9
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.9
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,6 +557,10 @@ 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_from_doc_case.rb
563
+ - lib/eco/api/usecases/ooze_samples/ooze_run_base_case.rb
540
564
  - lib/eco/api/usecases/ooze_samples/ooze_update_case.rb
541
565
  - lib/eco/api/usecases/use_case.rb
542
566
  - lib/eco/api/usecases/use_case_chain.rb