eco-helpers 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (123) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +1 -0
  3. data/.rspec +3 -0
  4. data/README.md +20 -0
  5. data/eco-helpers.gemspec +34 -0
  6. data/lib/eco-helpers.rb +15 -0
  7. data/lib/eco/api.rb +13 -0
  8. data/lib/eco/api/common.rb +10 -0
  9. data/lib/eco/api/common/people.rb +17 -0
  10. data/lib/eco/api/common/people/base_parser.rb +16 -0
  11. data/lib/eco/api/common/people/default_parsers.rb +40 -0
  12. data/lib/eco/api/common/people/default_parsers/boolean_parser.rb +28 -0
  13. data/lib/eco/api/common/people/default_parsers/date_parser.rb +33 -0
  14. data/lib/eco/api/common/people/default_parsers/multi_parser.rb +33 -0
  15. data/lib/eco/api/common/people/default_parsers/numeric_parser.rb +23 -0
  16. data/lib/eco/api/common/people/default_parsers/select_parser.rb +29 -0
  17. data/lib/eco/api/common/people/entries.rb +120 -0
  18. data/lib/eco/api/common/people/person_entry.rb +380 -0
  19. data/lib/eco/api/common/people/person_factory.rb +114 -0
  20. data/lib/eco/api/common/people/person_modifier.rb +62 -0
  21. data/lib/eco/api/common/people/person_parser.rb +140 -0
  22. data/lib/eco/api/common/people/types.rb +47 -0
  23. data/lib/eco/api/common/session.rb +15 -0
  24. data/lib/eco/api/common/session/base_session.rb +46 -0
  25. data/lib/eco/api/common/session/environment.rb +47 -0
  26. data/lib/eco/api/common/session/file_manager.rb +90 -0
  27. data/lib/eco/api/common/session/logger.rb +105 -0
  28. data/lib/eco/api/common/session/mailer.rb +92 -0
  29. data/lib/eco/api/common/session/s3_uploader.rb +110 -0
  30. data/lib/eco/api/common/version_patches.rb +11 -0
  31. data/lib/eco/api/common/version_patches/external_person.rb +11 -0
  32. data/lib/eco/api/eco_faker.rb +59 -0
  33. data/lib/eco/api/organization.rb +13 -0
  34. data/lib/eco/api/organization/account.rb +23 -0
  35. data/lib/eco/api/organization/people.rb +118 -0
  36. data/lib/eco/api/organization/policy_groups.rb +51 -0
  37. data/lib/eco/api/organization/preferences.rb +28 -0
  38. data/lib/eco/api/organization/preferences_reference.json +23 -0
  39. data/lib/eco/api/organization/presets.rb +138 -0
  40. data/lib/eco/api/organization/presets_backup.rb +220 -0
  41. data/lib/eco/api/organization/presets_values.json +10 -0
  42. data/lib/eco/api/organization/tag_tree.rb +134 -0
  43. data/lib/eco/api/organization_old.rb +73 -0
  44. data/lib/eco/api/session.rb +180 -0
  45. data/lib/eco/api/session/batch.rb +132 -0
  46. data/lib/eco/api/session/batch_job.rb +152 -0
  47. data/lib/eco/api/session/batch_jobs.rb +131 -0
  48. data/lib/eco/api/session/batch_status.rb +138 -0
  49. data/lib/eco/api/session/task.rb +92 -0
  50. data/lib/eco/api/session_config.rb +179 -0
  51. data/lib/eco/api/session_config/api.rb +47 -0
  52. data/lib/eco/api/session_config/apis.rb +78 -0
  53. data/lib/eco/api/session_config/files.rb +30 -0
  54. data/lib/eco/api/session_config/logger.rb +54 -0
  55. data/lib/eco/api/session_config/mailer.rb +65 -0
  56. data/lib/eco/api/session_config/people.rb +89 -0
  57. data/lib/eco/api/session_config/s3_bucket.rb +62 -0
  58. data/lib/eco/api/session_config/use_cases.rb +30 -0
  59. data/lib/eco/api/usecases.rb +12 -0
  60. data/lib/eco/api/usecases/base_case.rb +14 -0
  61. data/lib/eco/api/usecases/case_data.rb +13 -0
  62. data/lib/eco/api/usecases/default_cases.rb +53 -0
  63. data/lib/eco/api/usecases/default_cases/change_email_case.rb +47 -0
  64. data/lib/eco/api/usecases/default_cases/create_details_case.rb +29 -0
  65. data/lib/eco/api/usecases/default_cases/create_details_with_supervisor_case.rb +49 -0
  66. data/lib/eco/api/usecases/default_cases/delete_case.rb +20 -0
  67. data/lib/eco/api/usecases/default_cases/email_as_id_case.rb +24 -0
  68. data/lib/eco/api/usecases/default_cases/hris_case.rb +67 -0
  69. data/lib/eco/api/usecases/default_cases/new_email_case.rb +26 -0
  70. data/lib/eco/api/usecases/default_cases/new_id_case.rb +26 -0
  71. data/lib/eco/api/usecases/default_cases/refresh_presets.rb +25 -0
  72. data/lib/eco/api/usecases/default_cases/reinvite_case.rb +22 -0
  73. data/lib/eco/api/usecases/default_cases/remove_account_case.rb +36 -0
  74. data/lib/eco/api/usecases/default_cases/reset_landing_page_case.rb +24 -0
  75. data/lib/eco/api/usecases/default_cases/set_default_tag_case.rb +44 -0
  76. data/lib/eco/api/usecases/default_cases/set_supervisor_case.rb +39 -0
  77. data/lib/eco/api/usecases/default_cases/to_csv_case.rb +36 -0
  78. data/lib/eco/api/usecases/default_cases/update_details_case.rb +30 -0
  79. data/lib/eco/api/usecases/default_cases/upsert_account_case.rb +35 -0
  80. data/lib/eco/api/usecases/use_case.rb +177 -0
  81. data/lib/eco/api/usecases/use_group.rb +104 -0
  82. data/lib/eco/cli.rb +9 -0
  83. data/lib/eco/cli/input.rb +109 -0
  84. data/lib/eco/cli/input_multi.rb +137 -0
  85. data/lib/eco/cli/root.rb +8 -0
  86. data/lib/eco/cli/session.rb +9 -0
  87. data/lib/eco/cli/session/batch.rb +9 -0
  88. data/lib/eco/common.rb +7 -0
  89. data/lib/eco/common/base_cli.rb +116 -0
  90. data/lib/eco/common/language.rb +9 -0
  91. data/lib/eco/data.rb +9 -0
  92. data/lib/eco/data/crypto.rb +7 -0
  93. data/lib/eco/data/crypto/encryption.rb +318 -0
  94. data/lib/eco/data/files.rb +10 -0
  95. data/lib/eco/data/files/directory.rb +93 -0
  96. data/lib/eco/data/files/file_pattern.rb +32 -0
  97. data/lib/eco/data/files/helpers.rb +90 -0
  98. data/lib/eco/data/mapper.rb +54 -0
  99. data/lib/eco/data/random.rb +10 -0
  100. data/lib/eco/data/random/distribution.rb +133 -0
  101. data/lib/eco/data/random/fake.rb +320 -0
  102. data/lib/eco/data/random/values.rb +80 -0
  103. data/lib/eco/language.rb +12 -0
  104. data/lib/eco/language/curry.rb +28 -0
  105. data/lib/eco/language/hash_transform.rb +68 -0
  106. data/lib/eco/language/hash_transform_modifier.rb +114 -0
  107. data/lib/eco/language/match.rb +30 -0
  108. data/lib/eco/language/match_modifier.rb +190 -0
  109. data/lib/eco/language/models.rb +11 -0
  110. data/lib/eco/language/models/attribute_parser.rb +38 -0
  111. data/lib/eco/language/models/collection.rb +181 -0
  112. data/lib/eco/language/models/modifier.rb +68 -0
  113. data/lib/eco/language/models/wrap.rb +114 -0
  114. data/lib/eco/language/values_at.rb +159 -0
  115. data/lib/eco/lexic/dictionary.rb +33 -0
  116. data/lib/eco/lexic/dictionary/dictionary.txt +355484 -0
  117. data/lib/eco/lexic/dictionary/tags.json +38 -0
  118. data/lib/eco/scripting.rb +30 -0
  119. data/lib/eco/scripting/README.md +11 -0
  120. data/lib/eco/scripting/arguments.rb +40 -0
  121. data/lib/eco/tester.rb +97 -0
  122. data/lib/eco/version.rb +3 -0
  123. metadata +325 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bb418abf2d0a0a3bcf9d4f95c9696b9d6347f006
4
+ data.tar.gz: 978e4c69e536b192722df51e8eb361d835a708ae
5
+ SHA512:
6
+ metadata.gz: 72efbdbf8703f5be28ef9fb223c3886d83ed8a3d72d5ff58efe7bb59ce6cc5f82916cef8cdac685c01c7f1d70d70a6cf43fe7ddc35bfffe3754b98ded18e976e
7
+ data.tar.gz: b40e206e5e3d633e8dec159342e1df20b2424e378e4418cdd4820c675335d34f94925a3865ff12c2c10f1b0836178f6adab4cbd036e931a2eb6ffd5e9f082e25
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/README.md ADDED
@@ -0,0 +1,20 @@
1
+ # Script API Helpers (script_api_helpers)
2
+
3
+ Some helpers to simplify things:
4
+
5
+ * managing **files**: keep your updates with a timestamp, find the last version, encrypt/decrypt the data, shortcuts to avoid messing around with language specificities,...
6
+ generate fake data: different topics, use different distributions,...
7
+ * API:
8
+ * custom multiple *presets* profiles,
9
+ * get the highest of each flag among different custom presets
10
+
11
+ To do:
12
+
13
+ * more **fake data** features: link api details field, randomize your policy groups or apply a fixed distribution based on location
14
+ * API:
15
+ * **map** different **account properties**, such as policy groups to presets or preferences
16
+ * use an assistant to map **CSV** columns to the correct account or details properties
17
+ * use batch helpers that store and manage your update files and provide log errors, or use them as an example
18
+ * validate tagtree tags
19
+ * create configuration files for orgs you manage the **HRIS** (a weekly feed will become a one time execution script)
20
+ * create **unit tests** to check if the API behaviour meets the specifications and launch them periodically
@@ -0,0 +1,34 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+
4
+ require "./lib/eco/version"
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "eco-helpers"
8
+ s.version = Eco::VERSION
9
+ s.platform = Gem::Platform::RUBY
10
+ s.authors = ["Oscar Segura"]
11
+ s.email = ["oscar@ecoportal.co.nz"]
12
+ s.date = %q{2018-09-05}
13
+ s.summary = %q{eco-helpers to manage people api cases}
14
+ s.homepage = "https://www.ecoportal.com"
15
+ s.licenses = %w[MIT]
16
+
17
+ s.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+
21
+ #s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
22
+ #s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
23
+ s.require_paths = ["lib"]
24
+
25
+ s.add_development_dependency "rspec", "~> 3", ">= 3.8"
26
+
27
+ s.add_dependency 'ecoportal-api', '~> 0.3', '>= 0.3.6'
28
+ s.add_dependency 'faker', '~> 1', '>= 1.9'
29
+ s.add_dependency 'distribution', '~> 0.7', '>= 0.7.3'
30
+ s.add_dependency 'thor', '~> 0', '>= 0.20'
31
+ s.add_dependency 'aws-sdk-s3', '~> 1.30', '>= 1.30.1'
32
+ s.add_dependency 'aws-sdk-ses', '~> 1.14', '>= 1.14.0'
33
+ s.add_dependency 'dotenv', '~> 2.6', '>= 2.6.0'
34
+ end
@@ -0,0 +1,15 @@
1
+ require 'csv'
2
+ require 'json' # native
3
+ require 'pp'
4
+
5
+
6
+ module Eco
7
+ end
8
+
9
+ require_relative 'eco/language'
10
+ require_relative 'eco/common'
11
+ require_relative 'eco/scripting'
12
+ require_relative 'eco/data'
13
+ require_relative 'eco/api'
14
+ require_relative 'eco/tester'
15
+ require_relative 'eco/cli'
data/lib/eco/api.rb ADDED
@@ -0,0 +1,13 @@
1
+ require 'ecoportal/api'
2
+
3
+ module Eco
4
+ module API
5
+ end
6
+ end
7
+
8
+ require_relative 'api/common'
9
+ require_relative 'api/organization'
10
+ require_relative 'api/session'
11
+ require_relative 'api/usecases'
12
+ require_relative 'api/session_config'
13
+ require_relative 'api/eco_faker'
@@ -0,0 +1,10 @@
1
+ module Eco
2
+ module API
3
+ module Common
4
+ end
5
+ end
6
+ end
7
+
8
+ require_relative 'common/version_patches'
9
+ require_relative 'common/people'
10
+ require_relative 'common/session'
@@ -0,0 +1,17 @@
1
+ module Eco
2
+ module API
3
+ module Common
4
+ module People
5
+ end
6
+ end
7
+ end
8
+ end
9
+
10
+ require_relative 'people/types'
11
+ require_relative 'people/person_modifier'
12
+ require_relative 'people/person_factory'
13
+ require_relative 'people/base_parser'
14
+ require_relative 'people/person_parser'
15
+ require_relative 'people/default_parsers'
16
+ require_relative 'people/person_entry'
17
+ require_relative 'people/entries'
@@ -0,0 +1,16 @@
1
+ module Eco
2
+ module API
3
+ module Common
4
+ module People
5
+ class BaseParser
6
+ def initialize(parsers, **options)
7
+ @parsers = parsers
8
+ @options = options
9
+ end
10
+
11
+ def process; end
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,40 @@
1
+ module Eco
2
+ module API
3
+ module Common
4
+ module People
5
+ class DefaultParsers < PersonParser
6
+
7
+ def initialize(*args)
8
+ super(*args)
9
+ define_defaults
10
+ end
11
+
12
+ private
13
+
14
+ def define_defaults
15
+ # Select Options
16
+ select_hashes = @schema.fields.map do |fld|
17
+ if fld.type == "select"
18
+ options_hash = fld.options.map { |v| [v.downcase.strip, v] }.to_h
19
+ [fld.alt_id, options_hash]
20
+ end
21
+ end.compact.to_h
22
+
23
+ SelectParser.new(self, select_hashes: select_hashes).process
24
+ BooleanParser.new(self).process
25
+ NumericParser.new(self, schema: @schema).process
26
+ DateParser.new(self).process
27
+ MultiParser.new(self).process
28
+ end
29
+
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+
36
+ require_relative 'default_parsers/select_parser'
37
+ require_relative 'default_parsers/boolean_parser'
38
+ require_relative 'default_parsers/numeric_parser'
39
+ require_relative 'default_parsers/date_parser'
40
+ require_relative 'default_parsers/multi_parser'
@@ -0,0 +1,28 @@
1
+ module Eco
2
+ module API
3
+ module Common
4
+ module People
5
+ class DefaultParsers
6
+ class BooleanParser < People::BaseParser
7
+
8
+ def process
9
+ @parsers.define_attribute(:boolean) do |parser|
10
+ parser.def_parser do |value|
11
+ value.is_a?(Array) ? value.map { |v| truthy(v) } : truthy(value)
12
+ end.def_serializer do |value|
13
+ value.to_s
14
+ end
15
+ end
16
+ end
17
+
18
+ private
19
+
20
+ def truthy (value)
21
+ %w[true y yes x].include?(value.to_s&.downcase)
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,33 @@
1
+ module Eco
2
+ module API
3
+ module Common
4
+ module People
5
+ class DefaultParsers
6
+ class DateParser < People::BaseParser
7
+
8
+ def process
9
+ @parsers.define_attribute(:date) do |parser|
10
+ parser.def_parser do |value|
11
+ value.is_a?(Array) ? value.map { |v| parse_date(v) } : parse_date(value)
12
+ end.def_serializer do |value|
13
+ value.is_a?(Array) ? value.map { |d| d && d.strftime('%Y-%m-%d') } : value && value.strftime('%Y-%m-%d')
14
+ end
15
+ end
16
+ end
17
+
18
+ private
19
+
20
+ def parse_date(value)
21
+ begin
22
+ (value.to_s.empty?) ? nil : Time.parse(value)
23
+ rescue
24
+ nil
25
+ end
26
+ end
27
+
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,33 @@
1
+ module Eco
2
+ module API
3
+ module Common
4
+ module People
5
+ class DefaultParsers
6
+ class MultiParser < People::BaseParser
7
+
8
+ def process
9
+ @parsers.define_attribute(:multiple) do |parser|
10
+ parser.def_parser do |value|
11
+ return value if value.is_a?(Array)
12
+ value = value.split("|") if value&.include?("|")
13
+ into_a(value)
14
+ end.def_serializer do |value|
15
+ into_a(value).join("|")
16
+ end
17
+ end
18
+ end
19
+
20
+ private
21
+
22
+ def into_a(value)
23
+ value = [] if value == nil
24
+ value = [].push(value) unless value.is_a?(Array)
25
+ value
26
+ end
27
+
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,23 @@
1
+ module Eco
2
+ module API
3
+ module Common
4
+ module People
5
+ class DefaultParsers
6
+ class NumericParser < People::BaseParser
7
+
8
+ def process
9
+ @parsers.define_attribute(:number, dependencies: @options) do |parser|
10
+ parser.def_parser do |value, deps|
11
+ deps[:schema]&.fields[deps["attr"]].parse_text(value)
12
+ end.def_serializer do |value|
13
+ value.is_a?(Array) ? value.map { |v| v.to_s } : value.to_s
14
+ end
15
+ end
16
+ end
17
+
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,29 @@
1
+ module Eco
2
+ module API
3
+ module Common
4
+ module People
5
+ class DefaultParsers
6
+ class SelectParser < People::BaseParser
7
+
8
+ def process
9
+ @parsers.define_attribute(:select, dependencies: @options) do |parser|
10
+ parser.def_parser do |value, deps|
11
+ options_hash = deps[:select_hashes][deps["attr"]]
12
+ next value if !options_hash
13
+ if value.is_a?(Array)
14
+ value.map { |v| options_hash[v&.downcase.strip] || v }
15
+ elsif value
16
+ options_hash[value&.downcase.strip] || value
17
+ end
18
+ end.def_serializer do |value|
19
+ value
20
+ end
21
+ end
22
+ end
23
+
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,120 @@
1
+ module Eco
2
+ module API
3
+ module Common
4
+ module People
5
+ class EntryFactory
6
+
7
+ attr_reader :person_parser
8
+
9
+ def initialize(schema:, person_parser: nil, fields_mapper: nil, logger: ::Logger.new(IO::NULL))
10
+ raise "Constructor needs a PersonSchema. Given: #{schema}" if !schema.is_a?(Ecoportal::API::V1::PersonSchema)
11
+ raise "Expecting PersonParser. Given: #{person_parser}" if person_parser && !person_parser.is_a?(Eco::API::Common::People::PersonParser)
12
+ raise "Expecting Mapper object. Given: #{fields_mapper}" if fields_mapper && !fields_mapper.is_a?(Eco::Data::Mapper)
13
+
14
+ @logger = logger
15
+ @schema = Ecoportal::API::V1::PersonSchema.new(JSON.parse(schema.doc.to_json))
16
+
17
+ default_parser = Eco::API::Common::People::DefaultParsers.new(schema: @schema)
18
+ @parser = default_parser.merge(person_parser)
19
+
20
+ @fields_mapper = fields_mapper
21
+ end
22
+
23
+ def new(data, dependencies: {})
24
+ PersonEntry.new(data, parser: @parser, mapper: @fields_mapper, dependencies: dependencies, logger: @logger)
25
+ end
26
+
27
+ def entries(data)
28
+ Entries.new(data, klass: PersonEntry, factory: self)
29
+ end
30
+ end
31
+
32
+ class Entries < Eco::Language::Models::Collection
33
+
34
+ # build the shortcuts of Collection
35
+ attr_collection :id, :external_id, :email, :name, :supervisor_id
36
+
37
+ alias_method :entries, :to_a
38
+
39
+ include Common::People
40
+
41
+ def initialize(data = [], klass:, factory:)
42
+ super(data, klass: klass, factory: factory)
43
+ @caches_init = false
44
+ end
45
+
46
+ def [](id_or_ext)
47
+ self.id(id_or_ext) || self.external_id(id_or_ext)
48
+ end
49
+ #def []=(id, value)
50
+ # if self.id?(id)
51
+ #end
52
+
53
+ def id(*args)
54
+ attr('id', *args).first
55
+ end
56
+
57
+ def external_id(*args)
58
+ attr('external_id', *args).first
59
+ end
60
+
61
+ def entry(id: nil, external_id: nil, email: nil)
62
+ init_caches
63
+ pers = @by_id[id]&.first if id
64
+ pers = @by_external_id[external_id&.strip]&.first if !pers && !external_id.to_s.strip.empty?
65
+ pers = @by_email[email&.downcase.strip]&.first if !pers && !email.to_s.strip.empty?
66
+ pers = @by_external_id[email&.downcase.strip]&.first if !pers && !email.to_s.strip.empty?
67
+ pers
68
+ end
69
+
70
+ def find(object)
71
+ id = object.respond_to?("id")? object.send("id") : nil
72
+ external_id = object.respond_to?("external_id")? object.send("external_id") : nil
73
+ email = object.respond_to?("email")? object.send("email") : nil
74
+ entry(id: id, external_id: external_id, email: email)
75
+ end
76
+
77
+ def exclude(object)
78
+ exclude_people(into_a(object))
79
+ end
80
+
81
+ def exclude_people(list)
82
+ discarded = list.map do |person|
83
+ find(person)
84
+ end.compact
85
+ newFrom self.to_a - discarded
86
+ end
87
+
88
+ def email_id_maps
89
+ self.email_present.group_by(:email).transform_values { |person| person.id }
90
+ end
91
+
92
+ def group_by_supervisor
93
+ self.to_h(:supervisor_id)
94
+ end
95
+
96
+ def to_h(attr)
97
+ super(attr || "id")
98
+ end
99
+
100
+ protected
101
+
102
+ def on_change
103
+ @caches_init = false
104
+ end
105
+
106
+ private
107
+
108
+ def init_caches
109
+ return if @caches_init
110
+ @by_id = self.to_h('id')
111
+ @by_external_id = self.to_h('external_id')
112
+ @by_email = self.to_h('email')
113
+ @caches_init = true
114
+ end
115
+
116
+ end
117
+ end
118
+ end
119
+ end
120
+ end