eco-helpers 0.6.2 → 0.6.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6946d1680313de425978c9ebbb55f0f59211fee5
4
- data.tar.gz: dd889e82758cc4344fdcc5a5a9c28f9467846f8c
3
+ metadata.gz: ad5941e8792705047cf6dbd46515023c9bda2285
4
+ data.tar.gz: b1b3ef9fa046781f791170f2a4e872ef79862efe
5
5
  SHA512:
6
- metadata.gz: 3a51c49225a13b8ec76211571e4082c6e8b4d394ffa2ce886af430c8dfa1d48b2295724de2edf46e6a0c36f3547566ab42e34ee871a8d581dbbf74b888a3a0aa
7
- data.tar.gz: de29a8397e45cf8f22b500cffaa56b1e42c1d8ca28abc819b71f6c7adbfa7614479284ee02aa5fa1901ecc7fbcd3390326adc217f4bc3d5cc24c0fce68691059
6
+ metadata.gz: b05481b93f00fe18b0d8721ec2c1e11e25e85bda934042458e948217bb8e52a5efe2ae53464525bd95baffcb003f749361653bb0273edd8ae4ea80ea6b4a1c64
7
+ data.tar.gz: e26cc0b7607a2c7197c1e528a914ca9ea2414688205d21421c582d81317a54c6cc76c454646d88b865f287ebe389bd44e85cf0929b09036e8859183bc5856add
@@ -15,11 +15,11 @@ module Eco
15
15
  def initialize(init = {})
16
16
  init = init.conf if init.is_a?(Environment)
17
17
  msg = "Wrong Session::Environment initializer. Expected Hash or Environment object. Given: #{init}"
18
- raise msg unless init.is_a?(SessionConfig)
18
+ raise msg unless init.is_a?(Eco::API::SessionConfig)
19
19
 
20
20
  @config = init
21
21
  @file_manager = Eco::API::Common::Session::FileManager.new(enviro: self)
22
- @logger = Session::Logger.new(enviro: self)
22
+ @logger = Eco::API::Common::Session::Logger.new(enviro: self)
23
23
  new_api
24
24
  end
25
25
 
@@ -84,7 +84,6 @@ module Eco
84
84
  config.mailer.message_id_domain
85
85
  end
86
86
 
87
-
88
87
  end
89
88
  end
90
89
  end
@@ -15,7 +15,14 @@ module Eco
15
15
  end
16
16
 
17
17
  def to_id(name)
18
- policy_group(name)&.id
18
+ case name
19
+ when Array
20
+ name.map do |n|
21
+ policy_group(n)&.id
22
+ end.compact
23
+ else
24
+ policy_group(name)&.id
25
+ end
19
26
  end
20
27
 
21
28
  def to_name(id)
@@ -26,6 +33,21 @@ module Eco
26
33
  @by_id.fetch(policy_group_id(id_name), nil)
27
34
  end
28
35
 
36
+ def user_pg_ids(initial: [], final: [], non_custom: [], preserve_custom: true)
37
+ non_custom = to_id([non_custom].flatten.compact)
38
+ initial = to_id([initial].flatten.compact)
39
+ final = to_id([final].flatten.compact)
40
+ unless initial.is_a?(Array) && final.is_a?(Array) && non_custom.is_a?(Array)
41
+ raise "Expected Array for :initial, :final and :custom"
42
+ end
43
+
44
+ initial_custom = initial - non_custom
45
+ final = final + initial_custom if preserve_custom
46
+ new_pg_ids = final - initial
47
+ # keep same order as they where
48
+ (initial & final) + new_pg_ids
49
+ end
50
+
29
51
  private
30
52
 
31
53
  def policy_group_name(id_name)
@@ -53,8 +53,8 @@ module Eco
53
53
  end
54
54
 
55
55
  def user_tags(initial: [], final: [], preserve_custom: true, add_custom: false)
56
- initial ||= [initial].flatten.compact
57
- final ||= [final].flatten.compact
56
+ initial = [initial].flatten.compact
57
+ final = [final].flatten.compact
58
58
  raise "Expected Array for initial: and final:" unless initial.is_a?(Array) && final.is_a?(Array)
59
59
  final = filter_tags(final) unless add_custom
60
60
  custom = initial - filter_tags(initial)
@@ -101,10 +101,11 @@ module Eco
101
101
  params = {per_page: DEFAULT_BATCH_BLOCK}.merge(params)
102
102
  per_page = params.fetch(:per_page)
103
103
 
104
- iteration = 1; done = 0
104
+ iteration = 1; done = 0
105
+ iterations = (people.length / per_page).ceil
105
106
 
106
107
  people.each_slice(per_page) do |slice|
107
- msg = "starting batch '#{method}' iteration #{iteration}, which has #{slice.length} entries of #{people.length} -- #{done} done"
108
+ msg = "starting batch '#{method}' iteration #{iteration}/#{iterations}, with #{slice.length} entries of #{people.length} -- #{done} done"
108
109
  logger.info(msg)
109
110
 
110
111
  people_api.batch do |batch|
@@ -9,8 +9,8 @@ module Eco
9
9
 
10
10
  def initialize(name, type:, sets:, root:)
11
11
  raise "A name is required to refer a job. Given: #{name}" if !name
12
- raise "Type should be one of #{TYPES}. Given: #{type}" if !BatchJob.valid_type?(type)
13
- raise "Sets should be some of #{SETS}. Given: #{sets}" if !BatchJob.valid_sets?(sets)
12
+ raise "Type should be one of #{TYPES}. Given: #{type}" if !BatchJob.valid_type?(type)
13
+ raise "Sets should be some of #{SETS}. Given: #{sets}" if !BatchJob.valid_sets?(sets)
14
14
  super(root.enviro)
15
15
 
16
16
  @name = name
@@ -39,10 +39,10 @@ module Eco
39
39
  @queue.length > 0
40
40
  end
41
41
 
42
- def add(data)
43
- unless !data
44
- @queue.push(data)
45
- @callbacks[data] = Proc.new if block_given?
42
+ def add(entry)
43
+ unless !entry
44
+ @queue.push(entry)
45
+ @callbacks[entry] = Proc.new if block_given?
46
46
  end
47
47
  end
48
48
 
@@ -70,7 +70,7 @@ module Eco
70
70
  @status.root = self
71
71
  end
72
72
 
73
- logger.info("Simulate: this would have launched: '#{@type.to_s}'") if simulate
73
+ logger.info("Simulate: this would have launched: '#{@type.to_s}'") if simulate
74
74
  return @status
75
75
  end
76
76
 
@@ -1,7 +1,3 @@
1
- require 'json'
2
- require 'pp'
3
- require 'securerandom' # for uuid (perhaps metter to have structure i.e. Session:id-Batch:id)
4
-
5
1
  module Eco
6
2
  module API
7
3
  class Session
@@ -0,0 +1,58 @@
1
+ module Eco
2
+ module API
3
+ class Session
4
+ class BatchJobs < API::Common::Session::BaseSession
5
+ attr_reader :name, :session
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
12
+ reset
13
+ end
14
+
15
+ def reset
16
+ @jobs = {}
17
+ @callbacks = {}
18
+ end
19
+
20
+ def [](name)
21
+ @jobs[name]
22
+ end
23
+
24
+ def exists?(name)
25
+ @jobs.key?(name)
26
+ end
27
+
28
+ def new(name, type:, sets:)
29
+ raise("Can't create job named '#{name}' because it already exists.") if exists?(name)
30
+
31
+ job = BatchJob.new(name, type: type, sets: sets, root: self)
32
+ @jobs[name] = job
33
+ @callbacks[job] = Proc.new if block_given?
34
+
35
+ job
36
+ end
37
+
38
+ def pending?
39
+ @jobs.keys.any? do |key|
40
+ @jobs[key].pending?
41
+ end
42
+ end
43
+
44
+ def launch(simulate: false)
45
+ group_status = {}
46
+ @jobs.each do |name, job|
47
+ group_status[job] = job_status = job.launch(simulate: simulate)
48
+ callback = @callbacks[job]
49
+ callback.call(job, job_status) if callback
50
+ end
51
+
52
+ return group_status
53
+ end
54
+
55
+ end
56
+ end
57
+ end
58
+ end
@@ -73,59 +73,6 @@ module Eco
73
73
  end
74
74
 
75
75
  end
76
-
77
- class BatchJobs < API::Common::Session::BaseSession
78
- attr_reader :name, :session
79
-
80
- def initialize(name, session:)
81
- raise("BatchJobs requires a session object") unless session.is_a?(API::Session)
82
- super(session.enviro)
83
- @session = session
84
- @name = name
85
- reset
86
- end
87
-
88
- def reset
89
- @jobs = {}
90
- @callbacks = {}
91
- end
92
-
93
- def [](name)
94
- @jobs[name]
95
- end
96
-
97
- def exists?(name)
98
- @jobs.key?(name)
99
- end
100
-
101
- def new(name, type:, sets:)
102
- raise("Can't create job named '#{name}' because it already exists.") if exists?(name)
103
-
104
- job = BatchJob.new(name, type: type, sets: sets, root: self)
105
- @jobs[name] = job
106
- @callbacks[job] = Proc.new if block_given?
107
-
108
- job
109
- end
110
-
111
- def pending?
112
- @jobs.keys.any? do |key|
113
- @jobs[key].pending?
114
- end
115
- end
116
-
117
- def launch(simulate: false)
118
- group_status = {}
119
- @jobs.each do |name, job|
120
- group_status[job] = job_status = job.launch(simulate: simulate)
121
- callback = @callbacks[job]
122
- callback.call(job, job_status) if callback
123
- end
124
-
125
- return group_status
126
- end
127
-
128
- end
129
76
  end
130
77
  end
131
78
  end
@@ -23,7 +23,7 @@ module Eco
23
23
  people = batch.get_people
24
24
  file = file_manager.save_json(people, filename, :timestamp)
25
25
  logger.info("#{people.length} people loaded and saved locally to #{file}.")
26
- API::Organization::People.new(people)
26
+ Eco::API::Organization::People.new(people)
27
27
  end
28
28
 
29
29
  def load_people(filename = enviro.config.people.cache, modifier: [:newest, :api])
@@ -178,4 +178,5 @@ require_relative 'session/batch'
178
178
  require_relative 'session/batch_status'
179
179
  require_relative 'session/task'
180
180
  require_relative 'session/batch_job'
181
- require_relative 'session/batch_jobs'
181
+ require_relative 'session/job_group'
182
+ require_relative 'session/job_groups'
@@ -10,7 +10,6 @@ module Eco
10
10
 
11
11
  def apis
12
12
  self["apis"] ||= {}
13
- self["apis"]
14
13
  end
15
14
 
16
15
  def apis?
@@ -58,12 +57,20 @@ module Eco
58
57
  self["active-api"]
59
58
  end
60
59
 
61
- def api(logger = ::Logger.new(IO::NULL))
60
+ def service_up?
61
+ api_klass = (active_api.version == :external)? Ecoportal::API::External : Ecoportal::API::Internal
62
+ @api_test ||= api_klass.new("foobar", host: active_api.host, logger: ::Logger.new(IO::NULL))
63
+ status = @api_test.client.get("/policy_groups").status
64
+ # 401 Unauthorized "Permission denied. API key may be invalid."
65
+ status == 401
66
+ end
67
+
68
+ def api(logger = ::Logger.new(IO::NULL), force_new: false)
62
69
  key = active_api.key
63
70
  host = active_api.host
64
71
  mode = active_api.mode
65
72
  version = active_api.version
66
- return @api if @api && key == @key && host == @host && @mode == mode && version == @version
73
+ return @api if (!force_new) && @api && key == @key && host == @host && @mode == mode && version == @version
67
74
 
68
75
  api_klass = (version == :external)? Ecoportal::API::External : Ecoportal::API::Internal
69
76
 
@@ -72,6 +79,10 @@ module Eco
72
79
  @api
73
80
  end
74
81
 
82
+ def new_api(logger = ::Logger.new(IO::NULL))
83
+ api(logger, force_new: true)
84
+ end
85
+
75
86
  end
76
87
  end
77
88
  end
@@ -73,15 +73,14 @@ module Eco
73
73
  # CUSTOM PERSON PARSERS
74
74
  def add_parser(format: :csv)
75
75
  new_parsers = Eco::API::Common::People::PersonParser.new
76
-
77
76
  yield(new_parsers, config)
77
+
78
78
  parsers[format] ||= Eco::API::Common::People::PersonParser.new
79
79
  parsers[format] = parsers[format] ? parsers[format].merge(new_parsers) : new_parsers
80
80
  end
81
81
 
82
82
  def parsers
83
83
  self["parsers"] ||= {}
84
- self["parsers"]
85
84
  end
86
85
 
87
86
  def parser(format: :csv)
@@ -3,7 +3,7 @@ module Eco
3
3
  class SessionConfig
4
4
  class UseCases < Hash
5
5
  attr_reader :config
6
-
6
+
7
7
  def initialize(root:)
8
8
  super(nil)
9
9
  @root = root
@@ -22,7 +22,7 @@ module Eco
22
22
  end
23
23
 
24
24
  def use_group
25
- self["use_group"]
25
+ self["use_group"] ||= Eco::API::UseCases::UseGroup.new
26
26
  end
27
27
 
28
28
  end
data/lib/eco/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Eco
2
- VERSION = "0.6.2"
2
+ VERSION = "0.6.3"
3
3
  end
data/lib/eco-helpers.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  require 'csv'
2
- require 'json' # native
2
+ require 'json'
3
3
  require 'pp'
4
4
 
5
5
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eco-helpers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.2
4
+ version: 0.6.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oscar Segura
@@ -223,8 +223,9 @@ files:
223
223
  - lib/eco/api/session.rb
224
224
  - lib/eco/api/session/batch.rb
225
225
  - lib/eco/api/session/batch_job.rb
226
- - lib/eco/api/session/batch_jobs.rb
227
226
  - lib/eco/api/session/batch_status.rb
227
+ - lib/eco/api/session/job_group.rb
228
+ - lib/eco/api/session/job_groups.rb
228
229
  - lib/eco/api/session/task.rb
229
230
  - lib/eco/api/session_config.rb
230
231
  - lib/eco/api/session_config/api.rb