ecoportal-api 0.10.7 → 0.10.9
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 +4 -4
- data/CHANGELOG.md +30 -1
- data/lib/ecoportal/api/common/base_class.rb +11 -12
- data/lib/ecoportal/api/common/base_model.rb +22 -8
- data/lib/ecoportal/api/common/batch_operation.rb +10 -10
- data/lib/ecoportal/api/common/batch_response.rb +1 -1
- data/lib/ecoportal/api/common/client/elastic_apm_integration.rb +9 -9
- data/lib/ecoportal/api/common/client/error/checks.rb +1 -1
- data/lib/ecoportal/api/common/client/with_retry.rb +1 -1
- data/lib/ecoportal/api/common/client.rb +13 -14
- data/lib/ecoportal/api/common/doc_helpers.rb +7 -6
- data/lib/ecoportal/api/common/hash_diff.rb +8 -5
- data/lib/ecoportal/api/common/logging.rb +1 -0
- data/lib/ecoportal/api/common/response.rb +1 -1
- data/lib/ecoportal/api/common/wrapped_response.rb +1 -1
- data/lib/ecoportal/api/internal/account.rb +18 -18
- data/lib/ecoportal/api/internal/people.rb +1 -1
- data/lib/ecoportal/api/internal/person.rb +24 -18
- data/lib/ecoportal/api/internal/person_details.rb +1 -1
- data/lib/ecoportal/api/internal/person_schema.rb +1 -1
- data/lib/ecoportal/api/internal/person_schemas.rb +1 -1
- data/lib/ecoportal/api/internal/policy_groups.rb +3 -2
- data/lib/ecoportal/api/internal/preferences.rb +13 -10
- data/lib/ecoportal/api/internal.rb +5 -5
- data/lib/ecoportal/api/logger.rb +8 -7
- data/lib/ecoportal/api/v1/job/awaiter/timer.rb +9 -7
- data/lib/ecoportal/api/v1/job/status.rb +2 -2
- data/lib/ecoportal/api/v1/job.rb +4 -4
- data/lib/ecoportal/api/v1/people.rb +12 -10
- data/lib/ecoportal/api/v1/person.rb +27 -15
- data/lib/ecoportal/api/v1/person_details.rb +7 -7
- data/lib/ecoportal/api/v1/person_schema.rb +5 -4
- data/lib/ecoportal/api/v1/person_schemas.rb +9 -6
- data/lib/ecoportal/api/v1/schema_field.rb +8 -8
- data/lib/ecoportal/api/v1/schema_field_value.rb +15 -15
- data/lib/ecoportal/api/v1.rb +4 -4
- data/lib/ecoportal/api/version.rb +1 -1
- data/lib/ecoportal/api.rb +9 -9
- metadata +2 -2
@@ -3,24 +3,27 @@ module Ecoportal
|
|
3
3
|
class Internal
|
4
4
|
# @attr account [Account, nil] the account of the person or `nil` if missing.
|
5
5
|
class Person < V1::Person
|
6
|
-
class_resolver :person_details_class,
|
7
|
-
class_resolver :person_account_class,
|
6
|
+
class_resolver :person_details_class, 'Ecoportal::API::Internal::PersonDetails'
|
7
|
+
class_resolver :person_account_class, 'Ecoportal::API::Internal::Account'
|
8
8
|
embeds_one :account, nullable: true, klass: :person_account_class
|
9
9
|
|
10
10
|
def initialize(doc = {}, *args, **kargs, &block)
|
11
|
-
super
|
11
|
+
super
|
12
|
+
|
12
13
|
@is_new = @prev_is_new = no_account_nor_details?(@doc)
|
13
14
|
end
|
14
15
|
|
15
16
|
def consolidate!
|
16
17
|
@prev_is_new = @is_new
|
17
|
-
@is_new
|
18
|
+
@is_new = false
|
19
|
+
|
18
20
|
super
|
19
21
|
end
|
20
22
|
|
21
23
|
def reset!(*args)
|
22
24
|
@is_new = @prev_is_new
|
23
|
-
|
25
|
+
|
26
|
+
super
|
24
27
|
end
|
25
28
|
|
26
29
|
# @note
|
@@ -34,27 +37,28 @@ module Ecoportal
|
|
34
37
|
# @return [Boolean] whether or not this entry is being created now (i.e. non existent on server)
|
35
38
|
def new?(value = :original)
|
36
39
|
return @is_new if value == :original
|
40
|
+
|
37
41
|
no_account_nor_details?(initial_doc)
|
38
42
|
end
|
39
43
|
|
40
44
|
# @return [Boolean] if the account has been added to `doc`
|
41
45
|
def account_added?(value = :original)
|
42
|
-
ref_doc =
|
43
|
-
!!account && !ref_doc[
|
46
|
+
ref_doc = value == :original ? original_doc : initial_doc
|
47
|
+
!!account && !ref_doc['account']
|
44
48
|
end
|
45
49
|
|
46
50
|
# @return [Boolean] if the account has been removed from `doc`
|
47
51
|
def account_removed?(value = :original)
|
48
|
-
ref_doc =
|
49
|
-
!account && !!ref_doc[
|
52
|
+
ref_doc = value == :original ? original_doc : initial_doc
|
53
|
+
!account && !!ref_doc['account']
|
50
54
|
end
|
51
55
|
|
52
56
|
def as_json
|
53
|
-
super.update(
|
57
|
+
super.update('account' => account&.as_json)
|
54
58
|
end
|
55
59
|
|
56
60
|
def as_update(ref = :last, ignore: [])
|
57
|
-
super(ref, ignore: ignore | [
|
61
|
+
super(ref, ignore: ignore | %w[user_id permissions_merged prefilter])
|
58
62
|
end
|
59
63
|
|
60
64
|
# Sets the Account to the person, depending on the paramter received:
|
@@ -67,19 +71,20 @@ module Ecoportal
|
|
67
71
|
def account=(value)
|
68
72
|
case value
|
69
73
|
when NilClass
|
70
|
-
doc[
|
74
|
+
doc['account'] = nil
|
71
75
|
when Internal::Account
|
72
|
-
doc[
|
76
|
+
doc['account'] = JSON.parse(value.to_json)
|
73
77
|
when Hash
|
74
78
|
user_id = account.user_id if account
|
75
|
-
doc[
|
76
|
-
doc[
|
79
|
+
doc['account'] = value.slice(*Internal::Account::PROPERTIES)
|
80
|
+
doc['account']['user_id'] = user_id if user_id
|
77
81
|
else
|
78
82
|
# TODO
|
79
83
|
raise "Invalid set on account: Need nil, Account or Hash; got #{value.class}"
|
80
84
|
end
|
81
|
-
|
82
|
-
|
85
|
+
|
86
|
+
remove_instance_variable('@account') if defined?(@account)
|
87
|
+
account
|
83
88
|
end
|
84
89
|
|
85
90
|
# Adds an empty account to the person.
|
@@ -93,7 +98,8 @@ module Ecoportal
|
|
93
98
|
|
94
99
|
def no_account_nor_details?(value = @doc)
|
95
100
|
return true unless value.is_a?(Hash)
|
96
|
-
|
101
|
+
|
102
|
+
!value.key?('details') && !value.key?('account')
|
97
103
|
end
|
98
104
|
end
|
99
105
|
end
|
@@ -2,7 +2,7 @@ module Ecoportal
|
|
2
2
|
module API
|
3
3
|
class Internal
|
4
4
|
class PersonDetails < V1::PersonDetails
|
5
|
-
class_resolver :schema_field_value_class,
|
5
|
+
class_resolver :schema_field_value_class, 'Ecoportal::API::Internal::SchemaFieldValue'
|
6
6
|
end
|
7
7
|
end
|
8
8
|
end
|
@@ -15,13 +15,14 @@ module Ecoportal
|
|
15
15
|
|
16
16
|
# Gets all the policy groups via api request.
|
17
17
|
# @return [Enumerable<PolicyGroup>] an `Enumerable` with all the policy groups already wrapped as `PolicyGroup` objects.
|
18
|
-
def get_all
|
19
|
-
response = client.get(
|
18
|
+
def get_all # rubocop:disable Naming/AccessorMethodName
|
19
|
+
response = client.get('/policy_groups')
|
20
20
|
Common::WrappedResponse.new(response, Internal::PolicyGroup)
|
21
21
|
end
|
22
22
|
|
23
23
|
def each(&block)
|
24
24
|
return to_enum(:each) unless block
|
25
|
+
|
25
26
|
get_all.each(&block)
|
26
27
|
end
|
27
28
|
end
|
@@ -2,17 +2,19 @@ module Ecoportal
|
|
2
2
|
module API
|
3
3
|
class Internal
|
4
4
|
class Preferences < Common::BaseModel
|
5
|
-
passthrough
|
6
|
-
|
7
|
-
|
5
|
+
passthrough(
|
6
|
+
:kiosk_enabled, :kiosk_workflow_message, :kiosk_create_button_label,
|
7
|
+
:kiosk_create_button_help, :kiosk_return_button_label, :kiosk_return_button_help,
|
8
|
+
:kiosk_dashboard_button_label, :kiosk_dashboard_button_help
|
9
|
+
)
|
8
10
|
|
9
|
-
[
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
11
|
+
%i[
|
12
|
+
show_sidebar
|
13
|
+
show_shortcuts
|
14
|
+
show_coming_soon
|
15
|
+
show_recently_visited_forms
|
16
|
+
show_tasks
|
17
|
+
show_task_bubbles
|
16
18
|
].map(&:to_s).each do |field|
|
17
19
|
define_method(field) do
|
18
20
|
if doc.key?(field)
|
@@ -21,6 +23,7 @@ module Ecoportal
|
|
21
23
|
true
|
22
24
|
end
|
23
25
|
end
|
26
|
+
|
24
27
|
define_method("#{field}=") do |value|
|
25
28
|
doc[field] = !!value
|
26
29
|
end
|
@@ -3,12 +3,12 @@ module Ecoportal
|
|
3
3
|
class Internal < V1
|
4
4
|
include Common::Logging
|
5
5
|
|
6
|
-
VERSION =
|
7
|
-
class_resolver :people_class,
|
8
|
-
class_resolver :person_schemas_class,
|
6
|
+
VERSION = 'v0'.freeze
|
7
|
+
class_resolver :people_class, 'Ecoportal::API::Internal::People'
|
8
|
+
class_resolver :person_schemas_class, 'Ecoportal::API::Internal::PersonSchemas'
|
9
9
|
|
10
|
-
class_resolver :policy_groups_class,
|
11
|
-
class_resolver :login_providers_class,
|
10
|
+
class_resolver :policy_groups_class, 'Ecoportal::API::Internal::PolicyGroups'
|
11
|
+
class_resolver :login_providers_class, 'Ecoportal::API::Internal::LoginProviders'
|
12
12
|
|
13
13
|
# Obtain specific object for policy groups api requests.
|
14
14
|
# @return [PolicyGroups] an instance object ready to make policy groups api requests.
|
data/lib/ecoportal/api/logger.rb
CHANGED
@@ -1,26 +1,27 @@
|
|
1
1
|
module Ecoportal
|
2
2
|
module API
|
3
3
|
class Logger
|
4
|
-
TIMESTAMP_PATTERN =
|
4
|
+
TIMESTAMP_PATTERN = '%Y-%m-%dT%H:%M:%S'.freeze
|
5
5
|
|
6
6
|
STDOUT_FORMAT_PROC = proc do |severity, _datetime, _progname, msg|
|
7
7
|
prefix = "%5s > " % severity # rubocop:disable Style/FormatString
|
8
|
-
msg.lines.map.with_index do |line, idx|
|
8
|
+
msg.lines.map.with_index do |line, idx| # rubocop:disable Style/StringConcatenation
|
9
9
|
if idx.zero?
|
10
10
|
prefix + line.chomp
|
11
11
|
else
|
12
|
-
(
|
12
|
+
(' ' * prefix.length) + line.chomp
|
13
13
|
end
|
14
14
|
end.join("\n")+"\n"
|
15
15
|
end
|
16
16
|
|
17
17
|
FILE_FORMAT_PROC = proc do |severity, datetime, _progname, msg|
|
18
18
|
prefix = "%5s(%s) > " % [severity, datetime.strftime(TIMESTAMP_PATTERN)] # rubocop:disable Style/FormatString, Style/FormatStringToken
|
19
|
-
|
19
|
+
|
20
|
+
msg.lines.map.with_index do |line, idx| # rubocop:disable Style/StringConcatenation
|
20
21
|
if idx.zero?
|
21
22
|
prefix + line.chomp
|
22
23
|
else
|
23
|
-
(
|
24
|
+
(' ' * prefix.length) + line.chomp
|
24
25
|
end
|
25
26
|
end.join("\n")+"\n"
|
26
27
|
end
|
@@ -30,7 +31,7 @@ module Ecoportal
|
|
30
31
|
def initialize(
|
31
32
|
console_level: ::Logger::INFO,
|
32
33
|
file_level: ::Logger::DEBUG,
|
33
|
-
output_file: File.join(Dir.getwd, "API_Log-#{Time.now.strftime(
|
34
|
+
output_file: File.join(Dir.getwd, "API_Log-#{Time.now.strftime('%Y-%m-%dT%H%M')}.txt")
|
34
35
|
)
|
35
36
|
@console = make_stdout_logger(console_level)
|
36
37
|
@file = make_file_logger(file_level, output_file)
|
@@ -54,7 +55,7 @@ module Ecoportal
|
|
54
55
|
|
55
56
|
def make_file_logger(level, output_file)
|
56
57
|
return unless output_file
|
57
|
-
::Logger.new(File.open(output_file,
|
58
|
+
::Logger.new(File.open(output_file, 'a')).tap do |logger|
|
58
59
|
logger.formatter = FILE_FORMAT_PROC
|
59
60
|
logger.level = level
|
60
61
|
end
|
@@ -9,6 +9,7 @@ module Ecoportal
|
|
9
9
|
status lstatus
|
10
10
|
].freeze
|
11
11
|
|
12
|
+
# The class
|
12
13
|
Timer = Struct.new(*TIMER_ARGS) do
|
13
14
|
self::MAX_START_DELAY = 60
|
14
15
|
|
@@ -21,11 +22,7 @@ module Ecoportal
|
|
21
22
|
end
|
22
23
|
|
23
24
|
alias_method :original_start, :start
|
24
|
-
# `start` time only counts from the moment that
|
25
|
-
# it already started to progress
|
26
25
|
def start
|
27
|
-
return timestamp unless lstatus&.started?
|
28
|
-
|
29
26
|
original_start || timestamp
|
30
27
|
end
|
31
28
|
|
@@ -76,6 +73,10 @@ module Ecoportal
|
|
76
73
|
end
|
77
74
|
|
78
75
|
def time_left
|
76
|
+
(timeout_in - waited).round(2)
|
77
|
+
end
|
78
|
+
|
79
|
+
def time_left_total
|
79
80
|
(timeout - waited).round(2)
|
80
81
|
end
|
81
82
|
|
@@ -86,7 +87,7 @@ module Ecoportal
|
|
86
87
|
end
|
87
88
|
|
88
89
|
def timeout_in
|
89
|
-
return
|
90
|
+
return time_left_total if started?
|
90
91
|
|
91
92
|
time_left_to_start
|
92
93
|
end
|
@@ -94,7 +95,7 @@ module Ecoportal
|
|
94
95
|
# timeout library is evil. So we make poor-man timeout.
|
95
96
|
# https://jvns.ca/blog/2015/11/27/why-rubys-timeout-is-dangerous-and-thread-dot-raise-is-terrifying/
|
96
97
|
def timeout?
|
97
|
-
!
|
98
|
+
!time_left_total.positive?
|
98
99
|
end
|
99
100
|
|
100
101
|
def start_timeout?
|
@@ -140,7 +141,8 @@ module Ecoportal
|
|
140
141
|
%i[total timeout start].each_with_object({}) do |key, kargs|
|
141
142
|
kargs[key] = send(key)
|
142
143
|
end.tap do |kargs|
|
143
|
-
kargs[:start] =
|
144
|
+
kargs[:start] = start
|
145
|
+
kargs[:last] = timestamp
|
144
146
|
kargs[:lstatus] = status
|
145
147
|
end
|
146
148
|
end
|
data/lib/ecoportal/api/v1/job.rb
CHANGED
@@ -31,7 +31,7 @@ module Ecoportal
|
|
31
31
|
raise_if_already_launched! unless recover
|
32
32
|
|
33
33
|
@operation ||= Common::BatchOperation.new(
|
34
|
-
|
34
|
+
'/people',
|
35
35
|
person_class,
|
36
36
|
logger: client.logger
|
37
37
|
)
|
@@ -84,7 +84,7 @@ module Ecoportal
|
|
84
84
|
def raise_if_already_launched!
|
85
85
|
return unless created?
|
86
86
|
|
87
|
-
msg =
|
87
|
+
msg = 'Missusage: job was already created.'
|
88
88
|
msg << " Can't call batch more than once"
|
89
89
|
raise msg
|
90
90
|
end
|
@@ -102,8 +102,8 @@ module Ecoportal
|
|
102
102
|
|
103
103
|
job_id = nil
|
104
104
|
|
105
|
-
client.post(
|
106
|
-
job_id = body_data(response.body)[
|
105
|
+
client.post('/people/job', data: operation.as_json).tap do |response|
|
106
|
+
job_id = body_data(response.body)['id'] if response.success?
|
107
107
|
|
108
108
|
next if job_id
|
109
109
|
|
@@ -8,7 +8,7 @@ module Ecoportal
|
|
8
8
|
include Common::DocHelpers
|
9
9
|
include Enumerable
|
10
10
|
|
11
|
-
class_resolver :person_class,
|
11
|
+
class_resolver :person_class, 'Ecoportal::API::V1::Person'
|
12
12
|
|
13
13
|
attr_reader :client
|
14
14
|
|
@@ -44,7 +44,7 @@ module Ecoportal
|
|
44
44
|
count = 5
|
45
45
|
|
46
46
|
loop do
|
47
|
-
response = client.get(
|
47
|
+
response = client.get('/people', params: params)
|
48
48
|
body = response && body_data(response.body)
|
49
49
|
break if response.success? || count <= 0
|
50
50
|
|
@@ -56,22 +56,22 @@ module Ecoportal
|
|
56
56
|
|
57
57
|
raise "Request failed - Status #{response.status}: #{body}" unless response.success?
|
58
58
|
|
59
|
-
unless silent || (total = body[
|
60
|
-
results += body[
|
59
|
+
unless silent || (total = body['total_results'])&.zero?
|
60
|
+
results += body['results'].length
|
61
61
|
percent = results * 100 / total
|
62
62
|
|
63
|
-
msg =
|
63
|
+
msg = 'People GET'
|
64
64
|
msg << " (search=#{params[:q]})" if params.key?(:q)
|
65
65
|
|
66
66
|
print "#{msg}: #{percent.round}% (of #{total}): #{results}\r"
|
67
67
|
$stdout.flush
|
68
68
|
end
|
69
69
|
|
70
|
-
body[
|
70
|
+
body['results'].each do |person|
|
71
71
|
yield person_class.new(person)
|
72
72
|
end
|
73
73
|
|
74
|
-
break unless (cursor_id = body[
|
74
|
+
break unless (cursor_id = body['cursor_id'])
|
75
75
|
end
|
76
76
|
self
|
77
77
|
end
|
@@ -106,6 +106,7 @@ module Ecoportal
|
|
106
106
|
def update(doc)
|
107
107
|
body = get_body(doc)
|
108
108
|
id = get_id(doc)
|
109
|
+
|
109
110
|
client.patch("/people/#{CGI.escape(id)}", data: body)
|
110
111
|
end
|
111
112
|
|
@@ -114,7 +115,8 @@ module Ecoportal
|
|
114
115
|
# @return [Response] an object with the api response.
|
115
116
|
def create(doc)
|
116
117
|
body = get_body(doc)
|
117
|
-
|
118
|
+
|
119
|
+
client.post('/people', data: body)
|
118
120
|
end
|
119
121
|
|
120
122
|
# Requests to update an existing person or if it does not exist, to create it, via api.
|
@@ -143,7 +145,7 @@ module Ecoportal
|
|
143
145
|
return job.batch(&block) if job_mode
|
144
146
|
|
145
147
|
operation = Common::BatchOperation.new(
|
146
|
-
|
148
|
+
'/people',
|
147
149
|
person_class,
|
148
150
|
logger: client.logger
|
149
151
|
)
|
@@ -151,7 +153,7 @@ module Ecoportal
|
|
151
153
|
yield operation
|
152
154
|
|
153
155
|
# The batch operation is responsible for logging the output
|
154
|
-
client.post(
|
156
|
+
client.post('/people/batch', data: operation.as_json).tap do |response|
|
155
157
|
operation.process_response(response)
|
156
158
|
end
|
157
159
|
end
|
@@ -10,25 +10,35 @@ module Ecoportal
|
|
10
10
|
class Person < Common::BaseModel
|
11
11
|
passthrough :id, :external_id, :name, :email, :filter_tags
|
12
12
|
passthrough :archived
|
13
|
+
alias_method :archived?, :archived
|
14
|
+
|
13
15
|
passthrough :supervisor_id, :contractor_organization_id
|
14
16
|
passthrough :brand_id
|
15
17
|
passthrough :freemium
|
16
18
|
|
17
|
-
class_resolver :person_schema_class,
|
18
|
-
class_resolver :person_details_class,
|
19
|
+
class_resolver :person_schema_class, 'Ecoportal::API::V1::PersonSchema'
|
20
|
+
class_resolver :person_details_class, 'Ecoportal::API::V1::PersonDetails'
|
19
21
|
embeds_one :details, nullable: true, klass: :person_details_class
|
20
22
|
|
21
23
|
VALID_TAG_REGEX = /^[A-Za-z0-9 &_'\/.-]+$/
|
22
24
|
VALID_EMAIL_REGEX = /^[^@\s]+@[^@\s]+\.[^@\s]+$/
|
23
25
|
|
26
|
+
def unarchive!
|
27
|
+
self.archived = false
|
28
|
+
end
|
29
|
+
|
30
|
+
def archive!
|
31
|
+
self.archived = true
|
32
|
+
end
|
33
|
+
|
24
34
|
# Gets the supervisor (`Person`) of this person, with given his `supervisor_id`.
|
25
35
|
#
|
26
36
|
# **Example Usage**:
|
27
37
|
# ```ruby
|
28
38
|
# API_KEY = 'some-private-api-key-version'
|
29
|
-
# HOST =
|
39
|
+
# HOST = 'live.ecoportal.com'
|
30
40
|
# api = Ecoportal::API::Internal.new(API_KEY, host: HOST)
|
31
|
-
# person = api.people.get({
|
41
|
+
# person = api.people.get({'id': 'my-dummy-user'})
|
32
42
|
# super = person.supervisor(api.client)
|
33
43
|
# pp "#{person.name}'s supervisor is #{super.name}."
|
34
44
|
# ```
|
@@ -37,6 +47,7 @@ module Ecoportal
|
|
37
47
|
def supervisor(client)
|
38
48
|
return @supervisor if defined?(@supervisor)
|
39
49
|
return @supervisor = nil if supervisor_id.nil?
|
50
|
+
|
40
51
|
@supervisor = client.people.get(supervisor_id).result
|
41
52
|
end
|
42
53
|
|
@@ -51,7 +62,7 @@ module Ecoportal
|
|
51
62
|
def email=(value)
|
52
63
|
raise "Invalid email #{value.inspect}" if value && !value.match(VALID_EMAIL_REGEX)
|
53
64
|
|
54
|
-
doc[
|
65
|
+
doc['email'] = value&.downcase
|
55
66
|
end
|
56
67
|
|
57
68
|
# Validates the string tags of the array, and sets the `filter_tags` property of the account.
|
@@ -69,20 +80,20 @@ module Ecoportal
|
|
69
80
|
tag.upcase
|
70
81
|
end
|
71
82
|
|
72
|
-
set_uniq_array_keep_order(
|
83
|
+
set_uniq_array_keep_order('filter_tags', end_tags)
|
73
84
|
end
|
74
85
|
|
75
86
|
# @return [Array<String>] the filter tags of this person.
|
76
87
|
def filter_tags
|
77
|
-
doc[
|
88
|
+
doc['filter_tags'] ||= []
|
78
89
|
end
|
79
90
|
|
80
91
|
def as_json
|
81
|
-
super.merge
|
92
|
+
super.merge 'details' => details&.as_json
|
82
93
|
end
|
83
94
|
|
84
95
|
def as_update(ref = :last, ignore: [])
|
85
|
-
super
|
96
|
+
super
|
86
97
|
end
|
87
98
|
|
88
99
|
# Sets the PersonDetails to the person, depending on the paramter received:
|
@@ -95,15 +106,15 @@ module Ecoportal
|
|
95
106
|
def details=(value)
|
96
107
|
case value
|
97
108
|
when NilClass
|
98
|
-
doc[
|
109
|
+
doc['details'] = nil
|
99
110
|
when person_details_class
|
100
|
-
doc[
|
111
|
+
doc['details'] = value.as_json
|
101
112
|
when Hash
|
102
|
-
doc[
|
113
|
+
doc['details'] = value.slice('schema_id', 'fields')
|
103
114
|
else
|
104
115
|
raise "Invalid type set on details. Required nil, PersonDetails or Hash; got #{value.class}"
|
105
116
|
end
|
106
|
-
remove_instance_variable(
|
117
|
+
remove_instance_variable('@details') if defined?(@details)
|
107
118
|
end
|
108
119
|
|
109
120
|
# Sets the PersonDetails to the person, depending on the parameter received:
|
@@ -127,10 +138,11 @@ module Ecoportal
|
|
127
138
|
else
|
128
139
|
raise "Invalid set on details: Requierd PersonSchema or String; got #{schema_or_id.class}"
|
129
140
|
end
|
141
|
+
|
130
142
|
self.details = new_details
|
131
143
|
# Patch out static data from as_update
|
132
|
-
original_doc[
|
133
|
-
|
144
|
+
original_doc['details'] = {
|
145
|
+
'fields' => JSON.parse(doc['details']['fields'].to_json)
|
134
146
|
}
|
135
147
|
end
|
136
148
|
end
|
@@ -7,10 +7,10 @@ module Ecoportal
|
|
7
7
|
|
8
8
|
passthrough :schema_id
|
9
9
|
|
10
|
-
class_resolver :schema_field_value_class,
|
10
|
+
class_resolver :schema_field_value_class, 'Ecoportal::API::V1::SchemaFieldValue'
|
11
11
|
|
12
12
|
def as_json
|
13
|
-
super.merge
|
13
|
+
super.merge 'fields' => fields.map(&:as_json)
|
14
14
|
end
|
15
15
|
|
16
16
|
# Sets the `id` of the PersonDetails.
|
@@ -18,15 +18,15 @@ module Ecoportal
|
|
18
18
|
# @param value [nil, String] the id of the schema.
|
19
19
|
def schema_id=(value)
|
20
20
|
@fields = [] if value.nil?
|
21
|
-
doc[
|
21
|
+
doc['schema_id'] = value
|
22
22
|
end
|
23
23
|
|
24
24
|
# Gets all the fields of the PersonDetails.
|
25
25
|
# @return [Array<SchemaFieldValue>] the array of fields of the schema.
|
26
26
|
def fields
|
27
27
|
return @fields if defined?(@fields)
|
28
|
-
@fields = (doc[
|
29
|
-
schema_field_value_class.new(field, parent: self, key: [
|
28
|
+
@fields = (doc['fields'] || []).each_with_index.map do |field, i|
|
29
|
+
schema_field_value_class.new(field, parent: self, key: ['fields', i])
|
30
30
|
end
|
31
31
|
end
|
32
32
|
|
@@ -69,13 +69,13 @@ module Ecoportal
|
|
69
69
|
def changed?(id, doc = :original)
|
70
70
|
return false unless (field = get_field(id))
|
71
71
|
|
72
|
-
field.as_update.key?(
|
72
|
+
field.as_update.key?('value')
|
73
73
|
end
|
74
74
|
|
75
75
|
def original_value(id)
|
76
76
|
return nil unless (field = get_field(id))
|
77
77
|
|
78
|
-
field.original_doc[
|
78
|
+
field.original_doc['value']
|
79
79
|
end
|
80
80
|
|
81
81
|
protected
|
@@ -5,7 +5,7 @@ module Ecoportal
|
|
5
5
|
passthrough :id, :name
|
6
6
|
passthrough :enable_tags, :tags
|
7
7
|
|
8
|
-
class_resolver :schema_field_class,
|
8
|
+
class_resolver :schema_field_class, 'Ecoportal::API::V1::SchemaField'
|
9
9
|
|
10
10
|
def fields
|
11
11
|
@fields_by_id or index_fields
|
@@ -31,7 +31,7 @@ module Ecoportal
|
|
31
31
|
@fields_by_id = {}
|
32
32
|
@fields_by_alt_id = {}
|
33
33
|
|
34
|
-
doc[
|
34
|
+
doc['fields'].each do |field|
|
35
35
|
wrapped = schema_field_class.new(field)
|
36
36
|
@fields_by_id[wrapped.id] = wrapped
|
37
37
|
@fields_by_alt_id[wrapped.alt_id] = wrapped
|
@@ -40,9 +40,9 @@ module Ecoportal
|
|
40
40
|
|
41
41
|
def initialize_details(details)
|
42
42
|
details.schema_id = id
|
43
|
-
details.doc[
|
43
|
+
details.doc['fields'] = fields.map do |field|
|
44
44
|
field.doc.slice(*%w[id alt_id name multiple type shared]).merge(
|
45
|
-
|
45
|
+
'value' => field.multiple ? [] : nil
|
46
46
|
)
|
47
47
|
end
|
48
48
|
end
|
@@ -50,4 +50,5 @@ module Ecoportal
|
|
50
50
|
end
|
51
51
|
end
|
52
52
|
end
|
53
|
+
|
53
54
|
require 'ecoportal/api/v1/schema_field'
|