eco-helpers 0.6.0
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 +7 -0
- data/.gitignore +1 -0
- data/.rspec +3 -0
- data/README.md +20 -0
- data/eco-helpers.gemspec +34 -0
- data/lib/eco-helpers.rb +15 -0
- data/lib/eco/api.rb +13 -0
- data/lib/eco/api/common.rb +10 -0
- data/lib/eco/api/common/people.rb +17 -0
- data/lib/eco/api/common/people/base_parser.rb +16 -0
- data/lib/eco/api/common/people/default_parsers.rb +40 -0
- data/lib/eco/api/common/people/default_parsers/boolean_parser.rb +28 -0
- data/lib/eco/api/common/people/default_parsers/date_parser.rb +33 -0
- data/lib/eco/api/common/people/default_parsers/multi_parser.rb +33 -0
- data/lib/eco/api/common/people/default_parsers/numeric_parser.rb +23 -0
- data/lib/eco/api/common/people/default_parsers/select_parser.rb +29 -0
- data/lib/eco/api/common/people/entries.rb +120 -0
- data/lib/eco/api/common/people/person_entry.rb +380 -0
- data/lib/eco/api/common/people/person_factory.rb +114 -0
- data/lib/eco/api/common/people/person_modifier.rb +62 -0
- data/lib/eco/api/common/people/person_parser.rb +140 -0
- data/lib/eco/api/common/people/types.rb +47 -0
- data/lib/eco/api/common/session.rb +15 -0
- data/lib/eco/api/common/session/base_session.rb +46 -0
- data/lib/eco/api/common/session/environment.rb +47 -0
- data/lib/eco/api/common/session/file_manager.rb +90 -0
- data/lib/eco/api/common/session/logger.rb +105 -0
- data/lib/eco/api/common/session/mailer.rb +92 -0
- data/lib/eco/api/common/session/s3_uploader.rb +110 -0
- data/lib/eco/api/common/version_patches.rb +11 -0
- data/lib/eco/api/common/version_patches/external_person.rb +11 -0
- data/lib/eco/api/eco_faker.rb +59 -0
- data/lib/eco/api/organization.rb +13 -0
- data/lib/eco/api/organization/account.rb +23 -0
- data/lib/eco/api/organization/people.rb +118 -0
- data/lib/eco/api/organization/policy_groups.rb +51 -0
- data/lib/eco/api/organization/preferences.rb +28 -0
- data/lib/eco/api/organization/preferences_reference.json +23 -0
- data/lib/eco/api/organization/presets.rb +138 -0
- data/lib/eco/api/organization/presets_backup.rb +220 -0
- data/lib/eco/api/organization/presets_values.json +10 -0
- data/lib/eco/api/organization/tag_tree.rb +134 -0
- data/lib/eco/api/organization_old.rb +73 -0
- data/lib/eco/api/session.rb +180 -0
- data/lib/eco/api/session/batch.rb +132 -0
- data/lib/eco/api/session/batch_job.rb +152 -0
- data/lib/eco/api/session/batch_jobs.rb +131 -0
- data/lib/eco/api/session/batch_status.rb +138 -0
- data/lib/eco/api/session/task.rb +92 -0
- data/lib/eco/api/session_config.rb +179 -0
- data/lib/eco/api/session_config/api.rb +47 -0
- data/lib/eco/api/session_config/apis.rb +78 -0
- data/lib/eco/api/session_config/files.rb +30 -0
- data/lib/eco/api/session_config/logger.rb +54 -0
- data/lib/eco/api/session_config/mailer.rb +65 -0
- data/lib/eco/api/session_config/people.rb +89 -0
- data/lib/eco/api/session_config/s3_bucket.rb +62 -0
- data/lib/eco/api/session_config/use_cases.rb +30 -0
- data/lib/eco/api/usecases.rb +12 -0
- data/lib/eco/api/usecases/base_case.rb +14 -0
- data/lib/eco/api/usecases/case_data.rb +13 -0
- data/lib/eco/api/usecases/default_cases.rb +53 -0
- data/lib/eco/api/usecases/default_cases/change_email_case.rb +47 -0
- data/lib/eco/api/usecases/default_cases/create_details_case.rb +29 -0
- data/lib/eco/api/usecases/default_cases/create_details_with_supervisor_case.rb +49 -0
- data/lib/eco/api/usecases/default_cases/delete_case.rb +20 -0
- data/lib/eco/api/usecases/default_cases/email_as_id_case.rb +24 -0
- data/lib/eco/api/usecases/default_cases/hris_case.rb +67 -0
- data/lib/eco/api/usecases/default_cases/new_email_case.rb +26 -0
- data/lib/eco/api/usecases/default_cases/new_id_case.rb +26 -0
- data/lib/eco/api/usecases/default_cases/refresh_presets.rb +25 -0
- data/lib/eco/api/usecases/default_cases/reinvite_case.rb +22 -0
- data/lib/eco/api/usecases/default_cases/remove_account_case.rb +36 -0
- data/lib/eco/api/usecases/default_cases/reset_landing_page_case.rb +24 -0
- data/lib/eco/api/usecases/default_cases/set_default_tag_case.rb +44 -0
- data/lib/eco/api/usecases/default_cases/set_supervisor_case.rb +39 -0
- data/lib/eco/api/usecases/default_cases/to_csv_case.rb +36 -0
- data/lib/eco/api/usecases/default_cases/update_details_case.rb +30 -0
- data/lib/eco/api/usecases/default_cases/upsert_account_case.rb +35 -0
- data/lib/eco/api/usecases/use_case.rb +177 -0
- data/lib/eco/api/usecases/use_group.rb +104 -0
- data/lib/eco/cli.rb +9 -0
- data/lib/eco/cli/input.rb +109 -0
- data/lib/eco/cli/input_multi.rb +137 -0
- data/lib/eco/cli/root.rb +8 -0
- data/lib/eco/cli/session.rb +9 -0
- data/lib/eco/cli/session/batch.rb +9 -0
- data/lib/eco/common.rb +7 -0
- data/lib/eco/common/base_cli.rb +116 -0
- data/lib/eco/common/language.rb +9 -0
- data/lib/eco/data.rb +9 -0
- data/lib/eco/data/crypto.rb +7 -0
- data/lib/eco/data/crypto/encryption.rb +318 -0
- data/lib/eco/data/files.rb +10 -0
- data/lib/eco/data/files/directory.rb +93 -0
- data/lib/eco/data/files/file_pattern.rb +32 -0
- data/lib/eco/data/files/helpers.rb +90 -0
- data/lib/eco/data/mapper.rb +54 -0
- data/lib/eco/data/random.rb +10 -0
- data/lib/eco/data/random/distribution.rb +133 -0
- data/lib/eco/data/random/fake.rb +320 -0
- data/lib/eco/data/random/values.rb +80 -0
- data/lib/eco/language.rb +12 -0
- data/lib/eco/language/curry.rb +28 -0
- data/lib/eco/language/hash_transform.rb +68 -0
- data/lib/eco/language/hash_transform_modifier.rb +114 -0
- data/lib/eco/language/match.rb +30 -0
- data/lib/eco/language/match_modifier.rb +190 -0
- data/lib/eco/language/models.rb +11 -0
- data/lib/eco/language/models/attribute_parser.rb +38 -0
- data/lib/eco/language/models/collection.rb +181 -0
- data/lib/eco/language/models/modifier.rb +68 -0
- data/lib/eco/language/models/wrap.rb +114 -0
- data/lib/eco/language/values_at.rb +159 -0
- data/lib/eco/lexic/dictionary.rb +33 -0
- data/lib/eco/lexic/dictionary/dictionary.txt +355484 -0
- data/lib/eco/lexic/dictionary/tags.json +38 -0
- data/lib/eco/scripting.rb +30 -0
- data/lib/eco/scripting/README.md +11 -0
- data/lib/eco/scripting/arguments.rb +40 -0
- data/lib/eco/tester.rb +97 -0
- data/lib/eco/version.rb +3 -0
- metadata +325 -0
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
module Eco
|
|
2
|
+
module API
|
|
3
|
+
module Common
|
|
4
|
+
module People
|
|
5
|
+
class PersonModifier < Eco::Language::Models::Modifier
|
|
6
|
+
|
|
7
|
+
WITH_DETAILS = [:contact, :details, :with_details, :schema, :with_schema]
|
|
8
|
+
NO_DETAILS = [:no_details, :only_account]
|
|
9
|
+
|
|
10
|
+
WITH_ACCOUNT = [:user, :account, :with_account]
|
|
11
|
+
NO_ACCOUNT = [:no_account, :only_details]
|
|
12
|
+
|
|
13
|
+
INTERNAL_PERSON = [:internal, :v0]
|
|
14
|
+
EXTERNAL_PERSON = [:external, :v1]
|
|
15
|
+
|
|
16
|
+
def reset_add_account
|
|
17
|
+
self < self.mode - (WITH_ACCOUNT | NO_ACCOUNT)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def no_details
|
|
21
|
+
self.push(:no_details)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def add_account?
|
|
25
|
+
mode.any? { |m| WITH_ACCOUNT.include?(m) }
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def no_account?
|
|
29
|
+
mode.any? { |m| NO_ACCOUNT.include?(m) }
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def add_details?
|
|
33
|
+
mode.any? { |m| WITH_DETAILS.include?(m) }
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def no_details?
|
|
37
|
+
mode.any? { |m| NO_DETAILS.include?(m) }
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def internal?
|
|
41
|
+
bool = mode.any? { |m| INTERNAL_PERSON.include?(m) }
|
|
42
|
+
bool || !external?
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def external?
|
|
46
|
+
mode.any? { |m| EXTERNAL_PERSON.include?(m) }
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
protected
|
|
50
|
+
|
|
51
|
+
def resolve_mode
|
|
52
|
+
modifiers = self.to_a
|
|
53
|
+
modifiers = resolve(modifiers, WITH_DETAILS | NO_DETAILS)
|
|
54
|
+
modifiers = resolve(modifiers, WITH_ACCOUNT | NO_ACCOUNT)
|
|
55
|
+
modifiers = resolve(modifiers, INTERNAL_PERSON | EXTERNAL_PERSON)
|
|
56
|
+
modifiers
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
module Eco
|
|
2
|
+
module API
|
|
3
|
+
module Common
|
|
4
|
+
module People
|
|
5
|
+
class PersonParser
|
|
6
|
+
CORE_ATTRS = ["id", "external_id", "email", "name", "supervisor_id"]
|
|
7
|
+
ACCOUNT_ATTRS = ["policy_group_ids", "filter_tags", "default_tag"]
|
|
8
|
+
TYPE = [:select, :text, :date, :number, :phone_number, :boolean, :multiple]
|
|
9
|
+
|
|
10
|
+
attr_reader :schema
|
|
11
|
+
attr_reader :details_attrs, :all_attrs
|
|
12
|
+
attr_reader :defined_attrs
|
|
13
|
+
|
|
14
|
+
def initialize(schema: nil) #), logger: nil)
|
|
15
|
+
#self.schema = schema
|
|
16
|
+
raise "Constructor needs a PersonSchema. Given: #{schema}" if schema && !schema.is_a?(Ecoportal::API::V1::PersonSchema)
|
|
17
|
+
@details_attrs = []
|
|
18
|
+
@parsers = {}
|
|
19
|
+
|
|
20
|
+
if schema
|
|
21
|
+
@schema = Ecoportal::API::Internal::PersonSchema.new(JSON.parse(schema.doc.to_json))
|
|
22
|
+
@details_attrs = @schema&.fields.map { |fld| fld.alt_id }
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
@all_attrs = CORE_ATTRS + ACCOUNT_ATTRS + @details_attrs
|
|
26
|
+
#init_default_parsers if @schema
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def target_attrs_core(source_attrs = nil)
|
|
30
|
+
return CORE_ATTRS if !source_attrs
|
|
31
|
+
scoped_attrs(source_attrs, CORE_ATTRS)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def target_attrs_details(source_attrs = nil)
|
|
35
|
+
return @details_attrs if ! source_attrs
|
|
36
|
+
scoped_attrs(source_attrs, @details_attrs)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def target_attrs_account(source_attrs = nil)
|
|
40
|
+
return ACCOUNT_ATTRS if !source_attrs
|
|
41
|
+
scoped_attrs(source_attrs, ACCOUNT_ATTRS)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def defined
|
|
45
|
+
@parsers.keys
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def list
|
|
49
|
+
@parsers.keys
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def defined_attrs
|
|
53
|
+
defined - (defined - all_attrs)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def undefined_attrs
|
|
57
|
+
all_attrs - defined_attrs
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def defined?(attr)
|
|
61
|
+
!!@parsers[attr]
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
#def append(attr, dependencies: {}, &bloc)
|
|
65
|
+
# @parsers[attr] = into_a(parsers[attr]).push(define_parser(attr, dependencies, &bloc))
|
|
66
|
+
#end
|
|
67
|
+
|
|
68
|
+
# merges parser overriding self for exisint parsers
|
|
69
|
+
def merge(parser)
|
|
70
|
+
return self if !parser
|
|
71
|
+
raise "Expected a PersonParser object. Given #{parser}" if !parser.is_a?(PersonParser)
|
|
72
|
+
@parsers.merge!(parser.hash)
|
|
73
|
+
self
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def define_attribute(attr, dependencies: {}, &bloc)
|
|
77
|
+
@parsers[attr] = define_parser(attr, dependencies, &bloc)
|
|
78
|
+
self
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def parse(attr, source, deps: {})
|
|
82
|
+
raise "There is no parser for attribute '#{attr}'" if !self.defined?(attr)
|
|
83
|
+
@parsers[attr].parse(source, dependencies: deps)
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def serialize(attr, object, deps: {})
|
|
87
|
+
raise "There is no parser for attribute '#{attr}'" if !self.defined?(attr)
|
|
88
|
+
@parsers[attr].serialize(object, dependencies: deps)
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
protected
|
|
92
|
+
|
|
93
|
+
def hash
|
|
94
|
+
@parsers
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
private
|
|
98
|
+
|
|
99
|
+
def scoped_attrs(source_attrs, section_attrs)
|
|
100
|
+
direct_attrs = source_attrs & section_attrs
|
|
101
|
+
parsed_attrs = @parsers.keys & section_attrs
|
|
102
|
+
(source_attrs + parsed_attrs) & (direct_attrs + parsed_attrs)
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def define_parser(attr, dependencies = {}, &definition)
|
|
106
|
+
if !valid?(attr)
|
|
107
|
+
str_attr = (attr.is_a?(Symbol) ? ":" : "") + attr.to_s
|
|
108
|
+
msg = "The attribute '#{str_attr}' is not part of core, account or target schema, or does not match any type: #{@details_attrs}"
|
|
109
|
+
raise msg
|
|
110
|
+
end
|
|
111
|
+
parser = Eco::Language::Models::AttributeParser.new(attr, dependencies: dependencies)
|
|
112
|
+
#yield(parser)
|
|
113
|
+
definition.call(parser)
|
|
114
|
+
parser
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def valid?(attr)
|
|
118
|
+
(attr.is_a?(Symbol) && valid_type?(attr)) ||
|
|
119
|
+
(attr.is_a?(String) && (!@schema || valid_attr?(attr)))
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
def valid_attr?(attr)
|
|
123
|
+
@all_attrs.include?(attr)
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
def valid_type?(attr)
|
|
127
|
+
TYPE.include?(attr)
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
def into_a(value)
|
|
131
|
+
value = [] if value == nil
|
|
132
|
+
value = [].push(value) unless value.is_a?(Array)
|
|
133
|
+
value
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
end
|
|
137
|
+
end
|
|
138
|
+
end
|
|
139
|
+
end
|
|
140
|
+
end
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
module Eco
|
|
2
|
+
module API
|
|
3
|
+
module Common
|
|
4
|
+
module People
|
|
5
|
+
|
|
6
|
+
EAPI = Ecoportal::API
|
|
7
|
+
LAST = EAPI::V1
|
|
8
|
+
EXTERNAL = LAST
|
|
9
|
+
INTERNAL = EAPI::Internal
|
|
10
|
+
COMMON_API = EAPI::Common
|
|
11
|
+
|
|
12
|
+
def is_internal_person?(object)
|
|
13
|
+
object.is_a?(INTERNAL::Person)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def is_person?(object)
|
|
17
|
+
object.is_a?(EXTERNAL::Person)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def is_internal?(object)
|
|
21
|
+
object.is_a?(INTERNAL)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def is_external?(object)
|
|
25
|
+
object.is_a?(EXTERNAL)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def is_api?(object)
|
|
29
|
+
is_internal?(object) || is_external?(object)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def is_internal_people?(object)
|
|
33
|
+
object.is_a?(INTERNAL::People)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def is_people?(object)
|
|
37
|
+
object.is_a?(EXTERNAL::People)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def is_batch_operation(object)
|
|
41
|
+
object.is_a?(COMMON_API::BatchOperation)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
module Eco
|
|
2
|
+
module API
|
|
3
|
+
module Common
|
|
4
|
+
module Session
|
|
5
|
+
end
|
|
6
|
+
end
|
|
7
|
+
end
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
require_relative 'session/logger'
|
|
11
|
+
require_relative 'session/mailer'
|
|
12
|
+
require_relative 'session/s3_uploader'
|
|
13
|
+
require_relative 'session/file_manager'
|
|
14
|
+
require_relative 'session/environment'
|
|
15
|
+
require_relative 'session/base_session'
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
module Eco
|
|
2
|
+
module API
|
|
3
|
+
module Common
|
|
4
|
+
module Session
|
|
5
|
+
class BaseSession
|
|
6
|
+
|
|
7
|
+
include Common::People
|
|
8
|
+
|
|
9
|
+
attr_accessor :environment, :config
|
|
10
|
+
alias_method :enviro, :environment
|
|
11
|
+
alias_method :enviro=, :environment=
|
|
12
|
+
|
|
13
|
+
attr_reader :api, :file_manager, :logger
|
|
14
|
+
alias_method :fm, :file_manager
|
|
15
|
+
|
|
16
|
+
def initialize(e)
|
|
17
|
+
e = Environment.new(e) if !e.is_a?(Environment)
|
|
18
|
+
self.environment = e if e.is_a?(Environment)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def environment=(value)
|
|
22
|
+
@environment = nil
|
|
23
|
+
@environment = value if value.is_a?(Environment)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def config
|
|
27
|
+
enviro.config
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def api
|
|
31
|
+
enviro.api
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def file_manager
|
|
35
|
+
enviro.file_manager
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def logger
|
|
39
|
+
enviro.logger
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
module Eco
|
|
2
|
+
module API
|
|
3
|
+
module Common
|
|
4
|
+
module Session
|
|
5
|
+
class Environment
|
|
6
|
+
|
|
7
|
+
include Common::People
|
|
8
|
+
|
|
9
|
+
attr_reader :config
|
|
10
|
+
attr_reader :api #, :host, :version
|
|
11
|
+
attr_reader :file_manager, :logger
|
|
12
|
+
|
|
13
|
+
alias_method :fm, :file_manager
|
|
14
|
+
|
|
15
|
+
def initialize(init = {})
|
|
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?(SessionConfig)
|
|
19
|
+
|
|
20
|
+
@config = init
|
|
21
|
+
@file_manager = Eco::API::Common::Session::FileManager.new(enviro: self)
|
|
22
|
+
@logger = Session::Logger.new(enviro: self)
|
|
23
|
+
new_api
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def new_api
|
|
27
|
+
return nil unless config.apis.active_api
|
|
28
|
+
|
|
29
|
+
log_connection = config.logger.log_connection?
|
|
30
|
+
log = log_connection ? @logger : ::Logger.new(IO::NULL)
|
|
31
|
+
|
|
32
|
+
api = config.api(log)
|
|
33
|
+
|
|
34
|
+
unless log_connection
|
|
35
|
+
logger.info("Created connection pointing to '#{config.apis.active_api.host}'")
|
|
36
|
+
api.logger.level = ::Logger::UNKNOWN
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
@api = api
|
|
40
|
+
api
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
module Eco
|
|
2
|
+
module API
|
|
3
|
+
module Common
|
|
4
|
+
module Session
|
|
5
|
+
class FileManager
|
|
6
|
+
|
|
7
|
+
include Eco::Data::Files
|
|
8
|
+
|
|
9
|
+
attr_reader :dir, :dir_path
|
|
10
|
+
attr_accessor :timestamp_pattern
|
|
11
|
+
|
|
12
|
+
def initialize(init = {}, enviro: nil)
|
|
13
|
+
@enviro = enviro
|
|
14
|
+
init = @enviro.config if @enviro && init.empty?
|
|
15
|
+
@timestamp_pattern = init.files.timestamp_pattern || DEFAULT_TIMESTAMP_PATTERN
|
|
16
|
+
self.dir_path = init.files.working_directory || Dir.pwd
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def dir_path=(value)
|
|
20
|
+
begin
|
|
21
|
+
@dir = Eco::Data::Files::Directory.new(value)
|
|
22
|
+
@dir_path = @dir.create
|
|
23
|
+
rescue
|
|
24
|
+
logger.error("could not create or make any sense of directory '#{value}'")
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def logger
|
|
29
|
+
@enviro&.logger || ::Logger.new(IO::NULL)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
##### FILE #####
|
|
33
|
+
def file(filename, should_exist: false)
|
|
34
|
+
dir.file(filename, should_exist: should_exit)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def newest(filename)
|
|
38
|
+
dir.newest_file(file: filename)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def file_content(filename)
|
|
42
|
+
file = dir.file(filename, should_exist: true)
|
|
43
|
+
if !file
|
|
44
|
+
logger.error("Can't read from file '#{filename}' because it does not exist.")
|
|
45
|
+
return nil
|
|
46
|
+
end
|
|
47
|
+
logger.debug("Reading from file '#{file}'")
|
|
48
|
+
File.read(file)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def load_json(filename)
|
|
52
|
+
content = file_content(filename)
|
|
53
|
+
return content && JSON.parse(content)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def touch(filename, modifier = :no_stamp, mode: :string)
|
|
57
|
+
save("", filename, modifier, mode: mode)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def save(content, filename, modifier = :no_stamp, mode: :string)
|
|
61
|
+
file = dir.file(filename)
|
|
62
|
+
file = FileManager.timestamp_file(file) if modifier == :timestamp
|
|
63
|
+
mode = (mode == :binary) ? 'wb' : 'w'
|
|
64
|
+
|
|
65
|
+
FileManager.create_directory(FileManager.file_fullpath(file))
|
|
66
|
+
|
|
67
|
+
logger.debug("Writting to file '#{file}'")
|
|
68
|
+
File.open(file, mode) { |fd| fd << content }
|
|
69
|
+
return file
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def save_json(data, filename, modifier = :no_stamp)
|
|
73
|
+
return save(data.to_json, filename, modifier)
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# if the file does not exist, it creates it
|
|
77
|
+
def append(content, filename, mode: :string)
|
|
78
|
+
file = dir.file(filename)
|
|
79
|
+
mode = (mode == :binary) ? 'ab' : 'a'
|
|
80
|
+
|
|
81
|
+
logger.debug("Appending to file '#{file}'")
|
|
82
|
+
File.open(file, mode) { |fd| fd << content + "\n" } # '\n' won't add line
|
|
83
|
+
return file
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
end
|