eco-helpers 1.5.1 → 1.5.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (173) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +82 -2
  3. data/LICENSE +21 -0
  4. data/eco-helpers.gemspec +1 -1
  5. data/lib/eco/api.rb +2 -0
  6. data/lib/eco/api/common.rb +4 -0
  7. data/lib/eco/api/common/base_loader.rb +54 -0
  8. data/lib/eco/api/common/class_auto_loader.rb +109 -0
  9. data/lib/eco/api/common/class_helpers.rb +33 -0
  10. data/lib/eco/api/common/class_hierarchy.rb +1 -1
  11. data/lib/eco/api/common/class_meta_basics.rb +16 -0
  12. data/lib/eco/api/common/loaders.rb +13 -0
  13. data/lib/eco/api/common/loaders/error_handler.rb +41 -0
  14. data/lib/eco/api/common/loaders/parser.rb +127 -0
  15. data/lib/eco/api/common/loaders/policy.rb +25 -0
  16. data/lib/eco/api/common/loaders/use_case.rb +40 -0
  17. data/lib/eco/api/common/people/default_parsers.rb +3 -12
  18. data/lib/eco/api/common/people/default_parsers/boolean_parser.rb +13 -23
  19. data/lib/eco/api/common/people/default_parsers/csv_parser.rb +20 -35
  20. data/lib/eco/api/common/people/default_parsers/date_parser.rb +15 -26
  21. data/lib/eco/api/common/people/default_parsers/freemium_parser.rb +15 -25
  22. data/lib/eco/api/common/people/default_parsers/login_providers_parser.rb +26 -0
  23. data/lib/eco/api/common/people/default_parsers/multi_parser.rb +15 -27
  24. data/lib/eco/api/common/people/default_parsers/numeric_parser.rb +14 -19
  25. data/lib/eco/api/common/people/default_parsers/policy_groups_parser.rb +24 -35
  26. data/lib/eco/api/common/people/default_parsers/send_invites_parser.rb +15 -25
  27. data/lib/eco/api/common/people/entries.rb +54 -24
  28. data/lib/eco/api/common/people/entry_factory.rb +10 -8
  29. data/lib/eco/api/common/people/person_attribute_parser.rb +29 -12
  30. data/lib/eco/api/common/people/person_entry.rb +308 -216
  31. data/lib/eco/api/common/people/person_entry_attribute_mapper.rb +3 -2
  32. data/lib/eco/api/common/people/person_parser.rb +51 -18
  33. data/lib/eco/api/common/session/logger.rb +4 -0
  34. data/lib/eco/api/common/version_patches/ecoportal_api/external_person.rb +2 -0
  35. data/lib/eco/api/common/version_patches/ecoportal_api/internal_person.rb +1 -1
  36. data/lib/eco/api/common/version_patches/exception.rb +22 -0
  37. data/lib/eco/api/custom.rb +13 -0
  38. data/lib/eco/api/custom/error_handler.rb +20 -0
  39. data/lib/eco/api/custom/namespace.rb +7 -0
  40. data/lib/eco/api/custom/parser.rb +50 -0
  41. data/lib/eco/api/custom/policy.rb +28 -0
  42. data/lib/eco/api/custom/use_case.rb +16 -0
  43. data/lib/eco/api/error.rb +1 -0
  44. data/lib/eco/api/error/handlers.rb +10 -3
  45. data/lib/eco/api/microcases.rb +17 -13
  46. data/lib/eco/api/microcases/account_excluded.rb +24 -0
  47. data/lib/eco/api/microcases/append_usergroups.rb +19 -0
  48. data/lib/eco/api/microcases/core_excluded.rb +4 -4
  49. data/lib/eco/api/microcases/{set_default_group.rb → fix_default_group.rb} +10 -9
  50. data/lib/eco/api/microcases/fix_filter_tags.rb +26 -6
  51. data/lib/eco/api/microcases/people_cache.rb +17 -0
  52. data/lib/eco/api/microcases/people_load.rb +59 -0
  53. data/lib/eco/api/microcases/people_refresh.rb +31 -0
  54. data/lib/eco/api/microcases/people_search.rb +65 -0
  55. data/lib/eco/api/microcases/refresh_abilities.rb +19 -0
  56. data/lib/eco/api/microcases/refresh_default_tag.rb +27 -0
  57. data/lib/eco/api/microcases/s3upload_targets.rb +39 -0
  58. data/lib/eco/api/microcases/set_account.rb +7 -19
  59. data/lib/eco/api/microcases/set_core.rb +5 -5
  60. data/lib/eco/api/microcases/set_core_with_supervisor.rb +23 -0
  61. data/lib/eco/api/microcases/set_supervisor.rb +17 -13
  62. data/lib/eco/api/microcases/strict_search.rb +12 -7
  63. data/lib/eco/api/microcases/with_each.rb +27 -0
  64. data/lib/eco/api/microcases/with_each_leaver.rb +24 -0
  65. data/lib/eco/api/microcases/with_each_present.rb +30 -0
  66. data/lib/eco/api/microcases/with_each_starter.rb +30 -0
  67. data/lib/eco/api/microcases/with_each_subordinate.rb +34 -0
  68. data/lib/eco/api/microcases/with_supervisor.rb +36 -0
  69. data/lib/eco/api/organization/people.rb +72 -35
  70. data/lib/eco/api/organization/presets_factory.rb +13 -4
  71. data/lib/eco/api/policies.rb +11 -7
  72. data/lib/eco/api/session.rb +54 -24
  73. data/lib/eco/api/session/batch.rb +1 -1
  74. data/lib/eco/api/session/batch/base_policy.rb +7 -6
  75. data/lib/eco/api/session/batch/errors.rb +28 -4
  76. data/lib/eco/api/session/batch/feedback.rb +7 -1
  77. data/lib/eco/api/session/batch/job.rb +40 -23
  78. data/lib/eco/api/session/batch/jobs.rb +9 -4
  79. data/lib/eco/api/session/batch/jobs_groups.rb +1 -1
  80. data/lib/eco/api/session/batch/request_stats.rb +91 -58
  81. data/lib/eco/api/session/batch/status.rb +35 -31
  82. data/lib/eco/api/session/config.rb +104 -42
  83. data/lib/eco/api/session/config/api.rb +17 -6
  84. data/lib/eco/api/session/config/logger.rb +2 -2
  85. data/lib/eco/api/session/config/post_launch.rb +1 -1
  86. data/lib/eco/api/session/config/workflow.rb +8 -7
  87. data/lib/eco/api/usecases.rb +47 -33
  88. data/lib/eco/api/usecases/backup/append_usergroups_case.rb +36 -0
  89. data/lib/eco/api/usecases/backup/create_case.rb +104 -0
  90. data/lib/eco/api/usecases/backup/create_details_case.rb +31 -0
  91. data/lib/eco/api/usecases/backup/create_details_with_supervisor_case.rb +48 -0
  92. data/lib/eco/api/usecases/backup/hris_case.rb +124 -0
  93. data/lib/eco/api/usecases/backup/set_default_tag_case.rb +49 -0
  94. data/lib/eco/api/usecases/backup/set_supervisor_case.rb +41 -0
  95. data/lib/eco/api/usecases/backup/transfer_account_case.rb +90 -0
  96. data/lib/eco/api/usecases/backup/update_case.rb +112 -0
  97. data/lib/eco/api/usecases/backup/update_details_case.rb +64 -0
  98. data/lib/eco/api/usecases/backup/upsert_case.rb +114 -0
  99. data/lib/eco/api/usecases/base_case.rb +2 -0
  100. data/lib/eco/api/usecases/base_io.rb +3 -3
  101. data/lib/eco/api/usecases/default_cases.rb +23 -53
  102. data/lib/eco/api/usecases/default_cases/append_usergroups_case.rb +10 -31
  103. data/lib/eco/api/usecases/default_cases/change_email_case.rb +23 -47
  104. data/lib/eco/api/usecases/default_cases/codes_to_tags_case.rb +56 -43
  105. data/lib/eco/api/usecases/default_cases/create_case.rb +15 -101
  106. data/lib/eco/api/usecases/default_cases/create_details_case.rb +11 -26
  107. data/lib/eco/api/usecases/default_cases/create_details_with_supervisor_case.rb +12 -43
  108. data/lib/eco/api/usecases/default_cases/delete_sync_case.rb +11 -0
  109. data/lib/eco/api/usecases/default_cases/delete_trans_case.rb +14 -0
  110. data/lib/eco/api/usecases/default_cases/email_as_id_case.rb +10 -21
  111. data/lib/eco/api/usecases/default_cases/hris_case.rb +23 -120
  112. data/lib/eco/api/usecases/default_cases/new_email_case.rb +10 -23
  113. data/lib/eco/api/usecases/default_cases/new_id_case.rb +11 -25
  114. data/lib/eco/api/usecases/default_cases/new_id_case0.rb +14 -0
  115. data/lib/eco/api/usecases/default_cases/org_data_convert_case.rb +83 -0
  116. data/lib/eco/api/usecases/default_cases/refresh_abilities_case.rb +30 -0
  117. data/lib/eco/api/usecases/default_cases/refresh_case.rb +7 -20
  118. data/lib/eco/api/usecases/default_cases/reinvite_sync_case.rb +11 -0
  119. data/lib/eco/api/usecases/default_cases/reinvite_trans_case.rb +17 -0
  120. data/lib/eco/api/usecases/default_cases/remove_account_sync_case.rb +11 -0
  121. data/lib/eco/api/usecases/default_cases/remove_account_trans_case.rb +17 -0
  122. data/lib/eco/api/usecases/default_cases/reset_landing_page_case.rb +9 -19
  123. data/lib/eco/api/usecases/default_cases/restore_db_case.rb +92 -0
  124. data/lib/eco/api/usecases/default_cases/set_default_tag_case.rb +32 -40
  125. data/lib/eco/api/usecases/default_cases/set_supervisor_case.rb +15 -33
  126. data/lib/eco/api/usecases/default_cases/switch_supervisor_case.rb +66 -57
  127. data/lib/eco/api/usecases/default_cases/to_csv_case.rb +36 -44
  128. data/lib/eco/api/usecases/default_cases/to_csv_detailed_case.rb +40 -55
  129. data/lib/eco/api/usecases/default_cases/transfer_account_case.rb +264 -84
  130. data/lib/eco/api/usecases/default_cases/update_case.rb +15 -109
  131. data/lib/eco/api/usecases/default_cases/update_details_case.rb +14 -61
  132. data/lib/eco/api/usecases/default_cases/upsert_case.rb +16 -111
  133. data/lib/eco/api/usecases/use_case_io.rb +9 -9
  134. data/lib/eco/cli/config.rb +10 -2
  135. data/lib/eco/cli/config/default.rb +2 -1
  136. data/lib/eco/cli/config/default/input_filters.rb +58 -0
  137. data/lib/eco/cli/config/default/options.rb +60 -25
  138. data/lib/eco/cli/config/default/people.rb +4 -4
  139. data/lib/eco/cli/config/default/people_filters.rb +108 -0
  140. data/lib/eco/cli/config/default/usecases.rb +69 -32
  141. data/lib/eco/cli/config/default/workflow.rb +37 -27
  142. data/lib/eco/cli/config/filters.rb +50 -0
  143. data/lib/eco/cli/config/filters/input_filters.rb +29 -0
  144. data/lib/eco/cli/config/filters/people_filters.rb +29 -0
  145. data/lib/eco/cli/config/help.rb +49 -0
  146. data/lib/eco/cli/config/options_set.rb +17 -1
  147. data/lib/eco/cli/config/use_cases.rb +79 -53
  148. data/lib/eco/cli/scripting.rb +10 -2
  149. data/lib/eco/cli/scripting/args_helpers.rb +25 -15
  150. data/lib/eco/cli/scripting/argument.rb +1 -0
  151. data/lib/eco/cli/scripting/arguments.rb +1 -1
  152. data/lib/eco/csv/table.rb +1 -1
  153. data/lib/eco/data/crypto/encryption.rb +3 -0
  154. data/lib/eco/language/match.rb +19 -9
  155. data/lib/eco/language/match_modifier.rb +13 -5
  156. data/lib/eco/language/models/collection.rb +77 -56
  157. data/lib/eco/language/models/parser_serializer.rb +39 -15
  158. data/lib/eco/version.rb +1 -1
  159. metadata +63 -18
  160. data/lib/eco/api/microcases/set_default_tag.rb +0 -23
  161. data/lib/eco/api/session/task.rb +0 -175
  162. data/lib/eco/api/usecases/default_case.rb +0 -19
  163. data/lib/eco/api/usecases/default_cases/delete_case.rb +0 -32
  164. data/lib/eco/api/usecases/default_cases/recover_db_case.rb +0 -99
  165. data/lib/eco/api/usecases/default_cases/refresh_presets_case.rb +0 -26
  166. data/lib/eco/api/usecases/default_cases/reinvite_case.rb +0 -41
  167. data/lib/eco/api/usecases/default_cases/remove_account_case.rb +0 -38
  168. data/lib/eco/api/usecases/microed_cases/hris_case.rb +0 -53
  169. data/lib/eco/api/usecases/microed_cases/update_case.rb +0 -33
  170. data/lib/eco/api/usecases/microed_cases/update_details_case.rb +0 -30
  171. data/lib/eco/api/usecases/microed_cases/upsert_case.rb +0 -36
  172. data/lib/eco/cli/config/default/filters.rb +0 -70
  173. data/lib/eco/cli/config/people_filters.rb +0 -38
@@ -0,0 +1,127 @@
1
+ module Eco
2
+ module API
3
+ module Common
4
+ module Loaders
5
+ class Parser < Eco::API::Common::BaseLoader
6
+
7
+ class << self
8
+ attr_reader :active_when
9
+
10
+ # @param value [String, Symbol] the attribute or type to be parsed/serialized
11
+ # 1. when `String`: the internal name of the field/attribute this parser/serializer manages for.
12
+ # 2. when `Symbol`: the type of data this parser converts between `String` and the specific type.
13
+ # @return [String] the `type` of usecase (i.e. `:sync`, `:transform`, `:import`, `:other`)
14
+ def attribute(value = nil)
15
+ unless value
16
+ return @attribute || raise("You should specify the 'attribute' this parser/serializer, #{self.class}, is linked to")
17
+ end
18
+ name value
19
+ @attribute = value
20
+ end
21
+
22
+ # Some parsers require dependencies to do their job.
23
+ def dependencies(value = nil)
24
+ @dependencies ||= {}
25
+ return @dependencies unless value
26
+ @dependencies = value
27
+ end
28
+
29
+ # Define or get the `phase` that the `parser` kicks in.
30
+ # @param phase [Symbol] the phase when this parser should be active.
31
+ # Must be one of [`:internal`, `:final`]
32
+ def parsing_phase(phase = nil)
33
+ @parsing_phase ||= :internal
34
+ return @parsing_phase unless phase
35
+ @parsing_phase = phase
36
+ end
37
+
38
+ # Define or get the `phase` that the `serializer` kicks in.
39
+ # @param phase [Symbol] the phase when this serializer should be active.
40
+ # Must be one of [`:person,` `:final`, `:internal`]
41
+ def serializing_phase(phase = nil)
42
+ @serializing_phase ||= :person
43
+ return @serializing_phase unless phase
44
+ @serializing_phase = phase
45
+ end
46
+
47
+ # Helper to build the `active_when` condition.
48
+ def active_when_any(*attrs)
49
+ @active_when = Proc.new do |source_data|
50
+ keys = data_keys(source_data)
51
+ attrs.any? {|key| keys.include?(key)}
52
+ end
53
+ end
54
+
55
+ # Helper to build the `active_when` condition.
56
+ def active_when_all(*attrs)
57
+ @active_when = Proc.new do |source_data|
58
+ keys = data_keys(source_data)
59
+ attrs.all? {|key| keys.include?(key)}
60
+ end
61
+ end
62
+
63
+ # Helper to obtain the current internal named attributes of the data
64
+ # @param source_data [Array<String>, Hash] if `Array` those are already the `keys`, if `Hash` it gets the `keys`
65
+ # @return [Array<String>] `keys` of `source_data`
66
+ def data_keys(source_data)
67
+ case source_data
68
+ when Array
69
+ keys = source_data
70
+ when Hash
71
+ keys = source_data.keys
72
+ else
73
+ keys = []
74
+ end
75
+ end
76
+
77
+ end
78
+
79
+ def initialize(person_parser)
80
+ raise "Expected Eco::API::Common::People::PersonParser. Given #{policies.class}" unless person_parser.is_a?(Eco::API::Common::People::PersonParser)
81
+ person_parser.define_attribute(self.attribute, dependencies: self.class.dependencies) do |attr_parser|
82
+ _define_parser(attr_parser)
83
+ _define_serializer(attr_parser)
84
+ end
85
+ end
86
+
87
+ # @param data [Hash] all the person data at the specified `parsing_phase`:
88
+ # - when `:internal`: the parser will receive external types (i.e. String with '|' delimiters instead of an Array).
89
+ # - when `:final`: the parser will receive the typed values (i.e. Array instread of String with '|' delimiters).
90
+ # @param deps [Hash] the merged dependencies (default to the class object and when calling the parser).
91
+ def parser(data, deps)
92
+ raise "You should implement this method"
93
+ end
94
+
95
+ # @param data [Hash, Ecoportal::API::V1::Person] all the person data at the specified `serializing_phase`:
96
+ # - when `:internal`: it will receive a `Hash` with the **internal values** but the types already serialized to `String`.
97
+ # - when `:final`: it will receive a `Hash` with the **internal values** and **types**.
98
+ # - when `:person`: it will receive the `person` object.
99
+ # @param deps [Hash] the merged dependencies (default to the class object and when calling the parser).
100
+ def seralizer(data, deps)
101
+ raise "You should implement this method"
102
+ end
103
+
104
+ # @return [String, Symbol] the field/attribute or type this parser is linked to.
105
+ def attribute
106
+ self.class.attribute
107
+ end
108
+
109
+ private
110
+
111
+ def _define_parser(attr_parser)
112
+ if active_when = self.class.active_when
113
+ attr_parser.def_parser(self.class.parsing_phase, active_when: active_when, &self.method(:parser))
114
+ else
115
+ attr_parser.def_parser(self.class.parsing_phase, &self.method(:parser))
116
+ end
117
+ end
118
+
119
+ def _define_serializer(attr_parser)
120
+ attr_parser.def_serializer(self.class.serializing_phase, &self.method(:serializer))
121
+ end
122
+
123
+ end
124
+ end
125
+ end
126
+ end
127
+ end
@@ -0,0 +1,25 @@
1
+ module Eco
2
+ module API
3
+ module Common
4
+ module Loaders
5
+ class Policy < Eco::API::Common::BaseLoader
6
+
7
+ def initialize(policies)
8
+ raise "Expected Eco::API::Policies. Given #{policies.class}" unless policies.is_a?(Eco::API::Policies)
9
+ policies.define(self.name, &self.method(:main))
10
+ end
11
+
12
+ # @param people [Eco::API::Organization::People] the people in the queue of the current `job`
13
+ # @param session [Eco::API::Session] the current session where the usecase kicks in.
14
+ # @param options [Hash] the options that modify the case behaviour or bring some dependencies.
15
+ # @param policy [Eco::API::Policies::Policy] the `policy` instance object.
16
+ # @param job [Eco::API::Session::Batch::Job] the `Batch::Job` that these `people` belong to the queue thereof.
17
+ def main(people, session, options, policy, job)
18
+ raise "You should implement this method"
19
+ end
20
+
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,40 @@
1
+ module Eco
2
+ module API
3
+ module Common
4
+ module Loaders
5
+ class UseCase < Eco::API::Common::BaseLoader
6
+
7
+ class << self
8
+ # @return [Symbol] the `type` of usecase (i.e. `:sync`, `:transform`, `:import`, `:other`)
9
+ def type(value = nil)
10
+ unless value
11
+ return @type || raise("You should specify a type of case [:sync, :transform, :import, :other] for #{self.class}")
12
+ end
13
+ @type = value
14
+ end
15
+ end
16
+
17
+ def initialize(usecases)
18
+ raise "Expected Eco::API::UseCases. Given #{usecases.class}" unless usecases.is_a?(Eco::API::UseCases)
19
+ usecases.define(self.name, type: self.type, &self.method(:main))
20
+ end
21
+
22
+ # The parameters of this method will depend on the `type` of usecase.
23
+ # @param entries [Eco::API::Common::People::Entries] the input entries with the data.
24
+ # @param people [Eco::API::Organization::People] the people in the queue of the current `job`
25
+ # @param session [Eco::API::Session] the current session where the usecase kicks in.
26
+ # @param options [Hash] the options that modify the case behaviour or bring some dependencies.
27
+ # @param usecase [Eco::API::Policies::Policy] the `usecase` instance object.
28
+ def main(entries, people, session, options, usecase)
29
+ raise "You should implement this method"
30
+ end
31
+
32
+ def type
33
+ self.class.type
34
+ end
35
+
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -5,6 +5,8 @@ module Eco
5
5
 
6
6
  # Class to define a set of default attribute parsers
7
7
  class DefaultParsers < PersonParser
8
+ autoloads_children_of "Eco::API::Common::Loaders::Parser"
9
+ autoload_namespace "Eco::API::Common::People::DefaultParsers"
8
10
 
9
11
  def initialize(*args)
10
12
  super(*args)
@@ -24,18 +26,6 @@ module Eco
24
26
  end.compact.to_h
25
27
 
26
28
  SelectParser.new(self, select_hashes: select_hashes).process
27
- BooleanParser.new(self).process
28
- NumericParser.new(self).process
29
- DateParser.new(self).process
30
- MultiParser.new(self).process
31
-
32
- # ALWAYS PRESENT FIELDS
33
- SendInvitesParser.new(self).process
34
- FreemiumParser.new(self).process
35
- PolicyGroupsParser.new(self).process
36
-
37
- # FORMAT PARSER
38
- CSVParser.new(self).process
39
29
  end
40
30
 
41
31
  end
@@ -52,4 +42,5 @@ require_relative 'default_parsers/multi_parser'
52
42
  require_relative 'default_parsers/send_invites_parser'
53
43
  require_relative 'default_parsers/freemium_parser'
54
44
  require_relative 'default_parsers/policy_groups_parser'
45
+ require_relative 'default_parsers/login_providers_parser'
55
46
  require_relative 'default_parsers/csv_parser'
@@ -1,28 +1,18 @@
1
- module Eco
2
- module API
3
- module Common
4
- module People
5
- class DefaultParsers
6
- class BooleanParser < People::BaseParser
1
+ class Eco::API::Common::People::DefaultParsers::BooleanParser < Eco::API::Common::Loaders::Parser
2
+ attribute :boolean
7
3
 
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
4
+ def parser(value, deps)
5
+ value.is_a?(Array) ? value.map { |v| truthy(v) } : truthy(value)
6
+ end
7
+
8
+ def serializer(value, deps)
9
+ value.to_s
10
+ end
17
11
 
18
- private
12
+ private
19
13
 
20
- def truthy (value)
21
- %w[true y yes x].include?(value.to_s.strip.downcase)
22
- end
23
- end
24
- end
25
- end
26
- end
14
+ def truthy (value)
15
+ %w[true y yes x].include?(value.to_s.strip.downcase)
27
16
  end
17
+
28
18
  end
@@ -1,41 +1,26 @@
1
- module Eco
2
- module API
3
- module Common
4
- module People
5
- class DefaultParsers
6
- class CSVParser < People::BaseParser
1
+ class Eco::API::Common::People::DefaultParsers::CSVParser < Eco::API::Common::Loaders::Parser
2
+ attribute :csv
7
3
 
8
- def process
9
- @parsers.define_attribute(:csv, dependencies: @options) do |parser|
10
- parser.def_parser do |data, deps|
11
- arr_hash = []
12
- CSV.parse(data, headers: true, skip_blanks: true).reject do |row|
13
- values = row.to_hash.values
14
- values.all?(&:nil?) || values.map(&:to_s).all?(&:empty?)
15
- end.each do |row|
16
- row_hash = row.headers.uniq.each_with_object({}) do |attr, hash|
17
- value = row[attr]
18
- hash[attr] = value.to_s.empty?? nil : value
19
- end
20
- arr_hash.push(row_hash)
21
- end
22
- arr_hash
23
- end.def_serializer do |array_hash, deps|
24
- arr_rows = []
25
- unless array_hash.empty?
26
- header = array_hash.first.keys
27
- arr_rows = array_hash.map do |csv_row|
28
- CSV::Row.new(header, csv_row.values_at(*header))
29
- end
30
- end
31
- CSV::Table.new(arr_rows).to_csv
32
- end
33
- end
34
- end
4
+ def parser(data, deps)
5
+ Eco::CSV.parse(data, headers: true, skip_blanks: true).each_with_object([]) do |row, arr_hash|
6
+ row_hash = row.headers.uniq.each_with_object({}) do |attr, hash|
7
+ next if attr.to_s.strip.empty?
8
+ value = row[attr]
9
+ hash[attr.strip] = value.to_s.empty?? nil : value
10
+ end
11
+ arr_hash.push(row_hash)
12
+ end
13
+ end
35
14
 
36
- end
37
- end
15
+ def serializer(array_hash, deps)
16
+ arr_rows = []
17
+ unless array_hash.empty?
18
+ header = array_hash.first.keys
19
+ arr_rows = array_hash.map do |csv_row|
20
+ CSV::Row.new(header, csv_row.values_at(*header))
38
21
  end
39
22
  end
23
+ CSV::Table.new(arr_rows).to_csv
40
24
  end
25
+
41
26
  end
@@ -1,33 +1,22 @@
1
- module Eco
2
- module API
3
- module Common
4
- module People
5
- class DefaultParsers
6
- class DateParser < People::BaseParser
1
+ class Eco::API::Common::People::DefaultParsers::DateParser < Eco::API::Common::Loaders::Parser
2
+ attribute :date
7
3
 
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
4
+ def parser(value, deps)
5
+ value.is_a?(Array) ? value.map { |v| parse_date(v) } : parse_date(value)
6
+ end
17
7
 
18
- private
8
+ def serializer(value, deps)
9
+ value.is_a?(Array) ? value.map { |d| d && d.strftime('%Y-%m-%d') } : value && value.strftime('%Y-%m-%d')
10
+ end
19
11
 
20
- def parse_date(value)
21
- begin
22
- (value.to_s.empty?) ? nil : Date.parse(value)
23
- rescue
24
- nil
25
- end
26
- end
12
+ private
27
13
 
28
- end
29
- end
30
- end
14
+ def parse_date(value)
15
+ begin
16
+ (value.to_s.empty?) ? nil : Date.parse(value)
17
+ rescue
18
+ nil
31
19
  end
32
20
  end
21
+
33
22
  end
@@ -1,30 +1,20 @@
1
- module Eco
2
- module API
3
- module Common
4
- module People
5
- class DefaultParsers
6
- class FreemiumParser < People::BaseParser
1
+ class Eco::API::Common::People::DefaultParsers::FreemiumParser < Eco::API::Common::Loaders::Parser
2
+ attribute "freemium"
7
3
 
8
- def process
9
- @parsers.define_attribute("freemium") do |parser|
10
- parser.def_parser do |hash|
11
- value = hash["freemium"]
12
- value = value.first if value.is_a?(Array)
13
- truthy(value)
14
- end.def_serializer do |person|
15
- person.freemium&.to_s
16
- end
17
- end
18
- end
4
+ def parser(hash, deps)
5
+ value = hash["freemium"]
6
+ value = value.first if value.is_a?(Array)
7
+ truthy(value)
8
+ end
9
+
10
+ def serializer(person, deps)
11
+ person.freemium&.to_s
12
+ end
19
13
 
20
- private
14
+ private
21
15
 
22
- def truthy (value)
23
- %w[true y yes x].include?(value.to_s&.downcase)
24
- end
25
- end
26
- end
27
- end
28
- end
16
+ def truthy (value)
17
+ %w[true y yes x].include?(value.to_s&.downcase)
29
18
  end
19
+
30
20
  end
@@ -0,0 +1,26 @@
1
+ class Eco::API::Common::People::DefaultParsers::LoginProvidersParser < Eco::API::Common::Loaders::Parser
2
+ attribute "login_provider_ids"
3
+ parsing_phase :final
4
+ serializing_phase :final
5
+
6
+ def parser(hash, deps)
7
+ hash["login_provider_ids"].map do |name|
8
+ login_providers.to_id(name&.downcase.strip)
9
+ end.compact
10
+ end
11
+
12
+ def serializer(hash, deps)
13
+ if ids = hash["login_provider_ids"]
14
+ login_providers.to_name(ids)
15
+ else
16
+ []
17
+ end
18
+ end
19
+
20
+ private
21
+
22
+ def login_providers
23
+ @login_providers ||= ASSETS.config.login_providers
24
+ end
25
+
26
+ end