eco-helpers 2.0.15 → 2.0.21

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 (97) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +109 -3
  3. data/eco-helpers.gemspec +11 -5
  4. data/lib/eco-helpers.rb +2 -0
  5. data/lib/eco/api/common/base_loader.rb +14 -0
  6. data/lib/eco/api/common/loaders/parser.rb +1 -0
  7. data/lib/eco/api/common/people/default_parsers/date_parser.rb +11 -1
  8. data/lib/eco/api/common/people/default_parsers/login_providers_parser.rb +1 -1
  9. data/lib/eco/api/common/people/default_parsers/policy_groups_parser.rb +11 -11
  10. data/lib/eco/api/common/people/entries.rb +1 -0
  11. data/lib/eco/api/common/people/entry_factory.rb +74 -23
  12. data/lib/eco/api/common/people/person_entry.rb +5 -2
  13. data/lib/eco/api/common/people/supervisor_helpers.rb +27 -0
  14. data/lib/eco/api/common/session.rb +1 -0
  15. data/lib/eco/api/common/session/base_session.rb +2 -0
  16. data/lib/eco/api/common/session/file_manager.rb +2 -2
  17. data/lib/eco/api/common/session/helpers.rb +30 -0
  18. data/lib/eco/api/common/session/helpers/prompt_user.rb +34 -0
  19. data/lib/eco/api/common/session/mailer.rb +0 -1
  20. data/lib/eco/api/common/session/s3_uploader.rb +0 -1
  21. data/lib/eco/api/common/session/sftp.rb +0 -1
  22. data/lib/eco/api/common/version_patches/ecoportal_api/external_person.rb +1 -1
  23. data/lib/eco/api/common/version_patches/ecoportal_api/internal_person.rb +7 -4
  24. data/lib/eco/api/common/version_patches/exception.rb +11 -4
  25. data/lib/eco/api/microcases.rb +3 -1
  26. data/lib/eco/api/microcases/append_usergroups.rb +0 -1
  27. data/lib/eco/api/microcases/people_cache.rb +2 -2
  28. data/lib/eco/api/microcases/people_load.rb +2 -2
  29. data/lib/eco/api/microcases/people_refresh.rb +2 -2
  30. data/lib/eco/api/microcases/people_search.rb +6 -6
  31. data/lib/eco/api/microcases/preserve_default_tag.rb +23 -0
  32. data/lib/eco/api/microcases/preserve_filter_tags.rb +28 -0
  33. data/lib/eco/api/microcases/preserve_policy_groups.rb +30 -0
  34. data/lib/eco/api/microcases/set_account.rb +0 -1
  35. data/lib/eco/api/microcases/with_each.rb +67 -6
  36. data/lib/eco/api/microcases/with_each_present.rb +4 -2
  37. data/lib/eco/api/microcases/with_each_starter.rb +4 -2
  38. data/lib/eco/api/organization.rb +1 -0
  39. data/lib/eco/api/organization/people.rb +98 -22
  40. data/lib/eco/api/organization/people_similarity.rb +272 -0
  41. data/lib/eco/api/organization/person_schemas.rb +5 -1
  42. data/lib/eco/api/organization/policy_groups.rb +5 -1
  43. data/lib/eco/api/organization/presets_factory.rb +40 -80
  44. data/lib/eco/api/organization/presets_integrity.json +6 -0
  45. data/lib/eco/api/organization/presets_values.json +5 -4
  46. data/lib/eco/api/organization/tag_tree.rb +33 -0
  47. data/lib/eco/api/policies/default_policies/99_user_access_policy.rb +0 -30
  48. data/lib/eco/api/session.rb +10 -24
  49. data/lib/eco/api/session/batch.rb +25 -7
  50. data/lib/eco/api/session/config.rb +16 -15
  51. data/lib/eco/api/session/config/api.rb +4 -0
  52. data/lib/eco/api/session/config/apis.rb +80 -0
  53. data/lib/eco/api/session/config/files.rb +7 -0
  54. data/lib/eco/api/session/config/people.rb +3 -19
  55. data/lib/eco/api/usecases/default_cases.rb +4 -1
  56. data/lib/eco/api/usecases/default_cases/abstract_policygroup_abilities_case.rb +161 -0
  57. data/lib/eco/api/usecases/default_cases/analyse_people_case.rb +223 -0
  58. data/lib/eco/api/usecases/default_cases/clean_unknown_tags_case.rb +37 -0
  59. data/lib/eco/api/usecases/default_cases/codes_to_tags_case.rb +2 -3
  60. data/lib/eco/api/usecases/default_cases/reset_landing_page_case.rb +11 -1
  61. data/lib/eco/api/usecases/default_cases/restore_db_case.rb +1 -2
  62. data/lib/eco/api/usecases/default_cases/supers_cyclic_identify_case.rb +72 -0
  63. data/lib/eco/api/usecases/default_cases/supers_hierarchy_case.rb +1 -1
  64. data/lib/eco/api/usecases/default_cases/to_csv_case.rb +132 -29
  65. data/lib/eco/api/usecases/default_cases/to_csv_detailed_case.rb +61 -36
  66. data/lib/eco/api/usecases/ooze_samples/ooze_update_case.rb +3 -2
  67. data/lib/eco/cli.rb +0 -10
  68. data/lib/eco/cli/config/default/options.rb +48 -17
  69. data/lib/eco/cli/config/default/people.rb +18 -24
  70. data/lib/eco/cli/config/default/people_filters.rb +3 -3
  71. data/lib/eco/cli/config/default/usecases.rb +105 -28
  72. data/lib/eco/cli/config/default/workflow.rb +21 -12
  73. data/lib/eco/cli/config/help.rb +1 -0
  74. data/lib/eco/cli/config/options_set.rb +106 -13
  75. data/lib/eco/cli/config/use_cases.rb +33 -33
  76. data/lib/eco/cli/scripting/args_helpers.rb +30 -3
  77. data/lib/eco/csv.rb +4 -2
  78. data/lib/eco/csv/table.rb +121 -21
  79. data/lib/eco/data.rb +1 -0
  80. data/lib/eco/data/crypto/encryption.rb +3 -3
  81. data/lib/eco/data/files/directory.rb +28 -20
  82. data/lib/eco/data/files/helpers.rb +6 -4
  83. data/lib/eco/data/fuzzy_match.rb +201 -0
  84. data/lib/eco/data/fuzzy_match/array_helpers.rb +75 -0
  85. data/lib/eco/data/fuzzy_match/chars_position_score.rb +38 -0
  86. data/lib/eco/data/fuzzy_match/ngrams_score.rb +82 -0
  87. data/lib/eco/data/fuzzy_match/pairing.rb +95 -0
  88. data/lib/eco/data/fuzzy_match/result.rb +87 -0
  89. data/lib/eco/data/fuzzy_match/results.rb +77 -0
  90. data/lib/eco/data/fuzzy_match/score.rb +49 -0
  91. data/lib/eco/data/fuzzy_match/stop_words.rb +35 -0
  92. data/lib/eco/data/fuzzy_match/string_helpers.rb +82 -0
  93. data/lib/eco/version.rb +1 -1
  94. metadata +168 -11
  95. data/lib/eco/api/microcases/refresh_abilities.rb +0 -19
  96. data/lib/eco/api/organization/presets_reference.json +0 -59
  97. data/lib/eco/api/usecases/default_cases/refresh_abilities_case.rb +0 -30
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9ea76fb273e572c873b2f23348c54b56fd4e51cd39d9bf1109ddbc4594ba260f
4
- data.tar.gz: 56d64b1eb28202f4bd18997ff8854fdd6ae36c758217796f4739012f59d4ef5c
3
+ metadata.gz: 5525ed41d4f4b42d96eb0d71f02911e7cfb9112890aff984fb872a60d7515976
4
+ data.tar.gz: d6dacbda91325cea0867c253b99458ae18b16e6450431ead852d686dd6e56859
5
5
  SHA512:
6
- metadata.gz: bf49254f9b3f162f278d65f734fba3d3f683906cb1202df4d4cc4f7a90f2d663655e5168cf5bdea13d78b63d8ece32bcd22541655b001f3a9bfad429d89d2de8
7
- data.tar.gz: 3dcd85969e34079aa050546e427ebe0d28cb7ec358b121f066cd603af23c9c44bb05551ce8db061f1721832981c2b74c1a46aa095c031f9ea0fb8b29b1b65310
6
+ metadata.gz: 1f4a164e153ac8e3d75bfc7024b2ee7c6c101a1703fb79b58a0d8c8fc31fc9978b6353c2a178a419c94ca22e6b49bf440364706642e7b087a4601f4cd7da3b51
7
+ data.tar.gz: 9fdd7340a79b853b0dd51676e9ea2ba773928c68928b62d7781a525b2c27c1032ec7da04cae1f57afb7b290649350280d0aa5d0f2e7b91ba8a46cfe685bbeadb
data/CHANGELOG.md CHANGED
@@ -1,11 +1,118 @@
1
1
  # Change Log
2
2
  All notable changes to this project will be documented in this file.
3
3
 
4
- ## [2.0.15] - 2021-04-xx
4
+ ## [2.0.21] - 2021-06-0x
5
5
 
6
6
  ### Added
7
- - Use case `Eco::API::UseCases::DefaultCases::SupersHierarchy`, invokable via `-supers-hiearchy`
7
+ - `Eco::CSV::Table`, support to create the table out of an `Array<Hash>`
8
+ - This opens new methods to transform input Excel file to this data structure and unify input data structures.
9
+ - **new** use case `Eco::API::UseCases::DefaultCases::CleanUnknownTags` invokable via `clean-unknown-tags`
10
+
8
11
  ### Changed
12
+ - `Eco::API::Common::People::EntryFactory` slight **refactor** to boost better support for multiple input formats
13
+
14
+ ### Fixed
15
+
16
+
17
+ ## [2.0.20] - 2021-05-31
18
+
19
+ ### Added
20
+ - **dependencies** to `creek`, `roo` and `roo-xls`
21
+ - **dependencies** to `hashdiff`
22
+ - `Eco::API::Session#parse_attribute` => added missing parameter `deps:`
23
+ - new option `-stdout [file]` to redirect the output to a file
24
+ - `Eco::CSV::Table`, **added** more helper methods `#group_by`, `#transform_values`, `#slice`, `#slice_columns`, `#delete_column`
25
+ - `Eco::API::Organization::TagTree` **added** more helper methods: `top?`, `tag=`, `as_json`, `dup`, `diff`
26
+
27
+ ### Fixed
28
+ - `Exception` patch: when `SystemStackError` there is not `backtrace` :/
29
+
30
+ ## [2.0.19] - 2021-05-31
31
+
32
+ ### Added
33
+ - Better error message for people searches & **offer** to select among the candidates:
34
+ - `Eco::API::Organization::People::MultipleSearchResults`, triggered from `Eco::API::Organization::People#find`
35
+ - `Eco::API::MicroCases#with_each` will offer the selection of candidates
36
+
37
+ ### Changed
38
+ - **renamed** and repurposed `Eco::API::Organization::PeopleAnalytics` to `PeopleSimilarity`
39
+
40
+ ### Fixed
41
+ - `Eco::Data::FuzzyMatch` adjustments for configuration propagation + some fixes
42
+ - Command option `-entries-from` can still be useful when used to obtain `-get-partial` of people base for `:export` use cases !!
43
+
44
+ ## [2.0.18] - 2021-05-25
45
+
46
+ ### Added
47
+ - **`-one-off`** option to not having to type the `-api-key` every time you launch one-off scripts
48
+ - `-api-key` will store the key to the `./.env_one_off` file (supports update and multi-environment)
49
+
50
+ ### Fixed
51
+ - patched `Exception#patch_full_message` to do not enter into a cyclic error rescue
52
+ - also rescue on `workflow.rescue`
53
+
54
+ ## [2.0.17] - 2021-05-25
55
+
56
+ Specific changes due to eP **release `1.5.9.70`** (_Policy Group Abilities_)
57
+ - And some improvements as well as new tools
58
+
59
+ ### Added
60
+ - `Eco::API::Organization::PresetsFactory` added integrity validation for `person_abilities` ability
61
+ - `Eco::API::Organization::PresetsFactory`
62
+ - `#validate`: returns an `Array` with all the errors that a `permissions_custom` has
63
+ - `#valid?`: checks if a `permissions_custom` is valid
64
+ - `Eco::API::Organization::PeopleAnalytics`: a **helper** class to identify things in the People Manager and provide mitigation action methods to resolve them.
65
+ - Added dependencies to `fuzzy_match`, `amatch` and `jaro_winkler` **gems**
66
+ - `Eco::Data::FuzzyMatch` with string match helpers and a set of home-made generic libs.
67
+ - `Eco::API::Organization::PeopleAnalytics` to launch analysis on the People Manager
68
+ - `Eco::API::UseCases::DefaultCases::AnalysePeople` invokable via `-analyse-people`
69
+ - **new** -> a way to define a hierarchy of options (at least of 1 Level)
70
+ - `API::CLI::Config::OptionsSet`
71
+ - `API::CLI::Config::UseCases::CaseConfig`
72
+ - Integrated the new feature to the `--help` command and methods.
73
+ - **Usecase** `Eco::API::UseCases::DefaultCases::ResetLandingPageCase` added parameter to specify `-page-id`
74
+ - `Eco::API::Common::BaseLoader` new shortcut methods `#micro` (_MicroCases_), `#session` and `#config`
75
+ - At this stage of the execution workflow it uses `ASSETS.session`
76
+ - **Important note**: when the `#parser` or `#serializer` are called the `ASSETS.session` might already be linked to the specific invoked environment
77
+ - New method helpers in `Ecoporta::API::MicroCases`
78
+ - `preserve_filter_tags`, `preserve_default_tag` and `preserve_policy_groups`
79
+
80
+ ### Changed
81
+ - upgraded dependency on `ecoportal-api` and `ecoportal-api-v2`
82
+ - **removed** `Eco::API::MicroCases#refresh_abilities`
83
+ - `Eco::API::MicroCases#set_account` **removed** `refresh_abilities`
84
+ - `Eco::API::MicroCases#append_usergroups` **removed** `refresh_abilities`
85
+ - **removed** _usecase_ `Eco::API::UseCases::DefaultCases::RefreshAbilitiesCase` (`-refresh-abilities`)
86
+ - `Eco::API::Policies::DefaultPolicies::UserAccess`: **removed** `refresh_abilities`
87
+ - **removed** `Eco::API::Session#new_preset`
88
+ - `Eco::API::Organization::PresetsFactory`
89
+ - **removed** `rspecs`
90
+ - `.new`: **removed** parameters `presets_custom` & `presets_map`
91
+ - **removed** constants `DEFAULT_CUSTOM`, and `DEFAULT_MAP`
92
+ - **removed** private methods `#presets_custom` and `#presets_map`
93
+ - **changed** private method `#compile` receives directly an array of `permissions_custom`
94
+ - `Eco::API::Session::Config` **removed** methods `#presets_custom=` & `presets_map=`
95
+ - `Eco::API::Session::Config::People` **removed** methods `#presets_custom=`, `#presets_map=`,`#presets_custom` & `#presets_map`
96
+ - **removed** `eco/api/organization/presets_reference.json`
97
+ - **moved** case-specific options to only be active when the user case is previously invoked in the command line.
98
+
99
+ ### Fixed
100
+ - **handle** `Ecoportal::API:Errors::TimeOut` in `Eco::API::Session::Batch` by offering to retry.
101
+
102
+ ## [2.0.16] - 2021-05-04
103
+
104
+ ### Added
105
+ - Use case `Eco::API::UseCases::DefaultCases::SupersCyclicIdentify`, invokable via `-identify-cyclic-supers`
106
+ - Use case `Eco::API::UseCases::DefaultCases::AbstractPolicyGroupAbilities`, invokable via `-abstract-policygroup-abilities`
107
+ - Option to run `one-off` scripts, without org configurations:
108
+ 1. `-api-key INTERNAL_API_KEY`
109
+ 2. `-enviro [live|pre.dev]`
110
+ 3. `-org NAME_OF_ORG`
111
+
112
+ ## [2.0.15] - 2021-04-29
113
+
114
+ ### Added
115
+ - Use case `Eco::API::UseCases::DefaultCases::SupersHierarchy`, invokable via `-supers-hierarchy`
9
116
  ### Fixed
10
117
  - `eco/cli/config/default/workflow.rb` prevent `rescue` looping
11
118
 
@@ -17,7 +124,6 @@ All notable changes to this project will be documented in this file.
17
124
  - `Eco::API::Organization::PresetsFactory` added integrity validation for `person_*` abilities
18
125
  - `Eco::API::Session::Batch::Job` more debug info on erron handlers
19
126
 
20
- ### Changed
21
127
  ### Fixed
22
128
  - `Eco::API::Error.get_type` was almost always matching `Eco::API::Error::Unclassified` -> fixed
23
129
 
data/eco-helpers.gemspec CHANGED
@@ -14,7 +14,7 @@ Gem::Specification.new do |spec|
14
14
  spec.homepage = "https://www.ecoportal.com"
15
15
  spec.licenses = %w[MIT]
16
16
 
17
- spec.required_ruby_version = '>= 2.4.4'
17
+ spec.required_ruby_version = '>= 2.5.0'
18
18
 
19
19
  spec.files = `git ls-files -z`.split("\x0").reject do |f|
20
20
  f.match(%r{^(test|spec|features)/})
@@ -24,17 +24,23 @@ Gem::Specification.new do |spec|
24
24
  #spec.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
25
25
  spec.require_paths = ["lib"]
26
26
 
27
- spec.add_development_dependency "bundler", ">= 2.2.11", "< 2.3"
27
+ spec.add_development_dependency "bundler", ">= 2.2.17", "< 2.3"
28
28
  spec.add_development_dependency "rspec", ">= 3.10.0", "< 3.11"
29
29
  spec.add_development_dependency "rake", ">= 13.0.3", "< 13.1"
30
30
  spec.add_development_dependency "yard", ">= 0.9.26", "< 0.10"
31
31
  spec.add_development_dependency "redcarpet", ">= 3.5.1", "< 3.6"
32
32
 
33
- spec.add_dependency 'ecoportal-api', '>= 0.8.2', '< 0.9'
34
- spec.add_dependency 'ecoportal-api-v2', '>= 0.8.6', '< 0.9'
33
+ spec.add_dependency 'ecoportal-api', '>= 0.8.3', '< 0.9'
34
+ spec.add_dependency 'ecoportal-api-v2', '>= 0.8.7', '< 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'
38
38
  spec.add_dependency 'net-sftp', '>= 3.0.0', '< 3.1'
39
-
39
+ spec.add_dependency 'hashdiff', '>= 1.0.1', '< 1.1'
40
+ spec.add_dependency 'fuzzy_match', '>= 2.1.0', '< 2.2'
41
+ spec.add_dependency 'amatch', '>= 0.4.0', '< 0.5'
42
+ spec.add_dependency 'jaro_winkler', '>= 1.5.4', '< 1.6'
43
+ spec.add_dependency 'roo', '>= 2.8.3', '< 2.9'
44
+ spec.add_dependency 'roo-xls', '>= 1.2.0', '< 1.3'
45
+ spec.add_dependency 'creek', '>= 2.5.2', '< 2.6'
40
46
  end
data/lib/eco-helpers.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  require 'csv'
2
2
  require 'json'
3
3
  require 'pp'
4
+ require 'dotenv'
5
+ require 'dotenv/load'
4
6
 
5
7
 
6
8
  module Eco
@@ -48,6 +48,20 @@ module Eco
48
48
  self.class.name
49
49
  end
50
50
 
51
+ private
52
+
53
+ def session
54
+ @session ||= ASSETS.session
55
+ end
56
+
57
+ def micro
58
+ session.micro
59
+ end
60
+
61
+ def config
62
+ @config ||= ASSETS.config
63
+ end
64
+
51
65
  end
52
66
  end
53
67
  end
@@ -19,6 +19,7 @@ module Eco
19
19
  @attribute = value
20
20
  end
21
21
 
22
+ # TODO: it migh rather merge?
22
23
  # Some parsers require dependencies to do their job.
23
24
  def dependencies(value = nil)
24
25
  @dependencies ||= {}
@@ -6,7 +6,7 @@ class Eco::API::Common::People::DefaultParsers::DateParser < Eco::API::Common::L
6
6
  end
7
7
 
8
8
  def serializer(value, deps)
9
- value.is_a?(Array) ? value.map { |d| d && d.strftime('%Y-%m-%d') } : value && value.strftime('%Y-%m-%d')
9
+ value.is_a?(Array) ? value.map { |d| serialize_date(d) } : serialize_date(value)
10
10
  end
11
11
 
12
12
  private
@@ -19,4 +19,14 @@ class Eco::API::Common::People::DefaultParsers::DateParser < Eco::API::Common::L
19
19
  end
20
20
  end
21
21
 
22
+ def serialize_date(value)
23
+ return value if value.is_a?(String)
24
+ begin
25
+ value && value.strftime('%Y-%m-%d')
26
+ rescue
27
+ nil
28
+ end
29
+ end
30
+
31
+
22
32
  end
@@ -20,7 +20,7 @@ class Eco::API::Common::People::DefaultParsers::LoginProvidersParser < Eco::API:
20
20
  private
21
21
 
22
22
  def login_providers
23
- @login_providers ||= ASSETS.config.login_providers
23
+ @login_providers ||= config.login_providers
24
24
  end
25
25
 
26
26
  end
@@ -1,31 +1,31 @@
1
1
  class Eco::API::Common::People::DefaultParsers::PolicyGroupsParser < Eco::API::Common::Loaders::Parser
2
2
  attribute "policy_group_ids"
3
+ parsing_phase :final
3
4
 
4
5
  def parser(hash, deps)
5
- policy_group_ids = []
6
- if policy_ids = hash["policy_group_ids"]
7
- policy_group_ids = policy_ids.split("|").map do |name|
8
- policy_groups.to_id(name&.downcase.strip)
9
- end.compact
6
+ policy_group_ids = hash["policy_group_ids"] || []
7
+ policy_group_ids.map do |name|
8
+ policy_groups.to_id(name&.downcase.strip)
9
+ end.compact.tap do |pg_names|
10
+ pg_names.push(default_id) if pg_names.empty?
10
11
  end
11
- policy_group_ids.empty?? default_id : policy_group_ids.join("|")
12
12
  end
13
13
 
14
14
  def serializer(person, deps)
15
15
  ids = person&.account&.policy_group_ids || []
16
16
  ids.map do |id|
17
17
  policy_groups.to_name(id)
18
- end.compact.join("|")
18
+ end.compact
19
19
  end
20
20
 
21
21
  private
22
22
 
23
- def policy_groups
24
- @policy_groups ||= ASSETS.config.policy_groups
23
+ def default_id
24
+ @default_id ||= policy_groups.to_id(config.people.default_usergroup)
25
25
  end
26
26
 
27
- def default_id
28
- @default_id ||= policy_groups.to_id(ASSETS.config.people.default_usergroup)
27
+ def policy_groups
28
+ @policy_groups ||= config.policy_groups
29
29
  end
30
30
 
31
31
  end
@@ -99,6 +99,7 @@ module Eco
99
99
  newFrom to_a - discarded
100
100
  end
101
101
 
102
+ # TODO: it should rather use the the people-to-csv case somehow
102
103
  # Helper to dump the entries into a CSV
103
104
  # @param filename [String] the destination file
104
105
  def export(filename)
@@ -2,6 +2,10 @@ module Eco
2
2
  module API
3
3
  module Common
4
4
  module People
5
+ # TODO: EntryFactory should suppport multiple schemas itself (rather that being done on `Session`)
6
+ # => currently, it's through session.entry_factory(schema: id), but this is wrong
7
+ # => This way, Entries and PersonEntry will be able to refer to attr_map and person_parser linked to schema_id
8
+ # => "schema_id" should be an optional column in the input file, or parsable via a custom parser to scope the schema
5
9
  # Helper factory class to generate entries (input entries).
6
10
  # @attr_reader schema [Ecoportal::API::V1::PersonSchema] person schema to be used in this entry factory
7
11
  class EntryFactory < Eco::API::Common::Session::BaseSession
@@ -12,7 +16,7 @@ module Eco
12
16
  # @param schema [Ecoportal::API::V1::PersonSchema] schema of person details that the parser will be based upon.
13
17
  # @param person_parser [nil, Eco::API::Common::People::PersonParser] set of attribute, type and format parsers/serializers.
14
18
  # @param attr_map [nil, Eco::Data::Mapper] attribute names mapper to translate external names into internal ones and _vice versa_.
15
- def initialize(e, schema:, person_parser: nil, attr_map: nil)
19
+ def initialize(e, schema:, person_parser: nil, default_parser: nil, attr_map: nil)
16
20
  fatal "Constructor needs a PersonSchema. Given: #{schema}" if !schema.is_a?(Ecoportal::API::V1::PersonSchema)
17
21
  fatal "Expecting PersonParser. Given: #{person_parser}" if person_parser && !person_parser.is_a?(Eco::API::Common::People::PersonParser)
18
22
  fatal "Expecting Mapper object. Given: #{fields_mapper}" if attr_map && !attr_map.is_a?(Eco::Data::Mapper)
@@ -22,14 +26,25 @@ module Eco
22
26
  @source_person_parser = person_parser
23
27
 
24
28
  # load default parser + custom parsers
25
- base_parser = Eco::API::Common::People::DefaultParsers.new(schema: @schema).merge(@source_person_parser)
29
+ @default_parser = default_parser&.new(schema: @schema) || Eco::API::Common::People::DefaultParsers.new(schema: @schema)
30
+ base_parser = @default_parser.merge(@source_person_parser)
26
31
  # new parser with linked schema
27
32
  @person_parser = @source_person_parser.new(schema: @schema).merge(base_parser)
28
33
  @person_parser_patch_version = @source_person_parser.patch_version
29
-
30
34
  @attr_map = attr_map
31
35
  end
32
36
 
37
+ def newFactory(schema: nil)
38
+ self.class.new(
39
+ environment,
40
+ schema: schema,
41
+ person_parser: @source_person_parser,
42
+ default_parser: @default_parser,
43
+ attr_map: @attr_map
44
+ )
45
+ end
46
+
47
+
33
48
  # provides with a Eco::API::Common::People::PersonParser object (collection of attribute parsers)
34
49
  # @note if the custom person parser has changed, it updates the copy of this EntryFactory instance
35
50
  # @return [Eco::API::Common::People::PersonParser] set of attribute, type and format parsers/serializers.
@@ -43,7 +58,7 @@ module Eco
43
58
 
44
59
  # key method to generate objects of `PersonEntry` that share dependencies via this `EntryFactory` environment.
45
60
  # @note this method is necessary to make the factory object work as a if it was a class `PersonEntry` you can call `new` on.
46
- # @param data [Array<Hash>] data to be parsed. The external hashed entry.
61
+ # @param data [Hash, Person] data to be parsed/serialized. Parsed: the external hashed entry. Serialized: a Person object.
47
62
  # @return [Eco::API::Common::People::PersonEntry]
48
63
  def new(data, dependencies: {})
49
64
  PersonEntry.new(
@@ -73,26 +88,49 @@ module Eco
73
88
  fatal("Format should be a Symbol. Given '#{format}'") if format && !format.is_a?(Symbol)
74
89
  fatal("There is no parser/serializer for format ':#{format.to_s}'") unless no_format || @person_parser.defined?(format)
75
90
 
76
- if file
77
- arr_hash = []
78
- if Eco::API::Common::Session::FileManager.file_exists?(file)
79
- encoding ||= Eco::API::Common::Session::FileManager.encoding(file)
80
- encoding = (encoding != "utf-8")? "#{encoding}|utf-8": encoding
81
- file_content = File.read(file, encoding: encoding)
82
- arr_hash = person_parser.parse(format, file_content).map.each_with_index do |entry_hash, i|
83
- j = (format == :csv)? i + 2 : i + 1
84
- entry_hash.tap {|hash| hash["idx"] = j}
85
- end
91
+ kargs = {}
92
+ kargs.merge!(content: data) unless no_data
93
+ kargs.merge!(file: file) unless no_file
94
+ kargs.merge!(format: format) unless no_format
95
+ kargs.merge!(encoding: encoding) if encoding
96
+
97
+ Entries.new(to_array_of_hashes(**kargs), klass: PersonEntry, factory: self)
98
+ end
99
+
100
+ def to_array_of_hashes(**kargs)
101
+ data = []
102
+ content, file, encoding, format = kargs.values_at(:content, :file, :encoding, :format)
103
+
104
+ content = get_file_content(file, format, encoding) if file
105
+
106
+ case content
107
+ when !content
108
+ logger.error("Could not obtain any data out of these: #{kargs}")
109
+ exit(1)
110
+ when Hash
111
+ logger.error("Input data as 'Hash' not supported. Expecting 'Enumerable' or 'String'")
112
+ exit(1)
113
+ when String
114
+ data = person_parser.parse(format, content).map.each_with_index do |entry_hash, i|
115
+ j = (format == :csv)? i + 2 : i + 1
116
+ entry_hash.tap {|hash| hash["idx"] = j}
117
+ end
118
+ to_array_of_hashes(content: data)
119
+ when Enumerable
120
+ sample = content.to_a.first
121
+ case sample
122
+ when Hash, Array, ::CSV::Row
123
+ Eco::CSV::Table.new(content).to_array_of_hashes
86
124
  else
87
- logger.warn("File does not exist: #{file}")
125
+ logger.error("Input 'Array' of '#{sample.class}' is not supported.")
88
126
  end
89
-
90
- entries(data: arr_hash)
91
127
  else
92
- Entries.new(data, klass: PersonEntry, factory: self)
128
+ logger.error("Could not obtain any data out of content: '#{content.class}'")
129
+ exit(1)
93
130
  end
94
131
  end
95
132
 
133
+
96
134
  # Helper that generates a file out of `data:`.
97
135
  # @raise Exception
98
136
  # - if you try to provide `data:` in the wrong format.
@@ -104,7 +142,7 @@ module Eco
104
142
  # @param format [Symbol] it specifies the format of the output `file:` (i.e. `:xml`, `:csv`). There must be a parser/serializer defined for it.
105
143
  # @param encoding [String] optional parameter to geneate `file:` content by unsing certain encoding.
106
144
  # @return [Void].
107
- def export(data:, file: "export", format: :csv, encoding: "utf-8")
145
+ def export(data:, file: "export", format: :csv, encoding: "utf-8", internal_names: false)
108
146
  fatal("data: Expected Eco::API::Organization::People object. Given: #{data.class}") unless data.is_a?(Eco::API::Organization::People)
109
147
  fatal("A file should be specified.") unless !file.to_s.strip.empty?
110
148
  fatal("Format should be a Symbol. Given '#{format}'") if format && !format.is_a?(Symbol)
@@ -112,16 +150,18 @@ module Eco
112
150
 
113
151
  run = true
114
152
  if Eco::API::Common::Session::FileManager.file_exists?(file)
115
- print "The file '#{file}' already exists. Do you want to overwrite it? (Y/n): "
116
- res = STDIN.gets.strip
117
- run = ["y", "Y", ""].include?(res)
153
+ prompt_user("The file '#{file}' already exists. Do you want to overwrite it? (Y/n):", default: "Y") do |response|
154
+ run = (response == "") || reponse.upcase.start_with?("Y")
155
+ end
118
156
  end
119
157
 
120
158
  if run
121
159
  deps = {"supervisor_id" => {people: data}}
122
160
 
123
161
  data_entries = data.map do |person|
124
- self.new(person, dependencies: deps).external_entry
162
+ self.new(person, dependencies: deps).yield_self do |entry|
163
+ internal_names ? entry.mapped_entry : entry.external_entry
164
+ end
125
165
  end
126
166
 
127
167
  File.open(file, "w", enconding: encoding) do |fd|
@@ -133,6 +173,17 @@ module Eco
133
173
 
134
174
  private
135
175
 
176
+ def get_file_content(file, format, encoding)
177
+ unless Eco::API::Common::Session::FileManager.file_exists?(file)
178
+ logger.error("File does not exist: #{file}")
179
+ exit(1)
180
+ end
181
+ ext = File.extname(file)
182
+ encoding ||= Eco::API::Common::Session::FileManager.encoding(file)
183
+ encoding = (encoding != "utf-8")? "#{encoding}|utf-8": encoding
184
+ content = File.read(file, encoding: encoding)
185
+ end
186
+
136
187
  def fatal(msg)
137
188
  logger.fatal(msg)
138
189
  raise msg