eco-helpers 0.9.2 → 0.9.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (92) hide show
  1. checksums.yaml +4 -4
  2. data/lib/eco/api.rb +1 -0
  3. data/lib/eco/api/common.rb +1 -0
  4. data/lib/eco/api/common/class_helpers.rb +33 -0
  5. data/lib/eco/api/common/people.rb +1 -0
  6. data/lib/eco/api/common/people/person_attribute_parser.rb +52 -0
  7. data/lib/eco/api/common/people/person_entry_attribute_mapper.rb +2 -2
  8. data/lib/eco/api/common/people/person_parser.rb +16 -10
  9. data/lib/eco/api/common/version_patches.rb +2 -3
  10. data/lib/eco/api/common/version_patches/ecoportal_api.rb +4 -0
  11. data/lib/eco/api/common/version_patches/{base_model.rb → ecoportal_api/base_model.rb} +0 -0
  12. data/lib/eco/api/common/version_patches/{external_person.rb → ecoportal_api/external_person.rb} +0 -0
  13. data/lib/eco/api/common/version_patches/{internal_person.rb → ecoportal_api/internal_person.rb} +0 -0
  14. data/lib/eco/api/common/version_patches/hash.rb +2 -0
  15. data/lib/eco/api/common/version_patches/hash/deep_merge.rb +34 -0
  16. data/lib/eco/api/error.rb +133 -0
  17. data/lib/eco/api/error/handler.rb +19 -0
  18. data/lib/eco/api/error/handlers.rb +22 -0
  19. data/lib/eco/api/organization/people.rb +11 -11
  20. data/lib/eco/api/organization/policy_groups.rb +8 -0
  21. data/lib/eco/api/policies.rb +26 -3
  22. data/lib/eco/api/policies/policy.rb +4 -5
  23. data/lib/eco/api/session.rb +27 -18
  24. data/lib/eco/api/session/batch.rb +12 -6
  25. data/lib/eco/api/session/batch/errors.rb +134 -0
  26. data/lib/eco/api/session/batch/job.rb +213 -0
  27. data/lib/eco/api/session/batch/jobs.rb +72 -0
  28. data/lib/eco/api/session/batch/jobs_groups.rb +85 -0
  29. data/lib/eco/api/session/batch/status.rb +133 -0
  30. data/lib/eco/api/session/config.rb +36 -14
  31. data/lib/eco/api/session/config/base_config.rb +2 -0
  32. data/lib/eco/api/session/config/people.rb +8 -0
  33. data/lib/eco/api/session/config/post_launch.rb +58 -0
  34. data/lib/eco/api/session/config/workflow.rb +189 -0
  35. data/lib/eco/api/session/task.rb +49 -6
  36. data/lib/eco/api/usecases.rb +137 -2
  37. data/lib/eco/api/usecases/base_case.rb +20 -8
  38. data/lib/eco/api/usecases/base_io.rb +97 -0
  39. data/lib/eco/api/usecases/default_case.rb +19 -0
  40. data/lib/eco/api/usecases/default_cases.rb +2 -2
  41. data/lib/eco/api/usecases/default_cases/change_email_case.rb +2 -2
  42. data/lib/eco/api/usecases/default_cases/create_case.rb +2 -2
  43. data/lib/eco/api/usecases/default_cases/create_details_case.rb +2 -2
  44. data/lib/eco/api/usecases/default_cases/create_details_with_supervisor_case.rb +2 -2
  45. data/lib/eco/api/usecases/default_cases/delete_case.rb +2 -2
  46. data/lib/eco/api/usecases/default_cases/email_as_id_case.rb +2 -2
  47. data/lib/eco/api/usecases/default_cases/hris_case.rb +2 -2
  48. data/lib/eco/api/usecases/default_cases/new_email_case.rb +2 -2
  49. data/lib/eco/api/usecases/default_cases/new_id_case.rb +2 -2
  50. data/lib/eco/api/usecases/default_cases/recover_db_case.rb +11 -8
  51. data/lib/eco/api/usecases/default_cases/refresh_presets_case.rb +2 -2
  52. data/lib/eco/api/usecases/default_cases/reinvite_case.rb +2 -2
  53. data/lib/eco/api/usecases/default_cases/remove_account_case.rb +2 -2
  54. data/lib/eco/api/usecases/default_cases/reset_landing_page_case.rb +2 -2
  55. data/lib/eco/api/usecases/default_cases/set_default_tag_case.rb +2 -2
  56. data/lib/eco/api/usecases/default_cases/set_supervisor_case.rb +2 -2
  57. data/lib/eco/api/usecases/default_cases/switch_supervisor_case.rb +2 -2
  58. data/lib/eco/api/usecases/default_cases/to_csv_case.rb +4 -4
  59. data/lib/eco/api/usecases/default_cases/to_csv_detailed_case.rb +3 -3
  60. data/lib/eco/api/usecases/default_cases/update_case.rb +2 -2
  61. data/lib/eco/api/usecases/default_cases/update_details_case.rb +2 -2
  62. data/lib/eco/api/usecases/default_cases/upsert_case.rb +2 -2
  63. data/lib/eco/api/usecases/use_case.rb +23 -36
  64. data/lib/eco/api/usecases/use_case_chain.rb +14 -24
  65. data/lib/eco/api/usecases/use_case_io.rb +23 -75
  66. data/lib/eco/assets.rb +11 -11
  67. data/lib/eco/cli.rb +37 -0
  68. data/lib/eco/cli/config.rb +63 -1
  69. data/lib/eco/cli/config/default.rb +15 -0
  70. data/lib/eco/cli/config/default/filters.rb +69 -0
  71. data/lib/eco/cli/config/default/input.rb +21 -0
  72. data/lib/eco/cli/config/default/options.rb +47 -0
  73. data/lib/eco/cli/config/default/people.rb +39 -0
  74. data/lib/eco/cli/config/default/usecases.rb +63 -0
  75. data/lib/eco/cli/config/default/workflow.rb +86 -0
  76. data/lib/eco/cli/config/input.rb +40 -0
  77. data/lib/eco/cli/config/options_set.rb +35 -0
  78. data/lib/eco/cli/config/people_filters.rb +38 -0
  79. data/lib/eco/cli/config/use_cases.rb +87 -0
  80. data/lib/eco/cli/scripting/args_helpers.rb +10 -4
  81. data/lib/eco/cli/scripting/argument.rb +6 -0
  82. data/lib/eco/language/models/collection.rb +1 -0
  83. data/lib/eco/version.rb +1 -1
  84. metadata +32 -12
  85. data/lib/eco/api/policies/base_policy.rb +0 -14
  86. data/lib/eco/api/policies/used_policies.rb +0 -37
  87. data/lib/eco/api/session/batch_job.rb +0 -215
  88. data/lib/eco/api/session/batch_jobs.rb +0 -62
  89. data/lib/eco/api/session/batch_status.rb +0 -205
  90. data/lib/eco/api/session/job_groups.rb +0 -75
  91. data/lib/eco/api/usecases/use_group.rb +0 -124
  92. data/lib/eco/cli/config/options.rb +0 -11
@@ -1,62 +0,0 @@
1
- module Eco
2
- module API
3
- class Session
4
- class BatchJobs < API::Common::Session::BaseSession
5
- attr_reader :name
6
-
7
- def initialize(e, name:)
8
- super(e)
9
- @name = name
10
- reset
11
- end
12
-
13
- def reset
14
- @jobs = {}
15
- @callbacks = {}
16
- end
17
-
18
- def [](name)
19
- @jobs[name]
20
- end
21
-
22
- def exists?(name)
23
- @jobs.key?(name)
24
- end
25
-
26
- def job(name, type: nil, sets: nil, usecase: nil, &block)
27
- new(name, type: type, sets: sets, usecase: usecase, &block) unless exists?(name)
28
- self[name].tap do |job|
29
- block.call(job) if block
30
- end
31
- end
32
-
33
- def new(name, type:, sets:, usecase: nil)
34
- fatal "Can't create job named '#{name}' because it already exists." if exists?(name)
35
-
36
- BatchJob.new(enviro, name: name, type: type, sets: sets, usecase: usecase).tap do |job|
37
- @jobs[name] = job
38
- @callbacks[job] = Proc.new if block_given?
39
- end
40
- end
41
-
42
- def pending?
43
- @jobs.keys.any? do |key|
44
- @jobs[key].pending?
45
- end
46
- end
47
-
48
- def launch(simulate: false)
49
- group_status = {}
50
- @jobs.each do |name, job|
51
- group_status[job] = job_status = job.launch(simulate: simulate)
52
- callback = @callbacks[job]
53
- callback.call(job, job_status) if callback
54
- end
55
-
56
- return group_status
57
- end
58
-
59
- end
60
- end
61
- end
62
- end
@@ -1,205 +0,0 @@
1
- module Eco
2
- module API
3
- class Session
4
- class BatchStatus < Common::Session::BaseSession
5
- TYPES = [:exact, :search]
6
-
7
- attr_reader :source_queue
8
- attr_reader :queue, :method, :type
9
- attr_reader :root
10
-
11
- class << self
12
- def valid_type?(value)
13
- TYPES.include?(value)
14
- end
15
- end
16
-
17
- def initialize(e, queue:, method:, type: :exact)
18
- super(e)
19
- fatal("In batch operations you must batch an Enumerable. Received: #{queue}") unless queue && queue.is_a?(Enumerable)
20
-
21
- self.type = type
22
- @method = method
23
- @source_queue = queue
24
-
25
- que = queue.to_a
26
- que = queue if queue.respond_to?(:uniq)
27
- if que.length != que.uniq.length
28
- logger.warn("Please, review your entries-to-query builder, you have repeated entries")
29
- queue = que.uniq
30
- end
31
-
32
- @queue = queue
33
-
34
- @hash = @queue.each_with_index.map do |entry, i|
35
- [entry, i]
36
- end.to_h
37
- @responses = []
38
- @person_match = []
39
- @people_match = Array.new(@queue.length, [])
40
- end
41
-
42
- def root=(object)
43
- @root = object
44
- end
45
-
46
- def type=(value)
47
- fatal("Invalid :type '#{value}. You must specify type: as one of #{TYPES} ") unless self.class.valid_type?(value)
48
- @type = value
49
- end
50
-
51
- def [](key)
52
- @responses[to_index(key)]
53
- end
54
-
55
-
56
- def []=(key, response)
57
- @responses[to_index(key)] = response
58
- end
59
-
60
- def person(key)
61
- return self[key].result if success?(key)
62
- nil
63
- end
64
-
65
- def person_match(key)
66
- @person_match[to_index(key)]
67
- end
68
-
69
- def set_person_match(key, person)
70
- @person_match[to_index(key)] = person
71
- end
72
-
73
- def people_match(key)
74
- @people_match[to_index(key)]
75
- end
76
-
77
- def set_people_match(key, people)
78
- @people_match[to_index(key)] = people
79
- end
80
-
81
- def received?(key)
82
- !!self[key]
83
- end
84
-
85
- def success?(key)
86
- self[key]&.success?
87
- end
88
-
89
- def people
90
- fatal "This batch wasn't a 'get'. Can't obtain people without 'get' method" unless method == :get
91
- if type == :exact
92
- out = Array(queue.length)
93
- @responses.each_with_index do |respose, i|
94
- out[i] = response.result
95
- end
96
- elsif type == :search
97
- out = []
98
- queue.each_with_index.map do |entry, i|
99
- pers = person(entry)
100
- pers ||= person_match(entry)
101
- out += pers ? [pers] : people_match(entry)
102
- end
103
- end
104
- out
105
- end
106
-
107
- def error_queries
108
- queue.each_with_index.map do |query,i|
109
- unless self[i]
110
- msg = "Error: query with no response. You might have duplicated entries in your queue.\n"
111
- msg += "Queue length: #{queue.length}; Queue elements class: #{queue.first.class}\n"
112
- msg += "Query with no response. Person: #{person_ref(query)}\n"
113
- queue.map do |entry|
114
- if [:id, :external_id, :email].any? {|attr| (v = get_attr(entry, attr)) && v == get_attr(query, attr)}
115
- msg += "It could be this peson entry (idx: #{to_index(entry)}): #{person_ref(entry)}\n"
116
- end
117
- end
118
- raise msg
119
- end
120
-
121
- self[i].success? ? nil : query
122
- end.compact
123
- end
124
-
125
- def errors?
126
- queue.any? {|query| !self[query].success?}
127
- end
128
-
129
- def errors_count
130
- error_queries.length
131
- end
132
-
133
- def str_error(key)
134
- msg = ""
135
- unless success?(key)
136
- i = to_index(key)
137
- entry = queue.to_a[i]
138
- response = self[i]
139
- msg = "Error #{response.status}: #{response.body}\n"
140
- msg += "-- Failed to batch #{method} (entry #{i+1}). Person: #{person_ref(entry)}"
141
- end
142
- msg
143
- end
144
-
145
- def str_errors(sort: :by_status)
146
- queries = error_queries
147
- if sort == :by_status
148
- queries = error_queries.sort_by do |query|
149
- self[query].status
150
- end
151
- end
152
- strs = []
153
- if queries.length > 0
154
- strs = queries.map {|query| str_error(query)}
155
- end
156
- strs
157
- end
158
-
159
- def print_error(key)
160
- unless success?(key)
161
- logger.error(str_error(key))
162
- end
163
- end
164
-
165
- def print_errors(sort: :by_status)
166
- strs = str_errors(sort: sort)
167
- if strs.length > 0
168
- logger.info()
169
- logger.error("There were #{strs.length} errors:\n" + strs.join("\n"))
170
- else
171
- logger.info("There were no errors for the current batch '#{method}'!! ;)")
172
- end
173
- end
174
-
175
-
176
- private
177
-
178
- def person_ref(entry)
179
- "(id: '#{get_attr(entry, :id)}') '#{get_attr(entry, :name)}' ('#{get_attr(entry, :external_id)}': '#{get_attr(entry, :email)}')"
180
- end
181
-
182
- def get_attr(entry, attr)
183
- if entry.respond_to?(attr.to_sym)
184
- entry.public_send(attr.to_sym)
185
- elsif entry.is_a?(Hash)
186
- entry["#{attr}"]
187
- end
188
- end
189
-
190
- def to_index(key)
191
- key.is_a?(Integer) ? valid_index(index: key) : valid_index(entry: key)
192
- end
193
-
194
- def valid_index(index: nil, entry: nil)
195
- index ||= @hash[entry]
196
- unless index && index < @queue.length
197
- fatal "You must provide either the index or the original entry object of the batch"
198
- end
199
- index
200
- end
201
-
202
- end
203
- end
204
- end
205
- end
@@ -1,75 +0,0 @@
1
- module Eco
2
- module API
3
- class Session
4
- class JobGroups < API::Common::Session::BaseSession
5
- DELAY_BETWEEN_GROUPS = 2
6
-
7
- def initialize(e)
8
- super(e)
9
- reset
10
- end
11
-
12
- def reset
13
- @order = []
14
- @groups = {}
15
- @callbacks = {}
16
- end
17
-
18
- def [](name)
19
- @groups[name]
20
- end
21
-
22
- def exists?(name)
23
- @groups.key?(name)
24
- end
25
-
26
- def new(name, order: :last)
27
- fatal "Can't create job group named '#{name}' because it already exists." if exists?(name)
28
-
29
- BatchJobs.new(enviro, name: name).tap do |group|
30
- @groups[name] = group
31
-
32
- if order == :last
33
- @order.push(group)
34
- else
35
- @order.unshift(group)
36
- end
37
-
38
- @callbacks[group] = Proc.new if block_given?
39
- end
40
- end
41
-
42
- def pending?
43
- @groups.any? do |group|
44
- group.pending?
45
- end
46
- end
47
-
48
- def launch(simulate: false)
49
- groups_status = {}
50
- @order.each.with_index do |group, idx|
51
- groups_status[group] = group_status = group.launch(simulate: simulate)
52
- callback = @callbacks[group]
53
- callback.call(group, group_status) if callback
54
- JobGroups.counter(DELAY_BETWEEN_GROUPS) if !simulate && idx < @order.length - 1
55
- end
56
- return groups_status
57
- end
58
-
59
- def self.counter(seconds)
60
- puts "\n"
61
- while seconds + 1 > 0 do
62
- print " Waiting #{seconds}\r"
63
- $stdout.flush
64
- seconds -= 1
65
- sleep 1
66
- end
67
- print "#{" " * 40}"
68
- $stdout.flush
69
- puts ""
70
- end
71
-
72
- end
73
- end
74
- end
75
- end
@@ -1,124 +0,0 @@
1
- module Eco
2
- module API
3
- module UseCases
4
- class UseGroup
5
- include Enumerable
6
-
7
- def initialize()
8
- @usecases = {}
9
- @cache_init = false
10
- @cases_by_name = {}
11
- end
12
-
13
- def add(usecase)
14
- raise "Expected UseCase object. Given: #{usecase}" if !usecase || !usecase.is_a?(UseCase)
15
- name = usecase.name
16
- type = usecase.type
17
- puts "Warning: overriding '#{type.to_s}' case #{name}" if self.defined?(name, type: type)
18
- @cache_init = false
19
- @usecases[key(name, type)] = usecase
20
- usecase
21
- end
22
-
23
- def define(name, type:, &block)
24
- UseCase.new(name, type: type, root: self, &block).tap do |usecase|
25
- add(usecase)
26
- end
27
- end
28
-
29
- def defined?(name, type: nil)
30
- return @usecases.key?(key(name,type)) if type
31
- name?(name)
32
- end
33
-
34
- def name?(name)
35
- !!by_name[name]
36
- end
37
-
38
- def types(name)
39
- return nil if !name?(name)
40
- by_name[name].map { |usecase| usecase.type }
41
- end
42
-
43
- def case(name, type: nil)
44
- return @usecases[key(name, type)] if type
45
- return nil unless cases = by_name[name]
46
-
47
- msg = "There are cases of different types (#{types(name).map(&:to_s).join(", ")}) named '#{name}'. You should specify a correct type"
48
- raise msg if cases.length > 1
49
- cases.first
50
- end
51
-
52
- def names
53
- by_name.keys
54
- end
55
-
56
- # merges cases overriding self for exisint parsers
57
- def merge(cases)
58
- return self if !cases
59
- raise "Expected a UseGroup object. Given #{cases}" if !cases.is_a?(UseGroup)
60
- cases_hash = cases.to_h
61
-
62
- @usecases.merge!(cases_hash)
63
-
64
- cases_hash.transform_values do |usecase|
65
- usecase.root = self
66
- end
67
-
68
- @cache_init = false
69
- self
70
- end
71
-
72
- def length
73
- count
74
- end
75
-
76
- def empty?
77
- count == 0
78
- end
79
-
80
- def each(params: {}, &block)
81
- return to_enum(:each) unless block
82
- items.each(&block)
83
- end
84
-
85
- def items
86
- @usecases.values
87
- end
88
-
89
- protected
90
-
91
- def to_h
92
- @usecases
93
- end
94
-
95
- private
96
-
97
- def by_name
98
- init_caches
99
- @by_name
100
- end
101
-
102
- def init_caches
103
- return true if @cache_init
104
- @cache_init = true
105
- @by_name = @usecases.values.group_by { |usecase| usecase.name }
106
- end
107
-
108
- def key(name, type)
109
- name + type.to_s
110
- end
111
-
112
- def name(key)
113
- key&.split(":").first
114
- end
115
-
116
- def type
117
- key&.split(":").last&.to_sym
118
- end
119
-
120
- end
121
-
122
- end
123
- end
124
- end