eco-helpers 0.6.13 → 0.6.15

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. checksums.yaml +4 -4
  2. data/eco-helpers.gemspec +1 -1
  3. data/lib/eco/api.rb +1 -2
  4. data/lib/eco/api/common/people/person_entry.rb +1 -1
  5. data/lib/eco/api/common/people/person_factory.rb +3 -3
  6. data/lib/eco/api/common/session/base_session.rb +14 -2
  7. data/lib/eco/api/common/session/environment.rb +9 -7
  8. data/lib/eco/api/common/version_patches/external_person.rb +16 -2
  9. data/lib/eco/api/common/version_patches/internal_person.rb +17 -0
  10. data/lib/eco/api/session.rb +13 -19
  11. data/lib/eco/api/session/batch.rb +4 -17
  12. data/lib/eco/api/session/batch_job.rb +4 -13
  13. data/lib/eco/api/session/{job_group.rb → batch_jobs.rb} +6 -8
  14. data/lib/eco/api/session/batch_status.rb +3 -7
  15. data/lib/eco/api/session/config.rb +186 -0
  16. data/lib/eco/api/session/config/api.rb +49 -0
  17. data/lib/eco/api/session/config/apis.rb +91 -0
  18. data/lib/eco/api/session/config/files.rb +32 -0
  19. data/lib/eco/api/session/config/logger.rb +56 -0
  20. data/lib/eco/api/session/config/mailer.rb +67 -0
  21. data/lib/eco/api/session/config/people.rb +95 -0
  22. data/lib/eco/api/session/config/s3_storage.rb +64 -0
  23. data/lib/eco/api/session/config/use_cases.rb +33 -0
  24. data/lib/eco/api/session/job_groups.rb +4 -6
  25. data/lib/eco/api/session/task.rb +2 -13
  26. data/lib/eco/version.rb +1 -1
  27. metadata +18 -11
  28. data/lib/eco/api/session_config.rb +0 -187
  29. data/lib/eco/api/session_config/api.rb +0 -47
  30. data/lib/eco/api/session_config/apis.rb +0 -89
  31. data/lib/eco/api/session_config/files.rb +0 -30
  32. data/lib/eco/api/session_config/logger.rb +0 -54
  33. data/lib/eco/api/session_config/mailer.rb +0 -65
  34. data/lib/eco/api/session_config/people.rb +0 -93
  35. data/lib/eco/api/session_config/s3_storage.rb +0 -62
  36. data/lib/eco/api/session_config/use_cases.rb +0 -31
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: '097d36be4a38f2e209bb2b2d80861d17a45c83d7'
4
- data.tar.gz: db938dd522faf091e76714c7c4b58f4591144f02
3
+ metadata.gz: 12bd692f0b9d72b4116b42bfba1159f32d0be43f
4
+ data.tar.gz: 9e5d0d188d6a5b2ea908e96c2ac5ff17e8b322a4
5
5
  SHA512:
6
- metadata.gz: a2fdfafe595b112bc81f4a6fe1e7fbbb32ed9ef306a54a40b1bb96849101a81ec19bd58c6240150f06f69c425d667b3f59d173bc98424c0878d8ece17231dc99
7
- data.tar.gz: 55a8f3879f996db311974429e9ae1b111448cc51b4e676b9b714701fbfff05d07d0ead0011425fdad0d47d72d96c48be6cb987e127b1d4ac902e8b2ab0e273f2
6
+ metadata.gz: e3d1a520ec06cb397d3a24b3daf3113d1a4766db082649fc0317cbd29bf0088374a1380c66e6724159fff070cbdacbf71fc7157649cc913c86a21df941513271
7
+ data.tar.gz: 30feb556fd73cf5c3fca8c39769ba2e0749394d6e1c0de4e608151ed8d6f0f4fa19da76dc2737f2eef146ed86e1a8aafddc386f0d9c335674458625f4fce4deb
@@ -26,7 +26,7 @@ Gem::Specification.new do |s|
26
26
  s.add_development_dependency "yard", "~> 0.9", ">= 0.9.18"
27
27
  s.add_development_dependency "redcarpet", "~> 3.4", ">= 3.4.0"
28
28
 
29
- s.add_dependency 'ecoportal-api', '~> 0.3', '>= 0.3.6'
29
+ s.add_dependency 'ecoportal-api', '~> 0.3', '>= 0.3.6', '< 0.4.0'
30
30
  s.add_dependency 'faker', '~> 1', '>= 1.9'
31
31
  s.add_dependency 'distribution', '~> 0.7', '>= 0.7.3'
32
32
  s.add_dependency 'thor', '~> 0', '>= 0.20'
@@ -7,7 +7,6 @@ end
7
7
 
8
8
  require_relative 'api/common'
9
9
  require_relative 'api/organization'
10
- require_relative 'api/session'
11
10
  require_relative 'api/usecases'
12
- require_relative 'api/session_config'
11
+ require_relative 'api/session'
13
12
  require_relative 'api/eco_faker'
@@ -237,7 +237,7 @@ module Eco
237
237
  unserialized_entry.merge(serial_attrs)
238
238
  end
239
239
 
240
- # returns entry with interal names and values
240
+ # returns entry with internal names and values
241
241
  def internal_entry(data)
242
242
  return parsed_entry(data) if parsing?
243
243
  unserialized_entry(data)
@@ -10,9 +10,9 @@ module Eco
10
10
 
11
11
  def initialize(person: {}, schema: {}, account: {}, modifier: Common::People::PersonModifier.new)
12
12
  @modifier = Common::People::PersonModifier.new(modifier)
13
- @person = person
13
+ @person = person
14
14
  @account = account
15
- @schema = schema
15
+ @schema = schema
16
16
  @schema_attrs = @schema&.fields&.map { |fld| fld.alt_id }
17
17
  end
18
18
 
@@ -21,7 +21,7 @@ module Eco
21
21
  return PersonFactory.new(person: person, schema: @schema, modifier: in_raw_modifier).new unless !person
22
22
  person = klass.new(person_hash(@person))
23
23
  person.account = account_hash(@account) if @modifier.add_account? && @modifier.internal?
24
- person.details = details_hash(@schema) unless @modifier.no_details?
24
+ person.add_details(@schema) unless @modifier.no_details?
25
25
  person
26
26
  end
27
27
 
@@ -6,6 +6,7 @@ module Eco
6
6
 
7
7
  include Common::People
8
8
 
9
+ attr_accessor :session
9
10
  attr_accessor :environment, :config
10
11
  alias_method :enviro, :environment
11
12
  alias_method :enviro=, :environment=
@@ -14,8 +15,13 @@ module Eco
14
15
  alias_method :fm, :file_manager
15
16
 
16
17
  def initialize(e)
17
- e = Environment.new(e) if !e.is_a?(Environment)
18
- self.environment = e if e.is_a?(Environment)
18
+ raise "Expected object Eco::API::Common::Session::Environment. Given: #{e.class}" unless e.is_a?(Environment)
19
+ #e = Environment.new(e) if !e.is_a?(Environment)
20
+ self.environment = e #if e.is_a?(Environment)
21
+ end
22
+
23
+ def session
24
+ enviro.session
19
25
  end
20
26
 
21
27
  def environment=(value)
@@ -39,6 +45,12 @@ module Eco
39
45
  enviro.logger
40
46
  end
41
47
 
48
+ # TODO: paremeter for the exception
49
+ def fatal(msg)
50
+ logger.fatal(msg)
51
+ raise msg
52
+ end
53
+
42
54
  end
43
55
  end
44
56
  end
@@ -6,18 +6,20 @@ module Eco
6
6
 
7
7
  include Common::People
8
8
 
9
- attr_reader :config
9
+ attr_reader :config, :session
10
10
  attr_reader :api #, :host, :version
11
11
  attr_reader :file_manager, :logger
12
12
 
13
13
  alias_method :fm, :file_manager
14
14
 
15
- def initialize(init = {})
15
+ def initialize(init = {}, session:)
16
16
  init = init.conf if init.is_a?(Environment)
17
- msg = "Wrong Session::Environment initializer. Expected Hash or Environment object. Given: #{init}"
18
- raise msg unless init.is_a?(Eco::API::SessionConfig)
17
+ msg = "Expected object Eco::API::Session::Config or Environment. Given: #{init}"
18
+ raise msg unless init.is_a?(Eco::API::Session::Config)
19
+ raise "Expected an Eco::API::Session object. Given: #{session}" if session && !session.is_a?(Eco::API::Session)
19
20
 
20
- @config = init
21
+ @config = init
22
+ @session = session
21
23
  @file_manager = Eco::API::Common::Session::FileManager.new(enviro: self)
22
24
  @logger = Eco::API::Common::Session::Logger.new(enviro: self)
23
25
  new_api
@@ -27,9 +29,9 @@ module Eco
27
29
  return nil unless config.apis.active_api
28
30
 
29
31
  log_connection = config.logger.log_connection?
30
- log = log_connection ? @logger : ::Logger.new(IO::NULL)
32
+ logg = log_connection ? logger : ::Logger.new(IO::NULL)
31
33
 
32
- api = config.api(log)
34
+ api = config.api(logg)
33
35
 
34
36
  unless log_connection
35
37
  logger.info("Created connection pointing to '#{config.apis.active_api.host}' in '#{config.apis.active_api.mode}' mode")
@@ -1,10 +1,24 @@
1
1
  module Ecoportal
2
2
  module API
3
3
  class V1
4
- class Person < Common::BaseModel
5
- def sync
4
+ class Person
5
+
6
+ def consolidate!
6
7
  @original_doc = JSON.parse(@doc.to_json)
7
8
  end
9
+
10
+ def sync
11
+ consolidate!
12
+ end
13
+
14
+ #def reset_details!
15
+ # doc["details"] = JSON.parse(original_doc["details"])
16
+ #end
17
+
18
+ #def consolidate_details!
19
+ # original_doc["details"] = JSON.parse(doc["details"])
20
+ #end
21
+
8
22
  end
9
23
  end
10
24
  end
@@ -0,0 +1,17 @@
1
+ module Ecoportal
2
+ module API
3
+ class Internal
4
+ class Person
5
+
6
+ #def reset_account!
7
+ # doc["account"] = JSON.parse(original_doc["account"])
8
+ #end
9
+
10
+ #def consolidate_account!
11
+ # original_doc["account"] = JSON.parse(doc["account"])
12
+ #end
13
+
14
+ end
15
+ end
16
+ end
17
+ end
@@ -1,7 +1,6 @@
1
1
  module Eco
2
2
  module API
3
3
  class Session < Common::Session::BaseSession
4
-
5
4
  attr_reader :batch, :policy_groups
6
5
  attr_accessor :schema, :schemas_hash
7
6
  attr_reader :tagtree
@@ -11,12 +10,17 @@ module Eco
11
10
  #alias_method :org, :organization
12
11
 
13
12
  def initialize(init = {})
14
- super(init)
13
+ e = init
14
+ msg = "Expected object Eco::API::Session::Config or Eco::API::Common::Session::Environment. Given: #{init}"
15
+ raise msg unless e.is_a?(Session::Config) || e.is_a?(Eco::API::Common::Session::Environment)
16
+ e = Eco::API::Common::Session::Environment.new(init, session: self) if !e.is_a?(Eco::API::Common::Session::Environment)
17
+ super(e)
18
+
15
19
  logger.debug("LINE COMMAND: #{$0} #{ARGV.join(" ")}")
16
20
 
17
21
  @batch = Batch.new(enviro)
18
- @task = Task.new(self, enviro)
19
- @job_groups = JobGroups.new(session: self)
22
+ @task = Task.new(enviro)
23
+ @job_groups = JobGroups.new(enviro)
20
24
 
21
25
  @use_cases = Eco::API::UseCases::DefaultCases.new.merge(config.usecases.use_group)
22
26
 
@@ -46,16 +50,12 @@ module Eco
46
50
  end
47
51
 
48
52
  def self.configure
49
- #SessionConfig.new.tap
50
- conf = SessionConfig.new
53
+ # TODO: change to Session::Config.new.tap
54
+ conf = Session::Config.new
51
55
  yield(conf) if block_given?
52
56
  conf
53
57
  end
54
58
 
55
- def config
56
- enviro.config
57
- end
58
-
59
59
  # TASKS & JOBS
60
60
  def do
61
61
  @task
@@ -100,7 +100,7 @@ module Eco
100
100
  case value
101
101
  when String
102
102
  sch = @schemas_hash[value.downcase]
103
- raise "The schema with id or name '#{value}' does not exist" if !sch
103
+ fatal "The schema with id or name '#{value}' does not exist" if !sch
104
104
  @schema = sch
105
105
  when Ecoportal::API::V1::PersonSchema
106
106
  @schema = value
@@ -163,20 +163,14 @@ module Eco
163
163
  @use_cases.case(name, type: type).process(**args)
164
164
  end
165
165
 
166
- private
167
-
168
- def fatal(msg)
169
- logger.fatal(msg)
170
- raise msg
171
- end
172
-
173
166
  end
174
167
  end
175
168
  end
176
169
 
170
+ require_relative 'session/config'
177
171
  require_relative 'session/batch'
178
172
  require_relative 'session/batch_status'
179
173
  require_relative 'session/task'
180
174
  require_relative 'session/batch_job'
181
- require_relative 'session/job_group'
175
+ require_relative 'session/batch_jobs'
182
176
  require_relative 'session/job_groups'
@@ -15,11 +15,7 @@ module Eco
15
15
  end
16
16
 
17
17
  def launch(people = nil, method:, api: nil, params: {per_page: DEFAULT_BATCH_BLOCK})
18
- if !valid_method?(method)
19
- msg = "Invalid batch method: #{method}."
20
- logger.fatal(msg)
21
- raise msg
22
- end
18
+ fatal "Invalid batch method: #{method}." if !valid_method?(method)
23
19
  return nil if !people || !people.is_a?(Array)
24
20
  batch_from(people, method, api || self.api, params: params)
25
21
  end
@@ -35,11 +31,7 @@ module Eco
35
31
  end
36
32
 
37
33
  def batch_get(api, params: {})
38
- unless people_api = api&.people
39
- msg = "cannot batch get without api connnection, please provide a valid api connection!"
40
- logger.fatal(msg)
41
- raise msg
42
- end
34
+ fatal "cannot batch get without api connnection, please provide a valid api connection!" unless people_api = api&.people
43
35
 
44
36
  params = {per_page: DEFAULT_BATCH_BLOCK}.merge(params)
45
37
  client = people_api.client
@@ -69,8 +61,7 @@ module Eco
69
61
  unless response.success?
70
62
  msg = "Request failed - params: #{params}"
71
63
  msg += "\n Error message: - Status #{response.status}: #{response.body}"
72
- logger.fatal(msg)
73
- raise msg
64
+ fatal(msg)
74
65
  end
75
66
  people = []
76
67
  response.body["results"].each do |person_hash|
@@ -83,11 +74,7 @@ module Eco
83
74
 
84
75
  def batch_from(people, method, api, params: {})
85
76
  return nil if !people || !people.is_a?(Array)
86
- unless people_api = api&.people
87
- msg = "cannot batch #{method} without api connnection, please provide a valid api connection!"
88
- logger.fatal(msg)
89
- raise msg
90
- end
77
+ fatal "cannot batch #{method} without api connnection, please provide a valid api connection!" unless people_api = api&.people
91
78
 
92
79
  # batch Status
93
80
  status = new_status(people, method)
@@ -7,16 +7,15 @@ module Eco
7
7
 
8
8
  attr_reader :name, :status
9
9
 
10
- def initialize(name, type:, sets:, root:)
10
+ def initialize(e, name:, type:, sets:)
11
11
  raise "A name is required to refer a job. Given: #{name}" if !name
12
12
  raise "Type should be one of #{TYPES}. Given: #{type}" if !BatchJob.valid_type?(type)
13
13
  raise "Sets should be some of #{SETS}. Given: #{sets}" if !BatchJob.valid_sets?(sets)
14
- super(root.enviro)
14
+ super(e)
15
15
 
16
16
  @name = name
17
17
  @type = type
18
- @sets = [sets].flatten
19
- @root = root
18
+ @sets = [sets].flatten.compact
20
19
  reset
21
20
  end
22
21
 
@@ -66,7 +65,7 @@ module Eco
66
65
 
67
66
  if !simulate && queue.length > 0
68
67
  backup_update(queue)
69
- @status = batch.launch(queue, method: @type.to_s)
68
+ @status = session.batch.launch(queue, method: @type.to_s)
70
69
  @status.root = self
71
70
  end
72
71
 
@@ -141,14 +140,6 @@ module Eco
141
140
  file_manager.save_json(data_body, file, :timestamp)
142
141
  end
143
142
 
144
- def session
145
- @root.session
146
- end
147
-
148
- def batch
149
- session.batch
150
- end
151
-
152
143
  end
153
144
 
154
145
  end
@@ -2,13 +2,11 @@ module Eco
2
2
  module API
3
3
  class Session
4
4
  class BatchJobs < API::Common::Session::BaseSession
5
- attr_reader :name, :session
5
+ attr_reader :name
6
6
 
7
- def initialize(name, session:)
8
- raise("BatchJobs requires a session object") unless session.is_a?(API::Session)
9
- super(session.enviro)
10
- @session = session
11
- @name = name
7
+ def initialize(e, name:)
8
+ super(e)
9
+ @name = name
12
10
  reset
13
11
  end
14
12
 
@@ -26,9 +24,9 @@ module Eco
26
24
  end
27
25
 
28
26
  def new(name, type:, sets:)
29
- raise("Can't create job named '#{name}' because it already exists.") if exists?(name)
27
+ fatal "Can't create job named '#{name}' because it already exists." if exists?(name)
30
28
 
31
- job = BatchJob.new(name, type: type, sets: sets, root: self)
29
+ job = BatchJob.new(enviro, name: name, type: type, sets: sets)
32
30
  @jobs[name] = job
33
31
  @callbacks[job] = Proc.new if block_given?
34
32
 
@@ -7,12 +7,8 @@ module Eco
7
7
 
8
8
  def initialize(e, queue:, method:)
9
9
  super(e)
10
+ fatal("In batch operations you must batch an array. Received: #{queue}") unless queue && queue.is_a?(Array)
10
11
 
11
- unless queue && queue.is_a?(Array)
12
- msg = "In batch operations you must batch an array. Received: #{queue}"
13
- logger.fatal(msg)
14
- raise msg
15
- end
16
12
  @method = method
17
13
  @queue = queue
18
14
  @hash = @queue.each_with_index.map do |entry, i|
@@ -42,7 +38,7 @@ module Eco
42
38
  end
43
39
 
44
40
  def people
45
- raise "This batch wasn't a 'get'. Can't obtain people without 'get' method" unless method == "get"
41
+ fatal "This batch wasn't a 'get'. Can't obtain people without 'get' method" unless method == "get"
46
42
  out = Array(0..queue.length-1)
47
43
  @responses.each_with_index do |respose, i|
48
44
  out[i] = response.result
@@ -123,7 +119,7 @@ module Eco
123
119
  def valid_index(index: nil, entry: nil)
124
120
  index ||= @hash[entry]
125
121
  unless index && index < @queue.length
126
- raise "You must provide either the index or the original entry object of the batch"
122
+ fatal "You must provide either the index or the original entry object of the batch"
127
123
  end
128
124
  index
129
125
  end
@@ -0,0 +1,186 @@
1
+ module Eco
2
+ module API
3
+ class Session
4
+ class Config < Hash
5
+ def initialize()
6
+ super(nil)
7
+ self["org"] = {}
8
+ end
9
+
10
+ def reopen
11
+ yield(self)
12
+ end
13
+
14
+ def apis
15
+ self["apis"] ||= Session::Config::Apis.new(root: self)
16
+ end
17
+
18
+ def logger
19
+ self["logger"] ||= Session::Config::Logger.new(root: self)
20
+ end
21
+
22
+ def s3storage
23
+ self["s3_storage"] ||= Session::Config::S3Storage.new(root: self)
24
+ end
25
+
26
+ def files
27
+ self["files"] ||= Session::Config::Files.new(root: self)
28
+ end
29
+
30
+ def mailer
31
+ self["mailer"] ||= Session::Config::Mailer.new(root: self)
32
+ end
33
+
34
+ def org
35
+ self["org"]
36
+ end
37
+
38
+ def people
39
+ self["people"] ||= Session::Config::People.new(root: self)
40
+ end
41
+
42
+ def usecases
43
+ self["usecases"] ||= Session::Config::UseCases.new(root: self)
44
+ end
45
+
46
+ # LOGGER
47
+ def log_console_level=(value)
48
+ logger.console_level= value
49
+ end
50
+
51
+ def log_file_level=(value)
52
+ logger.file_level = value
53
+ end
54
+
55
+ def log_file=(file)
56
+ logger.file = file
57
+ end
58
+
59
+ def timestamp_console=(value)
60
+ logger.timestamp_console = value
61
+ end
62
+
63
+ def log_connection=(value)
64
+ logger.log_connection = value
65
+ end
66
+
67
+ # API
68
+ def dry_run!
69
+ self["dry-run"] = true
70
+ end
71
+
72
+ def dry_run?
73
+ self["dry-run"]
74
+ end
75
+
76
+ def run_mode=(mode)
77
+ apis.active_api.mode = mode
78
+ end
79
+
80
+ def run_mode_local?
81
+ apis.active_api.local?
82
+ end
83
+
84
+ def run_mode_remote?
85
+ apis.active_api.remote?
86
+ end
87
+
88
+ def apis?
89
+ apis.apis?
90
+ end
91
+
92
+ def add_api(name, **kargs)
93
+ apis.add(name, **kargs)
94
+ self
95
+ end
96
+
97
+ def active_api(name)
98
+ apis.active_name = name
99
+ self
100
+ end
101
+
102
+ def api(logger = ::Logger.new(IO::NULL))
103
+ apis.api(logger)
104
+ end
105
+
106
+ def policy_groups
107
+ policy_groups = api&.policy_groups.to_a.compact
108
+ Eco::API::Organization::PolicyGroups.new(policy_groups)
109
+ end
110
+
111
+ # FILES
112
+ def working_directory=(path)
113
+ files.working_directory = path
114
+ end
115
+
116
+ def file_timestamp_pattern=(pattern)
117
+ files.timestamp_pattern = pattern
118
+ end
119
+
120
+ def file_manager
121
+ Eco::API::Common::Session::FileManager.new(self)
122
+ end
123
+
124
+ def require(file)
125
+ require_relative "#{File.expand_path(file_manager.dir.file(file))}"
126
+ end
127
+
128
+ # ORG
129
+ def tagtree=(file)
130
+ org["tagtree"] = file
131
+ end
132
+
133
+ # PEOPLE
134
+ def discarded_people_file=(value)
135
+ people.discarded_file = value
136
+ end
137
+
138
+ def people_cache=(file)
139
+ people.cache = file
140
+ end
141
+
142
+ def requests_backup_folder=(folder)
143
+ people.requests_folder = folder
144
+ end
145
+
146
+ # PERSON FIELDS MAPPER
147
+ def person_fields_mapper=(file)
148
+ people.fields_mapper = file
149
+ end
150
+
151
+ def default_schema=(name)
152
+ people.default_schema = name
153
+ end
154
+
155
+ # ACCOUNT PRESETS
156
+ def presets_custom=(file)
157
+ people.presets_custom = file
158
+ end
159
+
160
+ def presets_map=(file)
161
+ people.presets_map = file
162
+ end
163
+
164
+ # CUSTOM PERSON PARSERS
165
+ def person_parser(format: :csv, &block)
166
+ people.add_parser(format: format, &block)
167
+ end
168
+
169
+ # CUSTOM USE CASES
170
+ def use_cases(&block)
171
+ usecases.add(&block)
172
+ end
173
+
174
+ end
175
+ end
176
+ end
177
+ end
178
+
179
+ require_relative 'config/api'
180
+ require_relative 'config/apis'
181
+ require_relative 'config/logger'
182
+ require_relative 'config/mailer'
183
+ require_relative 'config/s3_storage'
184
+ require_relative 'config/files'
185
+ require_relative 'config/people'
186
+ require_relative 'config/use_cases'