eco-helpers 2.6.3 → 2.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (172) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.rubocop.yml +95 -0
  4. data/CHANGELOG.md +135 -2
  5. data/Rakefile +13 -7
  6. data/eco-helpers.gemspec +3 -3
  7. data/lib/eco/api/common/loaders/base.rb +2 -2
  8. data/lib/eco/api/common/loaders/case_base.rb +1 -1
  9. data/lib/eco/api/common/loaders/config/workflow/mailer.rb +5 -5
  10. data/lib/eco/api/common/loaders/error_handler.rb +8 -5
  11. data/lib/eco/api/common/loaders/parser.rb +44 -22
  12. data/lib/eco/api/common/loaders/policy.rb +6 -4
  13. data/lib/eco/api/common/loaders/use_case.rb +13 -7
  14. data/lib/eco/api/common/people/base_parser.rb +0 -2
  15. data/lib/eco/api/common/people/default_parsers/boolean_parser.rb +0 -1
  16. data/lib/eco/api/common/people/default_parsers/csv_parser.rb +1 -1
  17. data/lib/eco/api/common/people/default_parsers/date_parser.rb +64 -12
  18. data/lib/eco/api/common/people/default_parsers/freemium_parser.rb +0 -1
  19. data/lib/eco/api/common/people/default_parsers/login_providers_parser.rb +13 -5
  20. data/lib/eco/api/common/people/default_parsers/multi_parser.rb +0 -1
  21. data/lib/eco/api/common/people/default_parsers/numeric_parser.rb +18 -5
  22. data/lib/eco/api/common/people/default_parsers/policy_groups_parser.rb +8 -8
  23. data/lib/eco/api/common/people/default_parsers/select_parser.rb +50 -26
  24. data/lib/eco/api/common/people/default_parsers/send_invites_parser.rb +6 -6
  25. data/lib/eco/api/common/people/default_parsers/xls_parser.rb +9 -12
  26. data/lib/eco/api/common/people/default_parsers.rb +1 -12
  27. data/lib/eco/api/common/people/entries.rb +13 -13
  28. data/lib/eco/api/common/people/entry_factory.rb +76 -45
  29. data/lib/eco/api/common/people/person_attribute_parser.rb +8 -12
  30. data/lib/eco/api/common/people/person_entry.rb +86 -75
  31. data/lib/eco/api/common/people/person_entry_attribute_mapper.rb +60 -44
  32. data/lib/eco/api/common/people/person_factory.rb +30 -22
  33. data/lib/eco/api/common/people/person_modifier.rb +11 -13
  34. data/lib/eco/api/common/people/person_parser.rb +101 -39
  35. data/lib/eco/api/common/people/supervisor_helpers.rb +25 -26
  36. data/lib/eco/api/common/session/base_session.rb +9 -9
  37. data/lib/eco/api/common/session/environment.rb +7 -5
  38. data/lib/eco/api/common/session/sftp.rb +59 -32
  39. data/lib/eco/api/common/version_patches/ecoportal_api/external_person.rb +10 -6
  40. data/lib/eco/api/common/version_patches/exception.rb +11 -13
  41. data/lib/eco/api/error.rb +32 -20
  42. data/lib/eco/api/microcases/set_supervisor.rb +0 -3
  43. data/lib/eco/api/organization/node_classifications.rb +82 -0
  44. data/lib/eco/api/organization/policy_groups.rb +4 -6
  45. data/lib/eco/api/organization/tag_tree.rb +169 -93
  46. data/lib/eco/api/organization.rb +1 -0
  47. data/lib/eco/api/session/batch/job.rb +1 -1
  48. data/lib/eco/api/session/config/tagtree.rb +41 -23
  49. data/lib/eco/api/session/config/workflow.rb +113 -88
  50. data/lib/eco/api/session/config.rb +6 -0
  51. data/lib/eco/api/session.rb +51 -29
  52. data/lib/eco/api/usecases/base_io.rb +28 -25
  53. data/lib/eco/api/usecases/default/locations/cli/tagtree_extract_cli.rb +7 -2
  54. data/lib/eco/api/usecases/default/locations/cli/tagtree_upload_cli.rb +21 -0
  55. data/lib/eco/api/usecases/default/locations/csv_to_tree_case.rb +3 -3
  56. data/lib/eco/api/usecases/default/locations/tagtree_extract_case.rb +54 -23
  57. data/lib/eco/api/usecases/default/locations/tagtree_upload_case.rb +87 -0
  58. data/lib/eco/api/usecases/default/locations.rb +1 -0
  59. data/lib/eco/api/usecases/default/people/analyse_people_case.rb +60 -56
  60. data/lib/eco/api/usecases/default/people/change_email_case.rb +8 -9
  61. data/lib/eco/api/usecases/default/people/clean_unknown_tags_case.rb +13 -11
  62. data/lib/eco/api/usecases/default/people/clear_abilities_case.rb +2 -2
  63. data/lib/eco/api/usecases/default/people/org_data_convert_case.rb +25 -27
  64. data/lib/eco/api/usecases/default/people/refresh_case.rb +2 -2
  65. data/lib/eco/api/usecases/default/people/reinvite_trans_case.rb +1 -1
  66. data/lib/eco/api/usecases/default/people/reinvite_trans_cli.rb +0 -1
  67. data/lib/eco/api/usecases/default/people/restore_db_case.rb +39 -34
  68. data/lib/eco/api/usecases/default/people/set_default_tag_case.rb +19 -15
  69. data/lib/eco/api/usecases/default/people/supers_cyclic_identify_case.rb +16 -12
  70. data/lib/eco/api/usecases/default_cases/hris_case.rb +17 -15
  71. data/lib/eco/api/usecases/default_cases/samples/sftp_case.rb +30 -16
  72. data/lib/eco/api/usecases/default_cases/to_csv_detailed_case.rb +0 -2
  73. data/lib/eco/api/usecases/graphql/base.rb +5 -3
  74. data/lib/eco/api/usecases/graphql/helpers/base/case_env.rb +4 -1
  75. data/lib/eco/api/usecases/graphql/helpers/base/graphql_env.rb +14 -0
  76. data/lib/eco/api/usecases/graphql/helpers/base.rb +5 -4
  77. data/lib/eco/api/usecases/graphql/helpers/location/base/classifications_parser.rb +60 -0
  78. data/lib/eco/api/usecases/graphql/helpers/location/base/tree_tracking.rb +72 -0
  79. data/lib/eco/api/usecases/graphql/helpers/location/base.rb +25 -59
  80. data/lib/eco/api/usecases/graphql/helpers/location/command/diff/as_update.rb +59 -0
  81. data/lib/eco/api/usecases/graphql/helpers/location/command/diff/compare.rb +49 -0
  82. data/lib/eco/api/usecases/graphql/helpers/location/command/diff.rb +11 -0
  83. data/lib/eco/api/usecases/graphql/helpers/location/command/diffs/stages/commandable.rb +46 -0
  84. data/lib/eco/api/usecases/graphql/helpers/location/command/diffs/stages/diff_sortable/for_archive.rb +23 -0
  85. data/lib/eco/api/usecases/graphql/helpers/location/command/diffs/stages/diff_sortable/for_unarchive.rb +65 -0
  86. data/lib/eco/api/usecases/graphql/helpers/location/command/diffs/stages/diff_sortable.rb +49 -0
  87. data/lib/eco/api/usecases/graphql/helpers/location/command/diffs/stages/sortable/relation_safe_sort.rb +119 -0
  88. data/lib/eco/api/usecases/graphql/helpers/location/command/diffs/stages/sortable.rb +59 -0
  89. data/lib/eco/api/usecases/graphql/helpers/location/command/diffs/stages.rb +82 -0
  90. data/lib/eco/api/usecases/graphql/helpers/location/command/diffs.rb +20 -0
  91. data/lib/eco/api/usecases/graphql/helpers/location/command/optimizations.rb +84 -0
  92. data/lib/eco/api/usecases/graphql/helpers/location/command/result.rb +4 -4
  93. data/lib/eco/api/usecases/graphql/helpers/location/command/results.rb +24 -12
  94. data/lib/eco/api/usecases/graphql/helpers/location/command.rb +21 -24
  95. data/lib/eco/api/usecases/graphql/helpers/location/tags_remap/tags_map.rb +1 -1
  96. data/lib/eco/api/usecases/graphql/helpers/location/tags_remap/tags_set.rb +10 -11
  97. data/lib/eco/api/usecases/graphql/helpers/location/tags_remap.rb +8 -9
  98. data/lib/eco/api/usecases/graphql/samples/location/command/dsl.rb +41 -12
  99. data/lib/eco/api/usecases/graphql/samples/location/command/results.rb +11 -80
  100. data/lib/eco/api/usecases/graphql/samples/location/command/service/tree_update.rb +89 -0
  101. data/lib/eco/api/usecases/graphql/samples/location/command/service.rb +6 -0
  102. data/lib/eco/api/usecases/graphql/samples/location/command/track_changed_ids.rb +89 -0
  103. data/lib/eco/api/usecases/graphql/samples/location/command.rb +3 -0
  104. data/lib/eco/api/usecases/graphql/samples/location/service/base.rb +9 -0
  105. data/lib/eco/api/usecases/graphql/samples/location/service/tree_diff/convertible/heading.rb +18 -0
  106. data/lib/eco/api/usecases/graphql/samples/location/service/tree_diff/convertible/inputable.rb +53 -0
  107. data/lib/eco/api/usecases/graphql/samples/location/service/tree_diff/convertible/parsing/classifications.rb +34 -0
  108. data/lib/eco/api/usecases/graphql/samples/location/service/tree_diff/convertible/parsing/helpers.rb +28 -0
  109. data/lib/eco/api/usecases/graphql/samples/location/service/tree_diff/convertible/parsing.rb +46 -0
  110. data/lib/eco/api/usecases/graphql/samples/location/service/tree_diff/convertible.rb +38 -0
  111. data/lib/eco/api/usecases/graphql/samples/location/service/tree_diff.rb +105 -0
  112. data/lib/eco/api/usecases/graphql/samples/location/service/tree_to_list/converter/discarded.rb +16 -0
  113. data/lib/eco/api/usecases/graphql/samples/location/service/tree_to_list/converter/input.rb +15 -0
  114. data/lib/eco/api/usecases/graphql/samples/location/service/tree_to_list/converter/node_attr_maps.rb +22 -0
  115. data/lib/eco/api/usecases/graphql/samples/location/service/tree_to_list/converter/parser.rb +45 -0
  116. data/lib/eco/api/usecases/graphql/samples/location/service/tree_to_list/converter.rb +36 -0
  117. data/lib/eco/api/usecases/graphql/samples/location/service/tree_to_list/output.rb +56 -0
  118. data/lib/eco/api/usecases/graphql/samples/location/service/tree_to_list.rb +41 -0
  119. data/lib/eco/api/usecases/graphql/samples/location/service.rb +8 -0
  120. data/lib/eco/api/usecases/graphql/samples/location.rb +1 -0
  121. data/lib/eco/api/usecases/graphql/utils/sftp.rb +96 -36
  122. data/lib/eco/api/usecases/ooze_cases/export_register_case.rb +8 -6
  123. data/lib/eco/api/usecases/ooze_samples/helpers/creatable.rb +4 -3
  124. data/lib/eco/api/usecases/ooze_samples/helpers/exportable_ooze.rb +39 -25
  125. data/lib/eco/api/usecases/ooze_samples/helpers/exportable_register.rb +13 -15
  126. data/lib/eco/api/usecases/ooze_samples/helpers/filters.rb +50 -21
  127. data/lib/eco/api/usecases/ooze_samples/helpers/ooze_handlers.rb +21 -11
  128. data/lib/eco/api/usecases/ooze_samples/helpers/rescuable.rb +2 -0
  129. data/lib/eco/api/usecases/ooze_samples/helpers/shortcuts.rb +49 -43
  130. data/lib/eco/api/usecases/ooze_samples/ooze_base_case.rb +17 -19
  131. data/lib/eco/api/usecases/ooze_samples/register_export_case.rb +48 -43
  132. data/lib/eco/api/usecases/ooze_samples/register_update_case.rb +34 -34
  133. data/lib/eco/api/usecases/ooze_samples/target_oozes_update_case.rb +8 -10
  134. data/lib/eco/api/usecases.rb +0 -1
  135. data/lib/eco/cli/config/use_cases.rb +31 -29
  136. data/lib/eco/cli_default/input_filters.rb +0 -5
  137. data/lib/eco/cli_default/people_filters.rb +4 -4
  138. data/lib/eco/cli_default/workflow.rb +13 -14
  139. data/lib/eco/csv/table.rb +34 -25
  140. data/lib/eco/data/hashes/array_diff.rb +24 -35
  141. data/lib/eco/data/hashes/diff_result/meta.rb +131 -0
  142. data/lib/eco/data/hashes/diff_result.rb +65 -57
  143. data/lib/eco/data/hashes/sanke_camel_indifferent_access.rb +278 -0
  144. data/lib/eco/data/hashes.rb +1 -1
  145. data/lib/eco/data/locations/convert.rb +1 -1
  146. data/lib/eco/data/locations/node_base/csv_convert.rb +19 -9
  147. data/lib/eco/data/locations/node_base/parsing.rb +4 -2
  148. data/lib/eco/data/locations/node_base/treeify.rb +149 -132
  149. data/lib/eco/data/locations/node_base.rb +15 -4
  150. data/lib/eco/data/locations/node_diff/accessors.rb +13 -5
  151. data/lib/eco/data/locations/node_diff/nodes_diff/clustered_treeify.rb +101 -0
  152. data/lib/eco/data/locations/node_diff/nodes_diff/diffs_tree.rb +99 -0
  153. data/lib/eco/data/locations/node_diff/{selectors.rb → nodes_diff/selectors.rb} +1 -1
  154. data/lib/eco/data/locations/node_diff/nodes_diff.rb +50 -35
  155. data/lib/eco/data/locations/node_diff.rb +45 -17
  156. data/lib/eco/data/locations/node_level/parsing.rb +15 -21
  157. data/lib/eco/data/locations/node_level.rb +66 -22
  158. data/lib/eco/data/locations/node_plain/parsing.rb +1 -1
  159. data/lib/eco/data/locations/node_plain.rb +60 -7
  160. data/lib/eco/data/strings/camel_case.rb +35 -0
  161. data/lib/eco/data/strings/snake_case.rb +18 -0
  162. data/lib/eco/data/strings.rb +8 -0
  163. data/lib/eco/data.rb +1 -0
  164. data/lib/eco/language/methods/call_detector.rb +11 -0
  165. data/lib/eco/language/methods/dsl_able.rb +7 -1
  166. data/lib/eco/language/methods.rb +2 -1
  167. data/lib/eco/language/models/collection.rb +23 -25
  168. data/lib/eco/language/models/parser_serializer.rb +24 -5
  169. data/lib/eco/version.rb +1 -1
  170. data/lib/eco-helpers.rb +0 -1
  171. metadata +54 -9
  172. data/lib/eco/data/hashes/diff_meta.rb +0 -52
@@ -0,0 +1,15 @@
1
+ module Eco::API::UseCases::GraphQL::Samples::Location::Service
2
+ module TreeToList
3
+ module Converter
4
+ module Input
5
+ include Eco::API::UseCases::GraphQL::Samples::Location::DSL
6
+
7
+ private
8
+
9
+ def input_tagtree
10
+ @input_tagtree ||= live_tree
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,22 @@
1
+ module Eco::API::UseCases::GraphQL::Samples::Location::Service
2
+ module TreeToList
3
+ module Converter
4
+ module NodeAttrMaps
5
+ # Basic mappints to create NodeDiff
6
+ NODE_ATTR_MAPS = {
7
+ 'id' => 'nodeId',
8
+ 'parent_id' => 'parentId',
9
+ 'parent_name' => 'parentName',
10
+ 'archived_token' => 'archivedToken',
11
+ 'level' => 'level_reference'
12
+ }.freeze
13
+
14
+ private
15
+
16
+ def node_attr_maps
17
+ self.class::NODE_ATTR_MAPS
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,45 @@
1
+ module Eco::API::UseCases::GraphQL::Samples::Location::Service
2
+ module TreeToList
3
+ module Converter
4
+ module Parser
5
+ private
6
+
7
+ # Adds up to the generic_node_parser
8
+ # def custom_node_parser(node_hash)
9
+ # node_hash.each do |key, val|
10
+ # end
11
+ # end
12
+
13
+ def node_parser_block
14
+ return @node_parser_block if instance_variable_defined?(:@node_parser_block)
15
+ custom_before = custom_node_parser_before_block
16
+ custom_after = custom_node_parser_block
17
+ @node_parser_block = proc do |node_hash|
18
+ node_hash.tap do
19
+ custom_before&.call(node_hash)
20
+ default_node_parse(node_hash)
21
+ custom_after&.call(node_hash)
22
+ end
23
+ end
24
+ end
25
+
26
+ # Grabs the custom node parser that runs after the generic one
27
+ def custom_node_parser_block
28
+ return nil unless respond_to?(:custom_node_parser, true)
29
+ method(:custom_node_parser)
30
+ end
31
+
32
+ def custom_node_parser_before_block
33
+ return nil unless respond_to?(:custom_node_parser_before, true)
34
+ method(:custom_node_parser_before)
35
+ end
36
+
37
+ # Generic parsing
38
+ def default_node_parse(node_hash)
39
+ class_ids = to_classification_ids(node_hash['classifications'])
40
+ node_hash['classifications'] = class_ids
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,36 @@
1
+ require_relative 'converter/parser'
2
+ require_relative 'converter/input'
3
+ require_relative 'converter/node_attr_maps'
4
+ require_relative 'converter/discarded'
5
+
6
+ module Eco::API::UseCases::GraphQL::Samples::Location::Service
7
+ module TreeToList
8
+ module Converter
9
+ include Parser
10
+ include Input
11
+ include NodeAttrMaps
12
+ include Discarded
13
+
14
+ private
15
+
16
+ # Converter. Transforms an org tagtree to a list of hash nodes
17
+ # @note it ensures `DISCARDED_NODES` are excluded
18
+ # @return [Array<Hash>] list of hash nodes
19
+ def as_nodes_json(tree = input_tagtree)
20
+ tree.reject do |node|
21
+ discarded?(node.id)
22
+ end.each_with_object([]) do |node, list|
23
+ list << node.as_json(include_children: false).transform_keys do |key|
24
+ next key unless node_attr_maps.key?(key)
25
+ node_attr_maps[key]
26
+ end.tap do |node_hash|
27
+ node_parser_block&.call(node_hash)
28
+ yield(node_hash) if block_given?
29
+ end
30
+ end.tap do |list|
31
+ list.each(&node_parser_block)
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,56 @@
1
+ module Eco::API::UseCases::GraphQL::Samples::Location::Service
2
+ module TreeToList
3
+ module Output
4
+ OUT_FOLDER = 'sftp'.freeze
5
+ OUT_TIME_FORMAT = '%Y%m%dT%H%M%S'.freeze
6
+ OUT_FILENAME = 'Location_Nodes.csv'.freeze
7
+ OUT_HEADER = %w[
8
+ nodeId
9
+ name
10
+ parentId
11
+ parentName
12
+ archived
13
+ archivedToken
14
+ weight
15
+ level_reference
16
+ ].freeze
17
+
18
+ private
19
+
20
+ def generate_live_nodes_file(nodes_list)
21
+ return unless generate_live_nodes_file?
22
+ CSV.open(output_filename, "w") do |csv|
23
+ csv << output_header
24
+ nodes_list do |item|
25
+ csv << item.values_at(*output_header)
26
+ end
27
+ end
28
+ log(:info) { "Created file: #{output_filename}" }
29
+ end
30
+
31
+ def output_header
32
+ self.class::OUT_HEADER
33
+ end
34
+
35
+ def generate_live_nodes_file?
36
+ true
37
+ end
38
+
39
+ def output_filename
40
+ @output_filename ||= File.join(output_folder, "#{timestamp}_#{output_file}")
41
+ end
42
+
43
+ def output_folder
44
+ "#{config.active_enviro}/#{self.class::OUT_FOLDER}"
45
+ end
46
+
47
+ def output_file
48
+ options.dig(:output, :file) || self.class::OUT_FILENAME
49
+ end
50
+
51
+ def timestamp(date = Time.now)
52
+ date.strftime(self.class::OUT_TIME_FORMAT)
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,41 @@
1
+ module Eco::API::UseCases::GraphQL::Samples
2
+ module Location::Service
3
+ # Service to transform (CONVERT or PARSE) an input locations structure into a list
4
+ # of hash nodes.
5
+ # @note it can have multiple usages
6
+ # 1. Live locations tree structure into a nodes list file (nodeId, parendId, name,...)
7
+ # 2. A locations tree built from an input csv into a nodes list hash
8
+ # 3. Change the keys of the hash nodes
9
+ # 4. Parse the values
10
+ # @note this use case can be inherited from to provision **parsed** input to other
11
+ # use cases.
12
+ # @note it allows to define `custom_node_parser` and `custom_node_parser_after`
13
+ # methods (param: `node_hash`), to allow custom parsing.
14
+ # - It has a generic parser that validates the `classifications`
15
+ # As this format allows to easily compare what has changed in the locations structure.
16
+ # @example
17
+ # class Custom::UseCase::TagtreeToList < Eco::API::UseCases::GraphQL::Samples::Location
18
+ # name 'tagtree-to-list'
19
+ # type :other
20
+ #
21
+ # include Eco::API::UseCases::GraphQL::Samples::Location::Service::TreeToList
22
+ #
23
+ # end
24
+ module TreeToList
25
+ include Eco::API::UseCases::GraphQL::Samples::Location::DSL
26
+
27
+ require_relative 'tree_to_list/output'
28
+ require_relative 'tree_to_list/converter'
29
+
30
+ include Output
31
+ include Converter
32
+
33
+ def process
34
+ as_nodes_json(input_tagtree).tap do |list|
35
+ next generate_live_nodes_file(list) unless list.empty?
36
+ log(:error) { "There are no location nodes!" }
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,8 @@
1
+ require_relative 'service/base'
2
+ require_relative 'service/tree_to_list'
3
+ require_relative 'service/tree_diff'
4
+
5
+ module Eco::API::UseCases::GraphQL::Samples
6
+ module Location::Service
7
+ end
8
+ end
@@ -7,4 +7,5 @@ module Eco::API::UseCases::GraphQL
7
7
  end
8
8
  end
9
9
 
10
+ require_relative 'location/service'
10
11
  require_relative 'location/command'
@@ -2,61 +2,121 @@ module Eco::API::UseCases::GraphQL::Utils
2
2
  module Sftp
3
3
  include Eco::API::UseCases::GraphQL::Helpers::Base::CaseEnv
4
4
 
5
-
6
- def remote_subfolder
7
- nil
8
- end
9
-
10
- def remote_folder(subfolder = remote_subfolder)
11
- "#{sftp_config.remote_folder}/#{subfolder || ''}"
5
+ def upload(local_file, subfolder: remote_subfolder, gid: sftp_group_id)
6
+ sftp_session.upload(
7
+ local_file,
8
+ remote_folder: remote_folder(subfolder),
9
+ gid: gid
10
+ )
12
11
  end
12
+ alias_method :sftp_upload, :upload
13
13
 
14
14
  def sftp_group_id
15
15
  if self.class.const_defined?(:SFTP_GROUP)
16
- self.class.const_get(:TARGET_STRUCTURE_ID)
17
- elsif group_id = options.dig(:sftp, :group)
16
+ self.class.const_get(:SFTP_GROUP)
17
+ elsif (group_id = options.dig(:sftp, :group))
18
18
  group_id
19
19
  end
20
20
  end
21
21
 
22
- def upload(local_file, remote_folder: self.remote_folder, gid: sftp_group_id)
23
- return false unless local_file && File.exist?(local_file)
24
- dest_file = "#{remote_folder}/#{File.basename(local_file)}"
25
- res = sftp_session.upload!(local_file, dest_file)
26
- attrs = sftp_session.stat!(dest_file)
27
- if gid && gid != attrs.gid
28
- stat_res = sftp_session.setstat!(dest_file, {permissions: 0660, uid: attrs.uid, gid: gid})
22
+ def sftp_download_files(subfolder: remote_subfolder, pattern: nil, local_folder: self.local_folder)
23
+ with_remote_files(subfolder: subfolder, pattern: pattern).tap do |files|
24
+ next if files.empty?
25
+ file_names = files.map {|file| to_remote_path(file.name, subfolder: subfolder)}
26
+
27
+ log(:info) {
28
+ msg = "Getting the following files into the local folder '#{local_folder}':\n"
29
+ msg << " • "
30
+ msg << files_names.join("\n • ")
31
+ msg
32
+ }
33
+
34
+ sftp.download(file_names, local_folder: local_folder)
29
35
  end
30
- logger.info("Uploaded '#{local_file}' (#{res})")
31
36
  end
32
37
 
33
- def ensure_remote_empty
34
- files = with_remote_files
35
- unless files.empty?
36
- msg = "There are still files in the remote folder that will be deleted: '#{remote_folder}':\n"
37
- msg += " • " + files.map do |file|
38
- file.longname
39
- end.join("\n • ") + "\n"
40
- session.prompt_user("Do you want to proceed to delete? (Y/n):", explanation: msg, default: "Y", timeout: 3) do |response|
41
- if response.upcase.start_with?("Y")
42
- files.each do |file|
43
- remote_full_path = to_remote_path(file.name)
44
- res = sftp_session.remove(remote_full_path)
45
- logger.info("Deleted remote file: '#{remote_full_path}' (#{res})")
46
- end
47
- end
38
+ def sftp_move_file(source, dest)
39
+ sftp.move(source, dest, 0x0001) do |response|
40
+ if response.ok?
41
+ log(:info) { "#{source}\n -to-> #{dest}" }
42
+ else
43
+ log(:info) { "Could not move file #{source}" }
44
+ end
45
+ end
46
+ end
47
+
48
+ def ensure_remote_empty(subfolder: remote_subfolder, pattern: nil)
49
+ files = with_remote_files(subfolder: subfolder, pattern: pattern)
50
+ return if files.empty?
51
+
52
+ msg = "There are still files in the remote folder that will be deleted: '#{subfolder}':\n"
53
+ msg << " • "
54
+ str = files.map(&:longname).join("\n • ")
55
+ str << "\n"
56
+ msg << str
57
+
58
+ session.prompt_user(
59
+ "Do you want to proceed to delete? (Y/n):",
60
+ explanation: msg,
61
+ default: "Y",
62
+ timeout: 3
63
+ ) do |response|
64
+ next unless response.upcase.start_with?("Y")
65
+
66
+ files.each do |file|
67
+ remote_full_path = to_remote_path(file.name, subfolder: subfolder)
68
+ res = sftp_session.remove(remote_full_path)
69
+ log(:info) {
70
+ "Deleted remote file: '#{remote_full_path}' (#{res})"
71
+ }
48
72
  end
49
73
  end
50
74
  end
51
75
 
52
- def with_remote_files
53
- sftp.files(remote_folder).each do |remote_file|
76
+ # @param subfolder [String] the subfolder.
77
+ def with_remote_files(subfolder: remote_subfolder, pattern: nil)
78
+ sftp.files(remote_folder(subfolder), pattern: pattern).each do |remote_file|
54
79
  yield(remote_file) if block_given?
55
80
  end
81
+ rescue ArgumentError
82
+ raise
83
+ rescue ::Net::SFTP::StatusException => err
84
+ log(:error) {
85
+ msg = "(#{self.class}) There was an error trying to access "
86
+ msg << "the remote folder '#{subfolder}': #{err}"
87
+ msg
88
+ }
89
+ []
90
+ end
91
+
92
+ def to_remote_path(file, subfolder: remote_subfolder)
93
+ [remote_folder(subfolder), file].join('/')
94
+ end
95
+
96
+ def remote_subfolder
97
+ if (remote_dir = options.dig(:sftp, :remote_subfolder))
98
+ remote_dir
99
+ elsif self.class.const_defined?(:REMOTE_FOLDER)
100
+ self.class::REMOTE_FOLDER
101
+ end
56
102
  end
57
103
 
58
- def to_remote_path(file, subfolder: nil)
59
- remote_folder(subfolder) + "/" + file
104
+ def remote_folder(subfolder = remote_subfolder)
105
+ [sftp_env_base_path, subfolder].compact.join('/')
106
+ end
107
+
108
+ def sftp_env_base_path
109
+ sftp_config.remote_folder
110
+ end
111
+
112
+ def local_folder
113
+ if (local_dir = options.dig(:sftp, :local_folder))
114
+ local_dir
115
+ elsif self.class.const_defined?(:LOCAL_FOLDER)
116
+ self.class::LOCAL_FOLDER
117
+ else
118
+ "."
119
+ end
60
120
  end
61
121
 
62
122
  def sftp_config
@@ -23,8 +23,8 @@ class Eco::API::UseCases::OozeCases::ExportRegisterCase < Eco::API::UseCases::Oo
23
23
 
24
24
  def filters
25
25
  [].tap do |fltrs|
26
- [:any, :all].each {|mode| fltrs << build_tags_filter(mode)}
27
- [:created_at, :updated_at].each {|key| fltrs << build_range_filter(key)}
26
+ %i[any all].each {|mode| fltrs << build_tags_filter(mode)}
27
+ %i[created_at updated_at].each {|key| fltrs << build_range_filter(key)}
28
28
  end.compact
29
29
  end
30
30
 
@@ -45,8 +45,10 @@ class Eco::API::UseCases::OozeCases::ExportRegisterCase < Eco::API::UseCases::Oo
45
45
  end
46
46
 
47
47
  def build_range_filter(key = :update_at)
48
- from = from_date(key); to = to_date(key)
48
+ from = from_date(key)
49
+ to = to_date(key)
49
50
  return nil unless from || to
51
+
50
52
  date_range_filter(from: from, to: to, key: key)
51
53
  end
52
54
 
@@ -64,9 +66,9 @@ class Eco::API::UseCases::OozeCases::ExportRegisterCase < Eco::API::UseCases::Oo
64
66
 
65
67
  def export_options
66
68
  @export_options ||= {
67
- delimiter: options_delimiter,
68
- only_indexed: include_deindexed?,
69
- only_labeled: include_unnamed?,
69
+ delimiter: options_delimiter,
70
+ only_indexed: include_deindexed?,
71
+ only_labeled: include_unnamed?,
70
72
  only_with_ref: include_unhashed?
71
73
  }
72
74
  end
@@ -13,9 +13,9 @@ module Eco
13
13
  drafting_entry(template_id) do |draft|
14
14
  yield(draft) if block_given?
15
15
 
16
- if page_id = create_entry(draft, reference: draft_reference)
16
+ if (page_id = create_entry(draft, reference: draft_reference))
17
17
  log(:info) { "Page '#{page_id}' created successfully -- #{draft_reference}" }
18
- elsif options.dig(:dry_run)
18
+ elsif options[:dry_run]
19
19
  log(:info) { "Simulated launch for #{draft_reference}" }
20
20
  end
21
21
  end
@@ -32,7 +32,7 @@ module Eco
32
32
  # Does the actual creation of the entry
33
33
  def create_entry(draft, reference: "new entry from #{draft&.template_id}")
34
34
  with_rescue(reference) do
35
- if result = create_ooze(draft, template_id: draft.template_id)
35
+ if (result = create_ooze(draft, template_id: draft.template_id))
36
36
  return result.page_id
37
37
  end
38
38
  end
@@ -48,6 +48,7 @@ module Eco
48
48
 
49
49
  def validate_base_type!(base)
50
50
  return super if defined?(super)
51
+
51
52
  msg = "#{self} can only be included in Eco::API::UseCases::OozeSamples::RegisterUpdateCase"
52
53
  msg << "\nCan't be included in #{base}"
53
54
  raise LoadError, msg unless base <= Eco::API::UseCases::OozeSamples::RegisterUpdateCase
@@ -5,18 +5,23 @@ module Eco
5
5
  module Helpers
6
6
  # Class to ease the export process
7
7
  class ExportableOoze
8
- MERGE_DELIMITER = "#:#"
9
-
10
- META_FIELDS = {
11
- "id" => "Internal ID",
12
- "uid" => "Unique ID",
13
- "name" => "Name of Page",
14
- "state" => "State of Page",
15
- "time_zone" => "Time Zone",
8
+ MERGE_DELIMITER = "#:#".freeze
9
+ META_FIELDS = {
10
+ "id" => "Internal ID",
11
+ "uid" => "Unique ID",
12
+ "name" => "Name of Page",
13
+ "state" => "State of Page",
14
+ "time_zone" => "Time Zone",
16
15
  "created_at" => "Page Created",
17
16
  "updated_at" => "Last updated",
18
- "tags" => "Location Tags"
19
- }
17
+ "tags" => "Location Tags"
18
+ }.freeze
19
+ DEFAULT_OPTIONS = {
20
+ delimiter: "\n",
21
+ only_indexed: true,
22
+ only_labeled: true,
23
+ only_with_ref: true
24
+ }.freeze
20
25
 
21
26
  class << self
22
27
  def label?(field)
@@ -24,9 +29,9 @@ module Eco
24
29
  end
25
30
 
26
31
  def label(field, default: nil)
27
- value = nil
32
+ value = nil
28
33
  value ||= field.label.to_s.strip if field.respond_to?(:label)
29
- value = (!value || value.empty?) ? nil : value
34
+ value = nil unless value && !value.empty?
30
35
  return value if value || !default
31
36
  default || "Unnamed field:"
32
37
  end
@@ -42,8 +47,10 @@ module Eco
42
47
 
43
48
  def key_to_ref(str)
44
49
  return str unless str.include?(MERGE_DELIMITER)
50
+
45
51
  ref = str.split(MERGE_DELIMITER).first
46
- ref.to_s.strip.empty?? nil : ref
52
+ return ref unless ref.to_s.strip.empty?
53
+ nil
47
54
  end
48
55
 
49
56
  def indexed?(field)
@@ -65,14 +72,11 @@ module Eco
65
72
  attr_reader :options
66
73
 
67
74
  def initialize(ooze, **options)
68
- raise "Expecting Ecoportal::API::V2::Page. Given: #{ooze.class}" unless ooze.is_a?(Ecoportal::API::V2::Page)
69
- @ooze = ooze
70
- @options = options.merge({
71
- delimiter: "\n",
72
- only_indexed: true,
73
- only_labeled: true,
74
- only_with_ref: true
75
- })
75
+ msg = "Expecting Ecoportal::API::V2::Page. Given: #{ooze.class}"
76
+ raise msg unless ooze.is_a?(Ecoportal::API::V2::Page)
77
+
78
+ @ooze = ooze
79
+ @options = DEFAULT_OPTIONS.merge(options)
76
80
  end
77
81
 
78
82
  def delimiter
@@ -92,8 +96,17 @@ module Eco
92
96
  meta_fields.tap do |data|
93
97
  with_field_section_pairs do |field, section|
94
98
  next unless export?(field)
95
- key = self.class.key_ref_label(field, default: alternative_label(field, section))
96
- data[key] = merge_values(data[key], to_value(field), klass: field.class, delimiter: delimiter)
99
+
100
+ key = self.class.key_ref_label(
101
+ field,
102
+ default: alternative_label(field, section)
103
+ )
104
+ data[key] = merge_values(
105
+ data[key],
106
+ to_value(field),
107
+ klass: field.class,
108
+ delimiter: delimiter
109
+ )
97
110
  end
98
111
  end
99
112
  end
@@ -109,6 +122,7 @@ module Eco
109
122
  sections = []
110
123
  ordered_sections.each_with_object([]) do |section, out|
111
124
  next if sections.include?(section)
125
+
112
126
  sections << section
113
127
  section.components.each do |field|
114
128
  out << [field, section]
@@ -140,8 +154,8 @@ module Eco
140
154
  def export?(field, **opts)
141
155
  opts = options.merge(opts)
142
156
  return false unless field.respond_to?(:to_s)
143
- return false if opts[:only_indexed] && !self.class.indexed?(field)
144
- return false if opts[:only_labeled] && !self.class.label?(field)
157
+ return false if opts[:only_indexed] && !self.class.indexed?(field)
158
+ return false if opts[:only_labeled] && !self.class.label?(field)
145
159
  return false if opts[:only_with_ref] && !self.class.with_ref?(field)
146
160
  true
147
161
  end
@@ -5,7 +5,6 @@ module Eco
5
5
  module Helpers
6
6
  # Class to ease the export process
7
7
  class ExportableRegister
8
-
9
8
  attr_reader :options
10
9
 
11
10
  include Eco::API::UseCases::OozeSamples::Helpers::OozeHandlers
@@ -13,17 +12,17 @@ module Eco
13
12
 
14
13
  def initialize(**options)
15
14
  @options = options.merge({
16
- delimiter: "\n",
17
- only_indexed: true,
18
- only_labeled: true,
15
+ delimiter: "\n",
16
+ only_indexed: true,
17
+ only_labeled: true,
19
18
  only_with_ref: true
20
19
  })
21
20
  end
22
21
 
23
22
  def add_ooze(ooze)
24
- ooz = ExportableOoze.new(ooze, **options)
25
- data = ooz.key_typed_data
26
- @typed_header = merge_arrays(data.keys, typed_header)
23
+ ooz = ExportableOoze.new(ooze, **options)
24
+ data = ooz.key_typed_data
25
+ @typed_header = merge_arrays(data.keys, typed_header)
27
26
  key_typed_data << data
28
27
  end
29
28
 
@@ -32,19 +31,18 @@ module Eco
32
31
  end
33
32
 
34
33
  def each(as_values: true, as_row: false, &block)
35
- return to_enum(:each, as_row: as_row) unless block
34
+ return to_enum(:each, as_values: as_values, as_row: as_row) unless block
35
+
36
36
  key_typed_data.each do |ooze_data|
37
37
  values = ooze_data.values_at(*typed_header)
38
- case
39
- when as_values
38
+
39
+ if as_values
40
40
  values
41
- when as_row
41
+ elsif as_row
42
42
  ::CSV::Row.new(values, header)
43
43
  else
44
44
  header.zip(values)
45
- end.tap do |out|
46
- yield(out)
47
- end
45
+ end.tap(&block)
48
46
  end
49
47
  end
50
48
 
@@ -62,7 +60,7 @@ module Eco
62
60
  end
63
61
 
64
62
  def key_typed_data
65
- @typed_keyed_data ||= []
63
+ @typed_keyed_data ||= [] # rubocop:disable Naming/MemoizedInstanceVariableName
66
64
  end
67
65
  end
68
66
  end