eco-helpers 2.0.16 → 2.0.22

Sign up to get free protection for your applications and to get access to all the features.
Files changed (98) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +125 -6
  3. data/eco-helpers.gemspec +10 -5
  4. data/lib/eco-helpers.rb +2 -0
  5. data/lib/eco/api/common/base_loader.rb +18 -0
  6. data/lib/eco/api/common/loaders/parser.rb +1 -0
  7. data/lib/eco/api/common/people/default_parsers.rb +1 -0
  8. data/lib/eco/api/common/people/default_parsers/date_parser.rb +11 -1
  9. data/lib/eco/api/common/people/default_parsers/login_providers_parser.rb +1 -1
  10. data/lib/eco/api/common/people/default_parsers/policy_groups_parser.rb +11 -11
  11. data/lib/eco/api/common/people/default_parsers/xls_parser.rb +53 -0
  12. data/lib/eco/api/common/people/entries.rb +1 -0
  13. data/lib/eco/api/common/people/entry_factory.rb +88 -23
  14. data/lib/eco/api/common/people/person_entry.rb +5 -2
  15. data/lib/eco/api/common/people/person_parser.rb +1 -1
  16. data/lib/eco/api/common/session.rb +1 -0
  17. data/lib/eco/api/common/session/base_session.rb +2 -0
  18. data/lib/eco/api/common/session/helpers.rb +30 -0
  19. data/lib/eco/api/common/session/helpers/prompt_user.rb +34 -0
  20. data/lib/eco/api/common/session/mailer.rb +0 -1
  21. data/lib/eco/api/common/session/s3_uploader.rb +0 -1
  22. data/lib/eco/api/common/session/sftp.rb +0 -1
  23. data/lib/eco/api/common/version_patches/ecoportal_api/external_person.rb +1 -1
  24. data/lib/eco/api/common/version_patches/ecoportal_api/internal_person.rb +7 -4
  25. data/lib/eco/api/common/version_patches/exception.rb +11 -4
  26. data/lib/eco/api/microcases.rb +3 -1
  27. data/lib/eco/api/microcases/append_usergroups.rb +0 -1
  28. data/lib/eco/api/microcases/people_cache.rb +2 -2
  29. data/lib/eco/api/microcases/people_load.rb +2 -2
  30. data/lib/eco/api/microcases/people_refresh.rb +2 -2
  31. data/lib/eco/api/microcases/people_search.rb +6 -6
  32. data/lib/eco/api/microcases/preserve_default_tag.rb +23 -0
  33. data/lib/eco/api/microcases/preserve_filter_tags.rb +28 -0
  34. data/lib/eco/api/microcases/preserve_policy_groups.rb +30 -0
  35. data/lib/eco/api/microcases/set_account.rb +0 -1
  36. data/lib/eco/api/microcases/with_each.rb +67 -6
  37. data/lib/eco/api/microcases/with_each_present.rb +4 -2
  38. data/lib/eco/api/microcases/with_each_starter.rb +4 -2
  39. data/lib/eco/api/organization.rb +1 -0
  40. data/lib/eco/api/organization/people.rb +98 -22
  41. data/lib/eco/api/organization/people_similarity.rb +272 -0
  42. data/lib/eco/api/organization/person_schemas.rb +5 -1
  43. data/lib/eco/api/organization/policy_groups.rb +5 -1
  44. data/lib/eco/api/organization/presets_factory.rb +22 -83
  45. data/lib/eco/api/organization/presets_integrity.json +6 -0
  46. data/lib/eco/api/organization/presets_values.json +5 -4
  47. data/lib/eco/api/organization/tag_tree.rb +33 -0
  48. data/lib/eco/api/policies/default_policies/99_user_access_policy.rb +0 -30
  49. data/lib/eco/api/session.rb +20 -28
  50. data/lib/eco/api/session/batch.rb +25 -7
  51. data/lib/eco/api/session/config.rb +0 -10
  52. data/lib/eco/api/session/config/apis.rb +80 -14
  53. data/lib/eco/api/session/config/people.rb +1 -17
  54. data/lib/eco/api/usecases.rb +2 -2
  55. data/lib/eco/api/usecases/base_case.rb +2 -2
  56. data/lib/eco/api/usecases/base_io.rb +17 -4
  57. data/lib/eco/api/usecases/default_cases.rb +2 -1
  58. data/lib/eco/api/usecases/default_cases/abstract_policygroup_abilities_case.rb +4 -4
  59. data/lib/eco/api/usecases/default_cases/analyse_people_case.rb +223 -0
  60. data/lib/eco/api/usecases/default_cases/clean_unknown_tags_case.rb +37 -0
  61. data/lib/eco/api/usecases/default_cases/codes_to_tags_case.rb +2 -3
  62. data/lib/eco/api/usecases/default_cases/reset_landing_page_case.rb +11 -1
  63. data/lib/eco/api/usecases/default_cases/restore_db_case.rb +1 -2
  64. data/lib/eco/api/usecases/default_cases/supers_cyclic_identify_case.rb +1 -1
  65. data/lib/eco/api/usecases/default_cases/supers_hierarchy_case.rb +1 -1
  66. data/lib/eco/api/usecases/default_cases/to_csv_case.rb +132 -29
  67. data/lib/eco/api/usecases/default_cases/to_csv_detailed_case.rb +61 -36
  68. data/lib/eco/api/usecases/ooze_samples/ooze_update_case.rb +3 -2
  69. data/lib/eco/cli/config/default/input.rb +61 -8
  70. data/lib/eco/cli/config/default/options.rb +48 -17
  71. data/lib/eco/cli/config/default/people.rb +18 -24
  72. data/lib/eco/cli/config/default/people_filters.rb +3 -3
  73. data/lib/eco/cli/config/default/usecases.rb +97 -32
  74. data/lib/eco/cli/config/default/workflow.rb +22 -13
  75. data/lib/eco/cli/config/help.rb +1 -0
  76. data/lib/eco/cli/config/options_set.rb +106 -13
  77. data/lib/eco/cli/config/use_cases.rb +33 -33
  78. data/lib/eco/cli/scripting/args_helpers.rb +32 -5
  79. data/lib/eco/csv.rb +4 -2
  80. data/lib/eco/csv/table.rb +121 -21
  81. data/lib/eco/data.rb +1 -0
  82. data/lib/eco/data/crypto/encryption.rb +3 -3
  83. data/lib/eco/data/files/helpers.rb +6 -4
  84. data/lib/eco/data/fuzzy_match.rb +201 -0
  85. data/lib/eco/data/fuzzy_match/array_helpers.rb +75 -0
  86. data/lib/eco/data/fuzzy_match/chars_position_score.rb +38 -0
  87. data/lib/eco/data/fuzzy_match/ngrams_score.rb +82 -0
  88. data/lib/eco/data/fuzzy_match/pairing.rb +95 -0
  89. data/lib/eco/data/fuzzy_match/result.rb +87 -0
  90. data/lib/eco/data/fuzzy_match/results.rb +77 -0
  91. data/lib/eco/data/fuzzy_match/score.rb +49 -0
  92. data/lib/eco/data/fuzzy_match/stop_words.rb +35 -0
  93. data/lib/eco/data/fuzzy_match/string_helpers.rb +82 -0
  94. data/lib/eco/version.rb +1 -1
  95. metadata +147 -11
  96. data/lib/eco/api/microcases/refresh_abilities.rb +0 -19
  97. data/lib/eco/api/organization/presets_reference.json +0 -59
  98. 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: 669f1a3e8d49a2499b02d925386477a8e52cc633c830072420484edcf8e4919d
4
- data.tar.gz: 86b36286a089bfa03d3cb17d4c17648a4dcc5f1ed07145ee81565cd19d102c90
3
+ metadata.gz: ba43099c7902b3e2623640a196abd86f39215403d28e7a614da78b2422629323
4
+ data.tar.gz: 7e3701cbeb7b3a0a65453dc002d6f4c5cb6e6ec4103e6cd0178c1af333d29d7d
5
5
  SHA512:
6
- metadata.gz: 0a346a01757374cc004c12b5aadadf6b3c1daebba937c8d1c0cf597608297ab825d604af3a7f70f92528e28e0f009fd482abd31eb660c4abcf9d9796f55f69db
7
- data.tar.gz: f912a967dfb21cb0e455b357c9f5ca28fae4de55cbacd060fd000add64992b86168a3ed38150d1ddaf9a264245da582a49c434d62d24bc28285d82b3f9a2e693
6
+ metadata.gz: f14ab296c2cff2dc694ef9182593329ca1bc86cf08ca32ba879cd5e74d658d1d123add425bc62a981182b9e4da06b33d225d613cd1a9d9cbfd7284e6b6d24ea5
7
+ data.tar.gz: f020822ad4ae2e18f1f0424bb3a47417ff9a6bc78a439517cee3cd9212548efe4c89256579d73ccde27709dc5d2ea3d3e74bd072de23d4c089ccf3c7a107fc07
data/CHANGELOG.md CHANGED
@@ -1,7 +1,130 @@
1
1
  # Change Log
2
2
  All notable changes to this project will be documented in this file.
3
3
 
4
- ## [2.0.16] - 2021-04-xx
4
+ ## [2.0.22] - 2021-06-18
5
+
6
+ ### Added
7
+ - exposed `logger` in `BaseLoader` and
8
+ - support for multiple input files
9
+ * `Eco::API::Common::People::EntryFactory#entries`:
10
+ - refactored to allow multiple input files parsing
11
+ - moreover to `idx`, hash entries will get their `source_file`
12
+ * Input callback at `lib/eco/cli/config/default/input` refactored format detection and enabled folder input
13
+ * `SCR.get_file` language extended to also mention folder (not just file)
14
+ - support for `.xls` and `.xlsx` files
15
+ * `Eco::API::Common::People::DefaultParsers::XLSParser` the Excel files **parser**
16
+ * `Eco::API::Common::People::PersonParser` added `:xls` as an accepted format
17
+ * `Eco::API::Session#fields_mapper` exposed mapper through a method to allow **headers detection**
18
+ - The external names of the fields are the column headers of the input file
19
+ * `Eco::API::UseCases::BaseIO` when arguments validation rails, now it raises with specific `MissingParameter` error
20
+
21
+ ### Changed
22
+ - dry out `BaseLoader` (only session is set as instance variable)
23
+ - removed `creek` **dependency** (it was not used anywhere in the gem)
24
+ * we just kept `roo` and `roo-xls`
25
+ - custom `Error` classes now all inherit from `StandardError` (rather than `Exception`)
26
+
27
+
28
+ ### Fixed
29
+
30
+
31
+ ## [2.0.21] - 2021-06-04
32
+
33
+ ### Added
34
+ - `Eco::CSV::Table`, support to create the table out of an `Array<Hash>`
35
+ - This opens new methods to transform input Excel file to this data structure and unify input data structures.
36
+ - **new** use case `Eco::API::UseCases::DefaultCases::CleanUnknownTags` invokable via `clean-unknown-tags`
37
+
38
+ ### Changed
39
+ - `Eco::API::Common::People::EntryFactory` slight **refactor** to boost better support for multiple input formats
40
+
41
+
42
+ ## [2.0.20] - 2021-05-31
43
+
44
+ ### Added
45
+ - **dependencies** to `creek`, `roo` and `roo-xls`
46
+ - **dependencies** to `hashdiff`
47
+ - `Eco::API::Session#parse_attribute` => added missing parameter `deps:`
48
+ - new option `-stdout [file]` to redirect the output to a file
49
+ - `Eco::CSV::Table`, **added** more helper methods `#group_by`, `#transform_values`, `#slice`, `#slice_columns`, `#delete_column`
50
+ - `Eco::API::Organization::TagTree` **added** more helper methods: `top?`, `tag=`, `as_json`, `dup`, `diff`
51
+
52
+ ### Fixed
53
+ - `Exception` patch: when `SystemStackError` there is not `backtrace` :/
54
+
55
+ ## [2.0.19] - 2021-05-31
56
+
57
+ ### Added
58
+ - Better error message for people searches & **offer** to select among the candidates:
59
+ - `Eco::API::Organization::People::MultipleSearchResults`, triggered from `Eco::API::Organization::People#find`
60
+ - `Eco::API::MicroCases#with_each` will offer the selection of candidates
61
+
62
+ ### Changed
63
+ - **renamed** and repurposed `Eco::API::Organization::PeopleAnalytics` to `PeopleSimilarity`
64
+
65
+ ### Fixed
66
+ - `Eco::Data::FuzzyMatch` adjustments for configuration propagation + some fixes
67
+ - Command option `-entries-from` can still be useful when used to obtain `-get-partial` of people base for `:export` use cases !!
68
+
69
+ ## [2.0.18] - 2021-05-25
70
+
71
+ ### Added
72
+ - **`-one-off`** option to not having to type the `-api-key` every time you launch one-off scripts
73
+ - `-api-key` will store the key to the `./.env_one_off` file (supports update and multi-environment)
74
+
75
+ ### Fixed
76
+ - patched `Exception#patch_full_message` to do not enter into a cyclic error rescue
77
+ - also rescue on `workflow.rescue`
78
+
79
+ ## [2.0.17] - 2021-05-25
80
+
81
+ Specific changes due to eP **release `1.5.9.70`** (_Policy Group Abilities_)
82
+ - And some improvements as well as new tools
83
+
84
+ ### Added
85
+ - `Eco::API::Organization::PresetsFactory` added integrity validation for `person_abilities` ability
86
+ - `Eco::API::Organization::PresetsFactory`
87
+ - `#validate`: returns an `Array` with all the errors that a `permissions_custom` has
88
+ - `#valid?`: checks if a `permissions_custom` is valid
89
+ - `Eco::API::Organization::PeopleAnalytics`: a **helper** class to identify things in the People Manager and provide mitigation action methods to resolve them.
90
+ - Added dependencies to `fuzzy_match`, `amatch` and `jaro_winkler` **gems**
91
+ - `Eco::Data::FuzzyMatch` with string match helpers and a set of home-made generic libs.
92
+ - `Eco::API::Organization::PeopleAnalytics` to launch analysis on the People Manager
93
+ - `Eco::API::UseCases::DefaultCases::AnalysePeople` invokable via `-analyse-people`
94
+ - **new** -> a way to define a hierarchy of options (at least of 1 Level)
95
+ - `API::CLI::Config::OptionsSet`
96
+ - `API::CLI::Config::UseCases::CaseConfig`
97
+ - Integrated the new feature to the `--help` command and methods.
98
+ - **Usecase** `Eco::API::UseCases::DefaultCases::ResetLandingPageCase` added parameter to specify `-page-id`
99
+ - `Eco::API::Common::BaseLoader` new shortcut methods `#micro` (_MicroCases_), `#session` and `#config`
100
+ - At this stage of the execution workflow it uses `ASSETS.session`
101
+ - **Important note**: when the `#parser` or `#serializer` are called the `ASSETS.session` might already be linked to the specific invoked environment
102
+ - New method helpers in `Ecoporta::API::MicroCases`
103
+ - `preserve_filter_tags`, `preserve_default_tag` and `preserve_policy_groups`
104
+
105
+ ### Changed
106
+ - upgraded dependency on `ecoportal-api` and `ecoportal-api-v2`
107
+ - **removed** `Eco::API::MicroCases#refresh_abilities`
108
+ - `Eco::API::MicroCases#set_account` **removed** `refresh_abilities`
109
+ - `Eco::API::MicroCases#append_usergroups` **removed** `refresh_abilities`
110
+ - **removed** _usecase_ `Eco::API::UseCases::DefaultCases::RefreshAbilitiesCase` (`-refresh-abilities`)
111
+ - `Eco::API::Policies::DefaultPolicies::UserAccess`: **removed** `refresh_abilities`
112
+ - **removed** `Eco::API::Session#new_preset`
113
+ - `Eco::API::Organization::PresetsFactory`
114
+ - **removed** `rspecs`
115
+ - `.new`: **removed** parameters `presets_custom` & `presets_map`
116
+ - **removed** constants `DEFAULT_CUSTOM`, and `DEFAULT_MAP`
117
+ - **removed** private methods `#presets_custom` and `#presets_map`
118
+ - **changed** private method `#compile` receives directly an array of `permissions_custom`
119
+ - `Eco::API::Session::Config` **removed** methods `#presets_custom=` & `presets_map=`
120
+ - `Eco::API::Session::Config::People` **removed** methods `#presets_custom=`, `#presets_map=`,`#presets_custom` & `#presets_map`
121
+ - **removed** `eco/api/organization/presets_reference.json`
122
+ - **moved** case-specific options to only be active when the user case is previously invoked in the command line.
123
+
124
+ ### Fixed
125
+ - **handle** `Ecoportal::API:Errors::TimeOut` in `Eco::API::Session::Batch` by offering to retry.
126
+
127
+ ## [2.0.16] - 2021-05-04
5
128
 
6
129
  ### Added
7
130
  - Use case `Eco::API::UseCases::DefaultCases::SupersCyclicIdentify`, invokable via `-identify-cyclic-supers`
@@ -11,14 +134,10 @@ All notable changes to this project will be documented in this file.
11
134
  2. `-enviro [live|pre.dev]`
12
135
  3. `-org NAME_OF_ORG`
13
136
 
14
- ### Changed
15
- ### Fixed
16
-
17
-
18
137
  ## [2.0.15] - 2021-04-29
19
138
 
20
139
  ### Added
21
- - Use case `Eco::API::UseCases::DefaultCases::SupersHierarchy`, invokable via `-supers-hiearchy`
140
+ - Use case `Eco::API::UseCases::DefaultCases::SupersHierarchy`, invokable via `-supers-hierarchy`
22
141
  ### Fixed
23
142
  - `eco/cli/config/default/workflow.rb` prevent `rescue` looping
24
143
 
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,22 @@ 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
- #spec.add_dependency 'fuzzy_match' '>= 2.1.0', '< 2.2'
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'
40
45
  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,24 @@ module Eco
48
48
  self.class.name
49
49
  end
50
50
 
51
+ private
52
+
53
+ def session
54
+ ASSETS.session
55
+ end
56
+
57
+ def config
58
+ session.config
59
+ end
60
+
61
+ def logger
62
+ session.logger
63
+ end
64
+
65
+ def micro
66
+ session.micro
67
+ end
68
+
51
69
  end
52
70
  end
53
71
  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 ||= {}
@@ -44,3 +44,4 @@ require_relative 'default_parsers/freemium_parser'
44
44
  require_relative 'default_parsers/policy_groups_parser'
45
45
  require_relative 'default_parsers/login_providers_parser'
46
46
  require_relative 'default_parsers/csv_parser'
47
+ require_relative 'default_parsers/xls_parser'
@@ -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
@@ -0,0 +1,53 @@
1
+ class Eco::API::Common::People::DefaultParsers::XLSParser < Eco::API::Common::Loaders::Parser
2
+ attribute :xls
3
+
4
+ attr_accessor :already_required
5
+ attr_reader :file
6
+
7
+ def parser(file, deps)
8
+ @file = file
9
+ rows.tap {|r| @file = nil}
10
+ end
11
+
12
+ def serializer(array_hash, deps)
13
+ raise "Not implemented. TODO: using axlsx or rubyXL gems. See: https://spin.atomicobject.com/2017/03/22/parsing-excel-files-ruby/"
14
+ end
15
+
16
+ private
17
+
18
+ def headers
19
+ raise "You should implement this method"
20
+ end
21
+
22
+ def sheet_name
23
+ 0
24
+ end
25
+
26
+ def workbook
27
+ require_reading_libs!
28
+ Roo::Spreadsheet.open(file)
29
+ end
30
+
31
+ def spreadheet(name_or_index = sheet_name)
32
+ workbook.sheet(name_or_index)
33
+ end
34
+
35
+ def rows(target = headers)
36
+ begin
37
+ spreadheet.parse(header_search: target)
38
+ rescue Roo::HeaderRowNotFoundError => e
39
+ missing = JSON.parse(e.message)
40
+ logger.warn("The input file is missing these headers: #{missing}")
41
+ present = target - missing
42
+ rows(present)
43
+ end
44
+ end
45
+
46
+ def require_reading_libs!
47
+ return if already_required
48
+ require 'roo'
49
+ require 'roo-xls'
50
+ already_required = true
51
+ end
52
+
53
+ 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,63 @@ 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
+ # Support for multiple file
105
+ if file.is_a?(Array)
106
+ return file.each_with_object([]) do |f, out|
107
+ logger.info("Parsing file '#{f}'")
108
+ curr = to_array_of_hashes(**kargs.merge(file: f))
109
+ out.concat(curr)
110
+ end
111
+ end
112
+ # Get content only when it's not :xls
113
+ # note: even if content was provided, file takes precedence
114
+ content = get_file_content(file, format, encoding) if (format != :xls) && file
115
+
116
+ case content
117
+ when Hash
118
+ logger.error("Input data as 'Hash' not supported. Expecting 'Enumerable' or 'String'")
119
+ exit(1)
120
+ when String
121
+ to_array_of_hashes(content: person_parser.parse(format, content))
122
+ when Enumerable
123
+ sample = content.to_a.first
124
+ case sample
125
+ when Hash, Array, ::CSV::Row
126
+ Eco::CSV::Table.new(content).to_array_of_hashes
86
127
  else
87
- logger.warn("File does not exist: #{file}")
128
+ logger.error("Input content 'Array' of '#{sample.class}' is not supported.")
88
129
  end
89
-
90
- entries(data: arr_hash)
91
130
  else
92
- Entries.new(data, klass: PersonEntry, factory: self)
131
+ if file && format == :xls
132
+ person_parser.parse(format, file)
133
+ else
134
+ logger.error("Could not obtain any data out of these: #{kargs}. Given content: '#{content.class}'")
135
+ exit(1)
136
+ end
137
+ end.tap do |out_array|
138
+ start_from_two = (format == :csv) || format == :xls
139
+ out_array.each_with_index do |entry_hash, i|
140
+ entry_hash["idx"] = start_from_two ? i + 2 : i + 1
141
+ entry_hash["source_file"] = file
142
+ end
93
143
  end
144
+
94
145
  end
95
146
 
147
+
96
148
  # Helper that generates a file out of `data:`.
97
149
  # @raise Exception
98
150
  # - if you try to provide `data:` in the wrong format.
@@ -104,7 +156,7 @@ module Eco
104
156
  # @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
157
  # @param encoding [String] optional parameter to geneate `file:` content by unsing certain encoding.
106
158
  # @return [Void].
107
- def export(data:, file: "export", format: :csv, encoding: "utf-8")
159
+ def export(data:, file: "export", format: :csv, encoding: "utf-8", internal_names: false)
108
160
  fatal("data: Expected Eco::API::Organization::People object. Given: #{data.class}") unless data.is_a?(Eco::API::Organization::People)
109
161
  fatal("A file should be specified.") unless !file.to_s.strip.empty?
110
162
  fatal("Format should be a Symbol. Given '#{format}'") if format && !format.is_a?(Symbol)
@@ -112,16 +164,18 @@ module Eco
112
164
 
113
165
  run = true
114
166
  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)
167
+ prompt_user("The file '#{file}' already exists. Do you want to overwrite it? (Y/n):", default: "Y") do |response|
168
+ run = (response == "") || reponse.upcase.start_with?("Y")
169
+ end
118
170
  end
119
171
 
120
172
  if run
121
173
  deps = {"supervisor_id" => {people: data}}
122
174
 
123
175
  data_entries = data.map do |person|
124
- self.new(person, dependencies: deps).external_entry
176
+ self.new(person, dependencies: deps).yield_self do |entry|
177
+ internal_names ? entry.mapped_entry : entry.external_entry
178
+ end
125
179
  end
126
180
 
127
181
  File.open(file, "w", enconding: encoding) do |fd|
@@ -133,6 +187,17 @@ module Eco
133
187
 
134
188
  private
135
189
 
190
+ def get_file_content(file, format, encoding)
191
+ unless Eco::API::Common::Session::FileManager.file_exists?(file)
192
+ logger.error("File does not exist: #{file}")
193
+ exit(1)
194
+ end
195
+ ext = File.extname(file)
196
+ encoding ||= Eco::API::Common::Session::FileManager.encoding(file)
197
+ encoding = (encoding != "utf-8")? "#{encoding}|utf-8": encoding
198
+ content = File.read(file, encoding: encoding)
199
+ end
200
+
136
201
  def fatal(msg)
137
202
  logger.fatal(msg)
138
203
  raise msg