eco-helpers 2.4.9 → 2.5.1

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.
Files changed (44) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +24 -1
  3. data/eco-helpers.gemspec +1 -1
  4. data/lib/eco/api/common/loaders/case_base.rb +2 -3
  5. data/lib/eco/api/common/people/default_parsers/csv_parser.rb +97 -47
  6. data/lib/eco/api/common/people/default_parsers/select_parser.rb +2 -2
  7. data/lib/eco/api/common/people/default_parsers/xls_parser.rb +0 -1
  8. data/lib/eco/api/common/people/entry_factory.rb +13 -8
  9. data/lib/eco/api/common/people/person_entry_attribute_mapper.rb +2 -2
  10. data/lib/eco/api/common/session/base_session.rb +1 -2
  11. data/lib/eco/api/common/session/environment.rb +6 -10
  12. data/lib/eco/api/common/session/helpers/prompt_user.rb +18 -18
  13. data/lib/eco/api/common/session/mailer.rb +1 -3
  14. data/lib/eco/api/common/session/s3_uploader.rb +1 -3
  15. data/lib/eco/api/common/session/sftp.rb +6 -4
  16. data/lib/eco/api/common/version_patches/ecoportal_api/external_person.rb +5 -4
  17. data/lib/eco/api/session/config/api.rb +4 -2
  18. data/lib/eco/api/usecases/base_case.rb +0 -2
  19. data/lib/eco/api/usecases/base_io.rb +0 -3
  20. data/lib/eco/api/usecases/default_cases/samples/sftp_case.rb +1 -1
  21. data/lib/eco/api/usecases/ooze_samples/ooze_from_doc_case.rb +1 -2
  22. data/lib/eco/api/usecases/ooze_samples/ooze_run_base_case.rb +0 -1
  23. data/lib/eco/api/usecases/ooze_samples/ooze_update_case.rb +0 -1
  24. data/lib/eco/api/usecases/ooze_samples/register_export_case.rb +1 -1
  25. data/lib/eco/api/usecases/ooze_samples/register_update_case.rb +0 -2
  26. data/lib/eco/api/usecases/ooze_samples/target_oozes_update_case.rb +0 -1
  27. data/lib/eco/api/usecases/ooze_samples.rb +0 -1
  28. data/lib/eco/api/usecases/use_case.rb +4 -1
  29. data/lib/eco/api/usecases/use_case_chain.rb +1 -4
  30. data/lib/eco/api/usecases/use_case_io.rb +0 -2
  31. data/lib/eco/api.rb +0 -2
  32. data/lib/eco/cli/config/default/options.rb +11 -1
  33. data/lib/eco/csv.rb +4 -7
  34. data/lib/eco/data/files/directory.rb +0 -3
  35. data/lib/eco/data/files/encoding.rb +75 -0
  36. data/lib/eco/data/files/helpers.rb +15 -37
  37. data/lib/eco/data/files.rb +1 -0
  38. data/lib/eco/data/fuzzy_match.rb +8 -4
  39. data/lib/eco/data.rb +0 -1
  40. data/lib/eco/version.rb +1 -1
  41. data/lib/eco-helpers.rb +1 -1
  42. metadata +5 -6
  43. data/lib/eco/data/crypto/encryption.rb +0 -321
  44. data/lib/eco/data/crypto.rb +0 -7
@@ -1,4 +1,3 @@
1
- require 'docx'
2
1
  # Use case to abstract FORM from word document
3
2
  class Eco::API::UseCases::OozeSamples::OozeFromDocCase < Eco::API::UseCases::OozeSamples::OozeUpdateCase
4
3
  name "ooze-forms-case"
@@ -34,11 +33,11 @@ class Eco::API::UseCases::OozeSamples::OozeFromDocCase < Eco::API::UseCases::Ooz
34
33
  end
35
34
 
36
35
  def doc
36
+ require 'docx'
37
37
  @doc ||= Docx::Document.open(input_file)
38
38
  end
39
39
 
40
40
  def input_file
41
41
  options.dig(:source, :file)
42
42
  end
43
-
44
43
  end
@@ -58,5 +58,4 @@ class Eco::API::UseCases::OozeSamples::OozeRunBaseCase < Eco::API::UseCases::Ooz
58
58
  exit(1) unless response.upcase.start_with?("Y")
59
59
  end
60
60
  end
61
-
62
61
  end
@@ -11,5 +11,4 @@ class Eco::API::UseCases::OozeSamples::OozeUpdateCase < Eco::API::UseCases::Ooze
11
11
  fld ||= target.components.select {|fld| same_name?(fld.label, value)}
12
12
  #fld ||= value.reference if value.is_a?(Ecoportal::API::V2::Page::Force::Binding)
13
13
  end
14
-
15
14
  end
@@ -4,7 +4,6 @@
4
4
  # - You need to define the `process_ooze` method
5
5
  # This case expects `options[:source][:register_id]`
6
6
  class Eco::API::UseCases::OozeSamples::RegisterExportCase < Eco::API::Common::Loaders::UseCase
7
-
8
7
  class << self
9
8
  # @return [Integer] the number of pages to be processed in each batch
10
9
  def batch_size(size = nil)
@@ -27,6 +26,7 @@ class Eco::API::UseCases::OozeSamples::RegisterExportCase < Eco::API::Common::Lo
27
26
  @session = session; @options = options; @usecase = usecase
28
27
  @target = nil
29
28
  raise "You need to inherit from this class ('#{self.class}') and call super with a block" unless block_given?
29
+
30
30
  with_each_entry do |ooze|
31
31
  process_ooze(ooze)
32
32
  end
@@ -4,7 +4,6 @@
4
4
  # - You need to define the `process_ooze` method
5
5
  # This case expects `options[:source][:register_id]`
6
6
  class Eco::API::UseCases::OozeSamples::RegisterUpdateCase < Eco::API::UseCases::OozeSamples::OozeBaseCase
7
-
8
7
  class << self
9
8
  # @return [Integer] the number of pages to be processed in each batch
10
9
  def batch_size(size = nil)
@@ -236,5 +235,4 @@ class Eco::API::UseCases::OozeSamples::RegisterUpdateCase < Eco::API::UseCases::
236
235
  def register_id
237
236
  options.dig(:source, :register_id)
238
237
  end
239
-
240
238
  end
@@ -60,5 +60,4 @@ class Eco::API::UseCases::OozeSamples::TargetOozesUpdateCase < Eco::API::UseCase
60
60
  def input_csv
61
61
  @input_csv ||= Eco::CSV.read(options.dig(:source, :file))
62
62
  end
63
-
64
63
  end
@@ -2,7 +2,6 @@ module Eco
2
2
  module API
3
3
  class UseCases
4
4
  class OozeSamples
5
-
6
5
  end
7
6
  end
8
7
  end
@@ -73,13 +73,16 @@ module Eco
73
73
  # via an children class of `Eco::API::Common::Loaders::Base`
74
74
  def set_session_n_options(session:, options: @options)
75
75
  return false unless callback_from_loader?
76
+ use_case_self = self
76
77
  callback_self.instance_eval do
78
+ next unless self.is_a?(Eco::API::Common::Loaders::CaseBase)
77
79
  # `self` is the use case itself (when used the Loader)
78
80
  @session = session
79
81
  @options = options
82
+ @usecase = use_case_self
80
83
  end
81
84
  true
82
- end
85
+ end
83
86
  end
84
87
  end
85
88
  end
@@ -3,8 +3,7 @@ module Eco
3
3
  class UseCases
4
4
  # Class that enables to chain multiple UseCase
5
5
  class UseCaseChain < UseCase
6
- @types = UseCase.types
7
-
6
+ @types = UseCase.types
8
7
  MAX_CHAINS = 70
9
8
  @@num_chains = 0
10
9
 
@@ -86,9 +85,7 @@ module Eco
86
85
  usecase
87
86
  end
88
87
  end
89
-
90
88
  end
91
-
92
89
  end
93
90
  end
94
91
  end
@@ -1,7 +1,6 @@
1
1
  module Eco
2
2
  module API
3
3
  class UseCases
4
-
5
4
  # InputOutput class for usecases.
6
5
  # @note Same as Eco::API::UseCases::BaseIO but:
7
6
  # - includes `type` of usecase
@@ -76,7 +75,6 @@ module Eco
76
75
  self.class.new(**kargs)
77
76
  end
78
77
  end
79
-
80
78
  end
81
79
  end
82
80
  end
data/lib/eco/api.rb CHANGED
@@ -1,6 +1,4 @@
1
1
  require 'ecoportal/api'
2
- require 'ecoportal/api-v2'
3
- require 'ecoportal/api-graphql'
4
2
 
5
3
  module Eco
6
4
  module API
@@ -42,7 +42,17 @@ ASSETS.cli.config do |cnf|
42
42
 
43
43
  desc = "Skips the check of the headers"
44
44
  options_set.add("-skip-header-check", desc) do |options, session|
45
- options.deep_merge!(input: {skip_header_check: true})
45
+ options.deep_merge!(input: {header_check: {skip: true}})
46
+ end
47
+
48
+ desc = "It requires the order of the headers to be as expected (exact match required)"
49
+ options_set.add("-header-order-check", desc) do |options, session|
50
+ options.deep_merge!(input: {header_check: {order: true}})
51
+ end
52
+
53
+ desc = "It raises an exception if any header issues are detected"
54
+ options_set.add("-require-valid-header", desc) do |options, session|
55
+ options.deep_merge!(input: {header_check: {must_be_valid: true}})
46
56
  end
47
57
 
48
58
  desc = "Fix the current session to work with this schema"
data/lib/eco/csv.rb CHANGED
@@ -1,9 +1,8 @@
1
1
  require 'csv'
2
-
3
2
  module Eco
4
3
  class CSV < ::CSV
5
-
6
4
  class << self
5
+ include Eco::Data::Files
7
6
 
8
7
  # @return [Eco::CSV::Table]
9
8
  def parse(data, **kargs, &block)
@@ -13,12 +12,10 @@ module Eco
13
12
 
14
13
  # @return [Eco::CSV::Table]
15
14
  def read(file, **kargs)
16
- kargs = {headers: true, skip_blanks: true}.merge(kargs)
17
- args = [file].tap do |arg|
18
- encoding = Eco::API::Common::Session::FileManager.encoding(file)
19
- arg.push("rb:bom|utf-8") if encoding == "bom"
15
+ params = {}.tap do |prms|
16
+ prms.merge!(encoding: kargs.delete(:encoding)) if kargs.key?(:encoding)
20
17
  end
21
- Eco::CSV::Table.new(super(*args, **kargs))
18
+ parse(get_file_content(file, **params), **kargs)
22
19
  end
23
20
  end
24
21
  end
@@ -1,10 +1,7 @@
1
- #require 'fileutils'
2
-
3
1
  module Eco
4
2
  module Data
5
3
  module Files
6
4
  class Directory
7
-
8
5
  class << self
9
6
  def create(path, includes_file: false)
10
7
  return true if Files.file_exists?(path)
@@ -0,0 +1,75 @@
1
+ module Eco
2
+ module Data
3
+ module Files
4
+ module Encoding
5
+ include Eco::Language::AuxiliarLogger
6
+
7
+ BOM_BYTES = [239, 187, 191]
8
+
9
+ def has_bom?(path)
10
+ return false if !path || file_empty?(path)
11
+ File.open(path, "rb") do |f|
12
+ bytes = f.read(3)
13
+ return bytes.unpack('C*') == BOM_BYTES
14
+ end
15
+ end
16
+
17
+ def get_file_content_with_encoding(file, encoding: nil)
18
+ encoding ||= scoped_encoding(file)
19
+ unless !encoding || encoding == 'utf-8'
20
+ msg = "File encoding: '#{encoding}'"
21
+ logger.debug(msg)
22
+ puts msg
23
+ end
24
+
25
+ bom_enc = encoding && encoding.split('|')[0] == 'bom'
26
+ if has_bom?(file) || bom_enc
27
+ content = remove_bom(File.read(file, encoding: 'utf-8'))
28
+ encoding = 'utf-8'
29
+ else
30
+ content = File.read(file, encoding: encoding)
31
+ end
32
+ return nil unless content
33
+ content = content.encode("utf-8") unless encoding.include?('utf-8')
34
+ content
35
+ end
36
+
37
+ # Changes encoding from bom to utf8
38
+ # https://stackoverflow.com/a/24916365/4352306
39
+ def remove_bom(content)
40
+ if content.bytes[0..2] == BOM_BYTES
41
+ bom = BOM_BYTES.pack('C*').force_encoding('utf-8').encode('utf-8')
42
+ content = content.sub(bom, '')
43
+ content.force_encoding('utf-8')
44
+ end
45
+ content
46
+ end
47
+
48
+ def encoding(path)
49
+ has_bom?(path) ? "bom" : "utf-8"
50
+ end
51
+
52
+ # Gives the parameter as it should
53
+ def scoped_encoding(path)
54
+ unless file_exists?(path)
55
+ logger.error("File does not exist: #{path}")
56
+ return nil
57
+ end
58
+ encoding ||= encoding(path)
59
+ encoding = (encoding == "bom") ? "#{encoding}|utf-8": encoding
60
+ encoding
61
+ end
62
+
63
+ def file_exists?(file)
64
+ return false if !file
65
+ return File.exists?(file) || File.exists?(File.expand_path(file))
66
+ end
67
+
68
+ def file_empty?(path)
69
+ return true if !File.file?(path)
70
+ File.zero?(path)
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
@@ -12,44 +12,34 @@ module Eco
12
12
 
13
13
  module InstanceMethods
14
14
  include Eco::Language::AuxiliarLogger
15
+ include Eco::Data::Files::Encoding
15
16
 
16
17
  # It offers a resilient way to read content from a file
17
18
  # @tolerance [Integer] the number of allowed encoding errors.
18
19
  # @return [String] the content of the file
19
- def get_file_content(file, encoding, tolerance: 5)
20
- unless self.class.file_exists?(file)
21
- logger.error("File does not exist: #{file}")
22
- exit(1)
23
- end
24
- encoding ||= self.class.encoding(file)
25
- encoding = (encoding == "bom") ? "#{encoding}|utf-8": encoding
26
- unless !encoding || encoding == 'utf-8'
27
- msg = "File encoding: '#{encoding}'"
28
- logger.debug(msg)
29
- puts msg
30
- end
20
+ def get_file_content(file, encoding: nil, tolerance: 5)
31
21
  read_with_tolerance(file, encoding: encoding, tolerance: tolerance)
32
22
  end
33
23
 
34
24
  def read_with_tolerance(file, encoding:, tolerance: 5)
35
- if content = File.read(file, encoding: encoding)
36
- content = content.encode("utf-8") unless encoding.include?('utf-8')
37
- content.scrub do |bytes|
38
- replacement = '<' + bytes.unpack('H*')[0] + '>'
39
- if tolerance <= 0
40
- logger.error("There were more than 5 encoding errors in the file '#{file}'.")
41
- return content
42
- else
43
- tolerance -= 1
44
- logger.error("Encoding problem in file '#{file}': '#{replacement}'.")
45
- replacement
46
- end
25
+ return nil unless content = get_file_content_with_encoding(file, encoding: encoding)
26
+ content.scrub do |bytes|
27
+ replacement = '<' + bytes.unpack('H*')[0] + '>'
28
+ if tolerance <= 0
29
+ logger.error("There were more than 5 encoding errors in the file '#{file}'.")
30
+ return content
31
+ else
32
+ tolerance -= 1
33
+ logger.error("Encoding problem in file '#{file}': '#{replacement}'.")
34
+ replacement
47
35
  end
48
36
  end
49
37
  end
50
38
  end
51
39
 
52
40
  module ClassMethods
41
+ include Eco::Data::Files::Encoding
42
+
53
43
  def create_directory(path, includes_file: false)
54
44
  Directory.create(path, includes_file: includes_file)
55
45
  end
@@ -104,18 +94,6 @@ module Eco
104
94
  File.zero?(path)
105
95
  end
106
96
 
107
- def has_bom?(path)
108
- return false if !path || file_empty?(path)
109
- File.open(path, "rb") do |f|
110
- bytes = f.read(3)
111
- return bytes.unpack("C*") == [239, 187, 191]
112
- end
113
- end
114
-
115
- def encoding(path)
116
- has_bom?(path) ? "bom" : "utf-8"
117
- end
118
-
119
97
  def script_subfolder
120
98
  basename = File.basename($0, File.extname($0))
121
99
  path = File.dirname($0)
@@ -137,7 +115,7 @@ module Eco
137
115
 
138
116
  def csv_files(folder = ".", regexp: nil, older_than: nil)
139
117
  folder_files(folder, "*.csv", regexp: regexp, older_than: older_than).sort
140
- end
118
+ end
141
119
  end
142
120
 
143
121
  class << self
@@ -5,6 +5,7 @@ module Eco
5
5
  end
6
6
  end
7
7
 
8
+ require_relative 'files/encoding'
8
9
  require_relative 'files/helpers'
9
10
  require_relative 'files/file_pattern'
10
11
  require_relative 'files/directory'
@@ -1,7 +1,3 @@
1
- require 'fuzzy_match'
2
- require 'amatch'
3
- require 'jaro_winkler'
4
-
5
1
  require_relative 'fuzzy_match/stop_words'
6
2
  require_relative 'fuzzy_match/array_helpers'
7
3
  require_relative 'fuzzy_match/string_helpers'
@@ -33,6 +29,8 @@ module Eco
33
29
  ignore_case: true,
34
30
  weight: 0.25
35
31
  }.merge(options)
32
+
33
+ require 'jaro_winkler'
36
34
  JaroWinkler.distance(str1, str2, **options)
37
35
  end
38
36
 
@@ -63,7 +61,10 @@ module Eco
63
61
  return @fuzzy_match if fuzzy_match_options == fuzzy_match_options(options)
64
62
  end
65
63
  @fuzzy_options = options
64
+
66
65
  # make it run with a native C extension (for better performance: ~130 % increase of performance)
66
+ require 'fuzzy_match'
67
+ require 'amatch'
67
68
  ::FuzzyMatch.engine = :amatch
68
69
  @fuzzy_match = ::FuzzyMatch.new(haystack(haystack_data), fuzzy_match_options)
69
70
  end
@@ -115,7 +116,10 @@ module Eco
115
116
  end
116
117
  end
117
118
 
119
+ require 'fuzzy_match'
120
+ require 'amatch'
118
121
  res = ::FuzzyMatch.score_class.new(nstr, istr) unless dice && lev
122
+
119
123
  dice ||= res&.dices_coefficient_similar || 0
120
124
  lev ||= res&.levenshtein_similar || 0
121
125
  jaro_res ||= jaro(nstr, istr)
data/lib/eco/data.rb CHANGED
@@ -3,7 +3,6 @@ module Eco
3
3
  end
4
4
  end
5
5
 
6
- require_relative 'data/crypto'
7
6
  require_relative 'data/files'
8
7
  require_relative 'data/mapper'
9
8
  require_relative 'data/fuzzy_match'
data/lib/eco/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Eco
2
- VERSION = "2.4.9"
2
+ VERSION = "2.5.1"
3
3
  end
data/lib/eco-helpers.rb CHANGED
@@ -8,10 +8,10 @@ require 'dotenv/load'
8
8
  module Eco
9
9
  end
10
10
 
11
- require_relative 'eco/csv'
12
11
  require_relative 'eco/language'
13
12
  require_relative 'eco/common'
14
13
  require_relative 'eco/data'
14
+ require_relative 'eco/csv'
15
15
  require_relative 'eco/api'
16
16
  require_relative 'eco/cli'
17
17
  require_relative 'eco/assets'
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.4.9
4
+ version: 2.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oscar Segura
@@ -156,7 +156,7 @@ dependencies:
156
156
  requirements:
157
157
  - - ">="
158
158
  - !ruby/object:Gem::Version
159
- version: 0.3.6
159
+ version: 0.3.7
160
160
  - - "<"
161
161
  - !ruby/object:Gem::Version
162
162
  version: '0.4'
@@ -166,7 +166,7 @@ dependencies:
166
166
  requirements:
167
167
  - - ">="
168
168
  - !ruby/object:Gem::Version
169
- version: 0.3.6
169
+ version: 0.3.7
170
170
  - - "<"
171
171
  - !ruby/object:Gem::Version
172
172
  version: '0.4'
@@ -644,10 +644,9 @@ files:
644
644
  - lib/eco/csv.rb
645
645
  - lib/eco/csv/table.rb
646
646
  - lib/eco/data.rb
647
- - lib/eco/data/crypto.rb
648
- - lib/eco/data/crypto/encryption.rb
649
647
  - lib/eco/data/files.rb
650
648
  - lib/eco/data/files/directory.rb
649
+ - lib/eco/data/files/encoding.rb
651
650
  - lib/eco/data/files/file_pattern.rb
652
651
  - lib/eco/data/files/helpers.rb
653
652
  - lib/eco/data/fuzzy_match.rb
@@ -698,7 +697,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
698
697
  - !ruby/object:Gem::Version
699
698
  version: '0'
700
699
  requirements: []
701
- rubygems_version: 3.1.4
700
+ rubygems_version: 3.4.12
702
701
  signing_key:
703
702
  specification_version: 4
704
703
  summary: eco-helpers to manage people api cases