eco-helpers 0.6.17 → 0.7.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.
- checksums.yaml +4 -4
- data/.gitignore +19 -0
- data/.yardopts +2 -2
- data/Gemfile +6 -0
- data/Rakefile +27 -0
- data/eco-helpers.gemspec +9 -6
- data/lib/eco/api.rb +2 -1
- data/lib/eco/api/common/people.rb +1 -1
- data/lib/eco/api/common/people/base_parser.rb +31 -1
- data/lib/eco/api/common/people/default_parsers.rb +5 -1
- data/lib/eco/api/common/people/default_parsers/csv_parser.rb +37 -0
- data/lib/eco/api/common/people/default_parsers/numeric_parser.rb +0 -1
- data/lib/eco/api/common/people/entries.rb +14 -18
- data/lib/eco/api/common/people/entry_factory.rb +97 -9
- data/lib/eco/api/common/people/person_entry.rb +147 -206
- data/lib/eco/api/common/people/person_entry_attribute_mapper.rb +212 -0
- data/lib/eco/api/common/people/person_factory.rb +10 -12
- data/lib/eco/api/common/people/person_parser.rb +97 -37
- data/lib/eco/api/common/session/base_session.rb +1 -2
- data/lib/eco/api/common/session/file_manager.rb +1 -1
- data/lib/eco/api/organization.rb +2 -1
- data/lib/eco/api/organization/people.rb +54 -22
- data/lib/eco/api/organization/person_schemas.rb +54 -0
- data/lib/eco/api/organization/policy_groups.rb +5 -9
- data/lib/eco/api/organization/{presets.rb → presets_factory.rb} +1 -1
- data/lib/eco/api/policies.rb +10 -0
- data/lib/eco/api/policies/base_policy.rb +14 -0
- data/lib/eco/api/policies/policy.rb +20 -0
- data/lib/eco/api/policies/used_policies.rb +37 -0
- data/lib/eco/api/session.rb +36 -34
- data/lib/eco/api/session/batch.rb +94 -44
- data/lib/eco/api/session/batch_job.rb +108 -48
- data/lib/eco/api/session/batch_jobs.rb +4 -5
- data/lib/eco/api/session/batch_status.rb +70 -11
- data/lib/eco/api/session/config.rb +22 -5
- data/lib/eco/api/session/config/files.rb +10 -1
- data/lib/eco/api/session/config/people.rb +18 -5
- data/lib/eco/api/session/config/policies.rb +29 -0
- data/lib/eco/api/session/config/use_cases.rb +3 -7
- data/lib/eco/api/session/job_groups.rb +9 -10
- data/lib/eco/api/usecases.rb +2 -1
- data/lib/eco/api/usecases/base_case.rb +7 -2
- data/lib/eco/api/usecases/default_cases/change_email_case.rb +4 -2
- data/lib/eco/api/usecases/default_cases/create_case.rb +2 -1
- data/lib/eco/api/usecases/default_cases/create_details_case.rb +3 -1
- data/lib/eco/api/usecases/default_cases/create_details_with_supervisor_case.rb +4 -2
- data/lib/eco/api/usecases/default_cases/hris_case.rb +20 -13
- data/lib/eco/api/usecases/default_cases/new_email_case.rb +3 -1
- data/lib/eco/api/usecases/default_cases/new_id_case.rb +4 -2
- data/lib/eco/api/usecases/default_cases/recover_db_case.rb +9 -5
- data/lib/eco/api/usecases/default_cases/remove_account_case.rb +4 -2
- data/lib/eco/api/usecases/default_cases/set_supervisor_case.rb +4 -2
- data/lib/eco/api/usecases/default_cases/to_csv_case.rb +2 -2
- data/lib/eco/api/usecases/default_cases/to_csv_detailed_case.rb +2 -2
- data/lib/eco/api/usecases/default_cases/update_case.rb +16 -2
- data/lib/eco/api/usecases/default_cases/update_details_case.rb +3 -1
- data/lib/eco/api/usecases/default_cases/upsert_case.rb +25 -3
- data/lib/eco/api/usecases/use_case.rb +23 -140
- data/lib/eco/api/usecases/use_case_chain.rb +95 -0
- data/lib/eco/api/usecases/use_case_io.rb +117 -0
- data/lib/eco/api/usecases/use_group.rb +25 -5
- data/lib/eco/common/base_cli_backup.rb +1 -0
- data/lib/eco/language/models.rb +1 -1
- data/lib/eco/language/models/collection.rb +42 -31
- data/lib/eco/language/models/parser_serializer.rb +68 -0
- data/lib/eco/version.rb +1 -1
- metadata +93 -38
- data/lib/eco/api/common/people/types.rb +0 -47
- data/lib/eco/api/usecases/case_data.rb +0 -13
- data/lib/eco/language/models/attribute_parser.rb +0 -38
- data/lib/eco/lexic/dictionary.rb +0 -33
- data/lib/eco/lexic/dictionary/dictionary.txt +0 -355484
- data/lib/eco/lexic/dictionary/tags.json +0 -38
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a2c5373e513f2c43cd593684e28011ce9b0646a4457e19c31a5590f5cac0d743
|
4
|
+
data.tar.gz: d1985d695d0a8c390593a76f6cc65dbf8ebcbb584f6b1e6fd0032706be6c4299
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4101d84e2d20cdc95459277316177570af81d1fd9ef38aadba6f41d7df424a093d12619efaf4a811cc2374cb812235f86a3602ebf18b26ef8d33cfe469d5c89b
|
7
|
+
data.tar.gz: 25b6df7e98d964fb7b53685fa98f31eb0983138b8a3754404d5dd5704669adf69fee0bbe91162eca35234b0fcf5b6af09b3d053c995e9470371b2d880758d2f9
|
data/.gitignore
CHANGED
@@ -1 +1,20 @@
|
|
1
|
+
# it's a gem, ignore the lockfile
|
2
|
+
Gemfile.lock
|
3
|
+
|
4
|
+
# build artifacts
|
1
5
|
*.gem
|
6
|
+
/.bundle/
|
7
|
+
/vendor/bundle
|
8
|
+
/spec/reports/
|
9
|
+
/tmp/
|
10
|
+
/pkg/
|
11
|
+
|
12
|
+
# docs
|
13
|
+
/.yardoc
|
14
|
+
/_yardoc/
|
15
|
+
/coverage/
|
16
|
+
/doc/
|
17
|
+
|
18
|
+
# rspec failure tracking
|
19
|
+
.rspec_status
|
20
|
+
scratch.rb
|
data/.yardopts
CHANGED
data/Gemfile
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require "rspec/core/rake_task"
|
3
|
+
require "yard"
|
4
|
+
require "redcarpet"
|
5
|
+
|
6
|
+
desc "run the specs"
|
7
|
+
RSpec::Core::RakeTask.new(:spec)
|
8
|
+
|
9
|
+
desc "run rspec showing backtrace"
|
10
|
+
RSpec::Core::RakeTask.new(:spec_trace) do |task|
|
11
|
+
task.rspec_opts = ['--backtrace']
|
12
|
+
end
|
13
|
+
|
14
|
+
desc "run rspec stopping on first fail, and show backtrace"
|
15
|
+
RSpec::Core::RakeTask.new(:spec_fast) do |task|
|
16
|
+
task.rspec_opts = ['--fail-fast', '--backtrace']
|
17
|
+
end
|
18
|
+
|
19
|
+
# default task name is yard
|
20
|
+
desc "Yard: generate all the documentation"
|
21
|
+
YARD::Rake::YardocTask.new(:doc) do |t|
|
22
|
+
#t.files = ['lib/**/*.rb']
|
23
|
+
end
|
24
|
+
|
25
|
+
task :default => [:spec]
|
26
|
+
task :rspec_trace => :spec_trace
|
27
|
+
task :rspec_fast => :spec_fast
|
data/eco-helpers.gemspec
CHANGED
@@ -22,15 +22,18 @@ Gem::Specification.new do |s|
|
|
22
22
|
#s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
23
23
|
s.require_paths = ["lib"]
|
24
24
|
|
25
|
+
s.add_development_dependency "bundler", "~> 1.16"
|
25
26
|
s.add_development_dependency "rspec", "~> 3", ">= 3.8"
|
27
|
+
s.add_development_dependency "rake", "~> 10.0"
|
26
28
|
s.add_development_dependency "yard", "~> 0.9", ">= 0.9.18"
|
27
29
|
s.add_development_dependency "redcarpet", "~> 3.4", ">= 3.4.0"
|
28
30
|
|
29
|
-
s.add_dependency 'ecoportal-api', '~> 0.4',
|
30
|
-
s.add_dependency '
|
31
|
-
s.add_dependency '
|
32
|
-
s.add_dependency '
|
33
|
-
s.add_dependency 'aws-sdk-
|
34
|
-
s.add_dependency 'aws-sdk-ses', '~> 1.14', '>= 1.14.0'
|
31
|
+
s.add_dependency 'ecoportal-api', '~> 0.4', '>= 0.4.2'
|
32
|
+
s.add_dependency 'thor', '~> 0', '>= 0.20'
|
33
|
+
s.add_dependency 'nokogiri', '~> 1.6', '>= 1.6.8'
|
34
|
+
s.add_dependency 'aws-sdk-s3', '~> 1.30', '>= 1.30.1'
|
35
|
+
s.add_dependency 'aws-sdk-ses', '~> 1.14', '>= 1.14.0'
|
35
36
|
s.add_dependency 'dotenv', '~> 2.6', '>= 2.6.0'
|
37
|
+
s.add_dependency 'faker', '~> 1', '>= 1.9'
|
38
|
+
s.add_dependency 'distribution', '~> 0.7', '>= 0.7.3'
|
36
39
|
end
|
data/lib/eco/api.rb
CHANGED
@@ -6,7 +6,8 @@ module Eco
|
|
6
6
|
end
|
7
7
|
|
8
8
|
require_relative 'api/common'
|
9
|
-
require_relative 'api/organization'
|
10
9
|
require_relative 'api/usecases'
|
10
|
+
require_relative 'api/policies'
|
11
|
+
require_relative 'api/organization'
|
11
12
|
require_relative 'api/session'
|
12
13
|
require_relative 'api/eco_faker'
|
@@ -7,12 +7,12 @@ module Eco
|
|
7
7
|
end
|
8
8
|
end
|
9
9
|
|
10
|
-
require_relative 'people/types'
|
11
10
|
require_relative 'people/person_modifier'
|
12
11
|
require_relative 'people/person_factory'
|
13
12
|
require_relative 'people/base_parser'
|
14
13
|
require_relative 'people/person_parser'
|
15
14
|
require_relative 'people/default_parsers'
|
15
|
+
require_relative 'people/person_entry_attribute_mapper'
|
16
16
|
require_relative 'people/person_entry'
|
17
17
|
require_relative 'people/entries'
|
18
18
|
require_relative 'people/entry_factory'
|
@@ -2,13 +2,43 @@ module Eco
|
|
2
2
|
module API
|
3
3
|
module Common
|
4
4
|
module People
|
5
|
+
|
6
|
+
# @example Example of usage:
|
7
|
+
# class ExampleParser < Eco::API::Common::People::BaseParser
|
8
|
+
# def process
|
9
|
+
# @parsers.define_attribute("example") do |parser|
|
10
|
+
# parser.def_parser do |str, deps|
|
11
|
+
# i = value.to_i rescue 0
|
12
|
+
# i +=5 if deps.dig(:sum_5)
|
13
|
+
# i
|
14
|
+
# end.def_serializer do |value|
|
15
|
+
# value.to_s
|
16
|
+
# end
|
17
|
+
# end
|
18
|
+
# end
|
19
|
+
# end
|
20
|
+
# pparser = Eco::API::Common::People::PersonParser.new
|
21
|
+
# ExampleParser.new(pparser)
|
22
|
+
# pparser.parse("example","3") # out: 3
|
23
|
+
# pparser.parse("example","3", deps: {sum_5: true}) # out: 8
|
24
|
+
# pparser.serialise("example, 8) # out: "8"
|
25
|
+
#
|
26
|
+
# Helper class to inherit from to ease the definition of attribute parsers in a `PersonParser` object,
|
27
|
+
# mostly used in for configuration loading.
|
5
28
|
class BaseParser
|
29
|
+
|
30
|
+
# @param parsers [Eco::API::Common::People::PersonParser] set of attribute parsers/serialisers.
|
31
|
+
# @param options [Hash] keyword arguments to be used in the `process` method of the child class.
|
6
32
|
def initialize(parsers, **options)
|
7
33
|
@parsers = parsers
|
8
34
|
@options = options
|
9
35
|
end
|
10
36
|
|
11
|
-
|
37
|
+
# Method to be overriden by the child class.
|
38
|
+
# @note this method is called to load the definition of the attribute parser/serializer.
|
39
|
+
def process
|
40
|
+
raise "Method needs to be overriden in the child class #{self.class}"
|
41
|
+
end
|
12
42
|
end
|
13
43
|
end
|
14
44
|
end
|
@@ -22,9 +22,12 @@ module Eco
|
|
22
22
|
|
23
23
|
SelectParser.new(self, select_hashes: select_hashes).process
|
24
24
|
BooleanParser.new(self).process
|
25
|
-
NumericParser.new(self
|
25
|
+
NumericParser.new(self).process
|
26
26
|
DateParser.new(self).process
|
27
27
|
MultiParser.new(self).process
|
28
|
+
|
29
|
+
# FORMAT PARSER
|
30
|
+
CSVParser.new(self).process
|
28
31
|
end
|
29
32
|
|
30
33
|
end
|
@@ -38,3 +41,4 @@ require_relative 'default_parsers/boolean_parser'
|
|
38
41
|
require_relative 'default_parsers/numeric_parser'
|
39
42
|
require_relative 'default_parsers/date_parser'
|
40
43
|
require_relative 'default_parsers/multi_parser'
|
44
|
+
require_relative 'default_parsers/csv_parser'
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module Eco
|
2
|
+
module API
|
3
|
+
module Common
|
4
|
+
module People
|
5
|
+
class DefaultParsers
|
6
|
+
class CSVParser < People::BaseParser
|
7
|
+
|
8
|
+
def process
|
9
|
+
@parsers.define_attribute(:csv, dependencies: @options) do |parser|
|
10
|
+
parser.def_parser do |data, deps|
|
11
|
+
arr_hash = []
|
12
|
+
CSV.parse(data, headers: true).each do |row|
|
13
|
+
row_hash = row.headers.uniq.each_with_object({}) do |attr, hash|
|
14
|
+
hash[attr] = row[attr]
|
15
|
+
end
|
16
|
+
arr_hash.push(row_hash)
|
17
|
+
end
|
18
|
+
arr_hash
|
19
|
+
end.def_serializer do |array_hash, deps|
|
20
|
+
arr_rows = []
|
21
|
+
unless array_hash.empty?
|
22
|
+
header = array_hash.first.keys
|
23
|
+
arr_rows = array_hash.map do |csv_row|
|
24
|
+
CSV::Row.new(header, csv_row.values_at(*header))
|
25
|
+
end
|
26
|
+
end
|
27
|
+
CSV::Table.new(arr_rows).to_csv
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -8,7 +8,6 @@ module Eco
|
|
8
8
|
def process
|
9
9
|
@parsers.define_attribute(:number, dependencies: @options) do |parser|
|
10
10
|
parser.def_parser do |value, deps|
|
11
|
-
#deps[:schema]&.fields[deps["attr"]].parse_text(value)
|
12
11
|
value = value.to_s.strip == "" ? nil : (value.to_f rescue nil)
|
13
12
|
end.def_serializer do |value|
|
14
13
|
value.is_a?(Array) ? value.map { |v| v.to_s } : value.to_s
|
@@ -2,26 +2,22 @@ module Eco
|
|
2
2
|
module API
|
3
3
|
module Common
|
4
4
|
module People
|
5
|
+
# Class meant to offer a _collection_ of entries, normally used to get parsed input data.
|
6
|
+
# @attr_reader entries [Array<Eco::API::Common::PersonEntry] a pure `Array` object.
|
5
7
|
class Entries < Eco::Language::Models::Collection
|
6
|
-
|
7
8
|
# build the shortcuts of Collection
|
8
9
|
attr_collection :id, :external_id, :email, :name, :supervisor_id
|
9
10
|
|
10
11
|
alias_method :entries, :to_a
|
11
12
|
|
12
|
-
include Common::People
|
13
|
-
|
14
13
|
def initialize(data = [], klass:, factory:)
|
15
14
|
super(data, klass: klass, factory: factory)
|
16
15
|
@caches_init = false
|
17
16
|
end
|
18
17
|
|
19
18
|
def [](id_or_ext)
|
20
|
-
|
19
|
+
id(id_or_ext) || external_id(id_or_ext)
|
21
20
|
end
|
22
|
-
#def []=(id, value)
|
23
|
-
# if self.id?(id)
|
24
|
-
#end
|
25
21
|
|
26
22
|
def id(*args)
|
27
23
|
attr('id', *args).first
|
@@ -34,16 +30,16 @@ module Eco
|
|
34
30
|
def entry(id: nil, external_id: nil, email: nil)
|
35
31
|
init_caches
|
36
32
|
pers = @by_id[id]&.first if id
|
37
|
-
pers = @by_external_id[external_id&.strip]&.first
|
38
|
-
pers = @by_email[email&.downcase.strip]&.first
|
33
|
+
pers = @by_external_id[external_id&.strip]&.first if !pers && !external_id.to_s.strip.empty?
|
34
|
+
pers = @by_email[email&.downcase.strip]&.first if !pers && !email.to_s.strip.empty?
|
39
35
|
pers = @by_external_id[email&.downcase.strip]&.first if !pers && !email.to_s.strip.empty?
|
40
36
|
pers
|
41
37
|
end
|
42
38
|
|
43
39
|
def find(object)
|
44
|
-
id
|
40
|
+
id = object.respond_to?("id")? object.send("id") : nil
|
45
41
|
external_id = object.respond_to?("external_id")? object.send("external_id") : nil
|
46
|
-
email
|
42
|
+
email = object.respond_to?("email")? object.send("email") : nil
|
47
43
|
entry(id: id, external_id: external_id, email: email)
|
48
44
|
end
|
49
45
|
|
@@ -55,18 +51,18 @@ module Eco
|
|
55
51
|
discarded = list.map do |person|
|
56
52
|
find(person)
|
57
53
|
end.compact
|
58
|
-
newFrom
|
54
|
+
newFrom to_a - discarded
|
59
55
|
end
|
60
56
|
|
61
57
|
def email_id_maps
|
62
|
-
|
58
|
+
email_present.group_by(:email).transform_values { |person| person.id }
|
63
59
|
end
|
64
60
|
|
65
61
|
def group_by_supervisor
|
66
|
-
|
62
|
+
to_h(:supervisor_id)
|
67
63
|
end
|
68
64
|
|
69
|
-
def to_h(attr)
|
65
|
+
def to_h(attr = "id")
|
70
66
|
super(attr || "id")
|
71
67
|
end
|
72
68
|
|
@@ -80,9 +76,9 @@ module Eco
|
|
80
76
|
|
81
77
|
def init_caches
|
82
78
|
return if @caches_init
|
83
|
-
@by_id
|
84
|
-
@by_external_id =
|
85
|
-
@by_email
|
79
|
+
@by_id = to_h
|
80
|
+
@by_external_id = to_h('external_id')
|
81
|
+
@by_email = to_h('email')
|
86
82
|
@caches_init = true
|
87
83
|
end
|
88
84
|
|
@@ -2,31 +2,119 @@ module Eco
|
|
2
2
|
module API
|
3
3
|
module Common
|
4
4
|
module People
|
5
|
+
# @attr_reader person_parser [Eco::API::Common::People::PersonParser] set of attribute, type and format parsers/serializers.
|
5
6
|
class EntryFactory
|
6
7
|
|
7
8
|
attr_reader :person_parser
|
8
9
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
10
|
+
# @param schema [Ecoportal::API::V1::PersonSchema] schema of person details that the parser will be based upon.
|
11
|
+
# @param person_parser [nil, Eco::API::Common::People::PersonParser] set of attribute, type and format parsers/serializers.
|
12
|
+
# @param attr_map [nil, Eco::Data::Mapper] attribute names mapper to translate external names into internal ones and _vice versa_.
|
13
|
+
# @param logger [Common::Session::Logger, ::Logger] object to manage logs.
|
14
|
+
def initialize(schema:, person_parser: nil, attr_map: nil, logger: ::Logger.new(IO::NULL))
|
15
|
+
fatal "Constructor needs a PersonSchema. Given: #{schema}" if !schema.is_a?(Ecoportal::API::V1::PersonSchema)
|
16
|
+
fatal "Expecting PersonParser. Given: #{person_parser}" if person_parser && !person_parser.is_a?(Eco::API::Common::People::PersonParser)
|
17
|
+
fatal "Expecting Mapper object. Given: #{fields_mapper}" if attr_map && !attr_map.is_a?(Eco::Data::Mapper)
|
13
18
|
|
14
19
|
@logger = logger
|
15
20
|
@schema = Ecoportal::API::V1::PersonSchema.new(JSON.parse(schema.doc.to_json))
|
16
21
|
|
17
22
|
default_parser = Eco::API::Common::People::DefaultParsers.new(schema: @schema)
|
18
|
-
@
|
23
|
+
@person_parser = default_parser.merge(person_parser)
|
19
24
|
|
20
|
-
@
|
25
|
+
@attr_map = attr_map
|
21
26
|
end
|
22
27
|
|
28
|
+
# key method to generate objects of `PersonEntry` that share dependencies via this `EntryFactory` environment.
|
29
|
+
# @note this method is necessary to make the factory object work as a if it was a class `PersonEntry` you can call `new` on.
|
30
|
+
# @param data [Array<Hash>] data to be parsed. The external hashed entry.
|
31
|
+
# @return [Eco::API::Common::People::PersonEntry]
|
23
32
|
def new(data, dependencies: {})
|
24
|
-
PersonEntry.new(
|
33
|
+
PersonEntry.new(
|
34
|
+
data,
|
35
|
+
person_parser: @person_parser,
|
36
|
+
attr_map: @attr_map,
|
37
|
+
dependencies: dependencies,
|
38
|
+
logger: @logger
|
39
|
+
)
|
25
40
|
end
|
26
41
|
|
27
|
-
|
28
|
-
|
42
|
+
# Helper that provides a collection of `Entries`, which in turn provides with further helpers to find and exclude entries.
|
43
|
+
# It accepts a `file:` and `format:` or `data:` but not both options together.
|
44
|
+
# @raise Exception
|
45
|
+
# - if you try to provide `data:` and `file:` at the same time.
|
46
|
+
# - if you provide `file:` but omit `format:`.
|
47
|
+
# - if the `format:` you provide is not a `Symbol`.
|
48
|
+
# - if there is no _parser/serializer_ defined for `format:`.
|
49
|
+
# @param data [Array<Hash>] data to be parsed. It cannot be used alongside with `file:`
|
50
|
+
# @param file [String] absolute or relative path to the input file. It cannot be used alongside with `data:`.
|
51
|
+
# @param format [Symbol] it must be used when you use the option `file:` (i.e. `:xml`, `:csv`), as it specifies the format of the input `file:`.
|
52
|
+
# @param encoding [String] optional parameter to read `file:` by expecting certain encoding.
|
53
|
+
# @return [Eco::API::Common::People::Entries] collection of `Eco::API::Common::People::PersonEntry`.
|
54
|
+
def entries(data: (no_data = true; nil), file: (no_file = true; nil), format: (no_format = true; nil), encoding: nil)
|
55
|
+
fatal("You should at least use data: or file:, but not both") if no_data == no_file
|
56
|
+
fatal("You must specify a valid format: (symbol) when you use file.") if file && no_format
|
57
|
+
fatal("Format should be a Symbol. Given '#{format}'") if format && !format.is_a?(Symbol)
|
58
|
+
fatal("There is no parser/serializer for format ':#{format.to_s}'") unless no_format || @person_parser.defined?(format)
|
59
|
+
|
60
|
+
if file
|
61
|
+
arr_hash = []
|
62
|
+
if Eco::API::Common::Session::FileManager.file_exists?(file)
|
63
|
+
encoding ||= Eco::API::Common::Session::FileManager.encoding(file)
|
64
|
+
file_content = File.read(file, encoding: encoding)
|
65
|
+
arr_hash = @person_parser.parse(format, file_content)
|
66
|
+
end
|
67
|
+
entries(data: arr_hash)
|
68
|
+
else
|
69
|
+
Entries.new(data, klass: PersonEntry, factory: self)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
# Helper that generates a file out of `data:`.
|
74
|
+
# @raise Exception
|
75
|
+
# - if you try to provide `data:` in the wrong format.
|
76
|
+
# - if you `file:` is empty.
|
77
|
+
# - if the `format:` you provide is not a `Symbol`.
|
78
|
+
# - if there is no _parser/serializer_ defined for `format:`.
|
79
|
+
# @param data [Eco::API::Organization::People] data to be parsed.
|
80
|
+
# @param file [String] absolute or relative path to the ouput file.
|
81
|
+
# @param format [Symbol] it specifies the format of the output `file:` (i.e. `:xml`, `:csv`). There must be a parser/serializer defined for it.
|
82
|
+
# @param encoding [String] optional parameter to geneate `file:` content by unsing certain encoding.
|
83
|
+
# @return [Void].
|
84
|
+
def export(data:, file: "export", format: :csv, encoding: "utf-8")
|
85
|
+
fatal("data: Expected Eco::API::Organization::People object. Given: #{data.class}") unless data.is_a?(Eco::API::Organization::People)
|
86
|
+
fatal("A file should be specified.") unless !file.to_s.strip.empty?
|
87
|
+
fatal("Format should be a Symbol. Given '#{format}'") if format && !format.is_a?(Symbol)
|
88
|
+
fatal("There is no parser/serializer for format ':#{format.to_s}'") unless @person_parser.defined?(format)
|
89
|
+
|
90
|
+
run = true
|
91
|
+
if Eco::API::Common::Session::FileManager.file_exists?(file)
|
92
|
+
print "The file '#{file}' already exists. Do you want to overwrite it? (Y/n): "
|
93
|
+
res = STDIN.gets.strip
|
94
|
+
run = ["y", "Y", ""].include?(res)
|
95
|
+
end
|
96
|
+
|
97
|
+
if run
|
98
|
+
deps = {"supervisor_id" => {people: data}}
|
99
|
+
|
100
|
+
data_entries = data.map do |person|
|
101
|
+
self.new(person, dependencies: deps).to_hash
|
102
|
+
end
|
103
|
+
|
104
|
+
File.open(file, "w", enconding: encoding) do |fd|
|
105
|
+
fd.write(@person_parser.serialize(format, data_entries))
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
end
|
110
|
+
|
111
|
+
private
|
112
|
+
|
113
|
+
def fatal(msg)
|
114
|
+
@logger.fatal(msg)
|
115
|
+
raise msg
|
29
116
|
end
|
117
|
+
|
30
118
|
end
|
31
119
|
|
32
120
|
end
|