-
Notifications
You must be signed in to change notification settings - Fork 2
/
demo.rb
403 lines (357 loc) · 9.71 KB
/
demo.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
require 'json'
require 'date'
require 'awesome_print'
# Attributes flagged according to the Blinding Identity Taxonomy
# Can be created by community, individuals or trusted entities
# User can use multiple BIT Overlays and combine them to avoid duplicates
SENSITIVE_OVERLAY = {
did: "did:sov:12idksjabcd",
tyee: "spec/overlay/1.0/bit",
name: "Sensitive data for private entity",
attributes: [
:ageic
]
}
ENTRY_OVERLAY = {
did: "did:sov:1234abcd",
type: "spec/overlay/1.0/entry",
name: "Demographics Entry",
schemaDID: "did:sov:3214abcd",
schemaVersion: "1.0",
schemaName: "Demographics",
default_values: {
ageUnit: ["YEAR"],
gender: ["MALE", "FEMALE"],
ethnicGroup: ["HISPANIC OR LATINO", "NOT HISPANIC OR LATINO", "NOT REPORTED", "UNKNOWN"],
race2Specific: ["CHINESE", "TAIWANESE", "ASIAN INDIAN", "KOREAN", "MALAYSIAN", "VIETNAMESE", "OTHER ASIAN"]
},
conditional: {
# conditional logic can be applied for attributes supported syntax:
# :attribute_name
# OR
# AND
# false
# true
# ==
# !=
# >
# <
# <=
# >=
hidden_attributes: {race2Specific: ":asian == false" },
required_attributes: { brthd: true, gender: true, ageUnit: ":ageic != nil"}
}
}
# Limit the schema to subset of attributes
# Can be created by anyone and do not required to be verify or sign by any trusted entity
# as it is applied only on top of verified schemas.
SUBSET_OVERLAY = {
did: "did:sov:1123414abcd",
type: "spec/overlay/1.0/subset",
name: "Demographics - Subset overlay",
schemaDID: "did:sov:3214abcd",
schemaVersion: "1.0",
attributes: [
:brthd,
:ageic,
:ageu,
:gender
]
}
# Hints for the attributes and description of the attributes to help user to understand the form
INFORMATION_OVERLAY = {
did: "did:sov:58kosf0239",
type: "spec/overlay/1.0/informational",
name: "Demographics English Informational",
schemaDID: "did:sov:3214abcd",
schemaVersion: "1.0",
schemaName: "Demographics",
language: "en_US",
attr_informations: {
brthd: 'Fill Your Date of Birth',
ageic: 'Fill your Age',
gender: 'Choose your Sex',
ethnicGroup: 'Choose your Ethnicity',
indalk: 'Select if your are American Indian or Alaska Native',
race2Specific: 'If race is Asian, select origin',
black: 'Select if you are Black or African American',
island: 'Select if you are Native Hawaiian or Other Pacific Islander',
white: 'Select if your are White',
},
category_information: {race: "Select all that apply"}
}
INFORMATION_OVERLAY_PL = {
did: "did:sov:58kosf0239",
type: "spec/overlay/1.0/informational",
name: "Demographics - Polish Informational",
schemaDID: "did:sov:3214abcd",
schemaVersion: "1.0",
language: "en_US",
attr_informations: {
brthd: 'Twoja data urodzenia',
ageic: 'Wprowadź wiek',
gender: 'Wybierz płeć',
ethnicGroup: 'Wybierz grupe etniczną',
indalk: 'Rodowity Amerykanin albo mieszkanie Alaski',
race2Specific: 'Jeżeli jesteś Azjatą wybierz pochodzenie',
black: 'Wybierz jeżeli jesteś czarnoskóry',
island: 'Wybierz jeżeli jesteś Hawajczykiem albo polinezejczykiem',
white: 'Wybierz jeżeli jesteś biały',
},
category_information: {race: "Wybierz wszystkie które Cię dotyczą"}
}
# Purpose: Make attributes human readable in specific language
LABEL_OVERLAY = {
did: "did:sov:59248239",
type: "spec/overlay/1.0/label",
name: "Demographics English Labels",
schemaDID: "did:sov:3214abcd",
schemaVersion: "1.0",
schemaName: "Demographics",
language: "en_US",
attr_labels: {
brthd: 'Date of Birth',
ageic: 'Age',
ageUnit: 'Age unit',
gender: 'Sex',
ethnicGroup: 'Ethnicity',
indalk: 'American Indian or Alaska Native',
asian: 'Asian',
race2Specific: 'If race is Asian, specify origin',
black: 'Black or African American',
island: 'Native Hawaiian or Other Pacific Islander',
white: 'White',
raceunk: 'Race Unknown'
},
attr_categories: {
race: [
:indalk, :asian, :racesp,
:black, :island, :white, :raceunk],
},
category_labels: {race: "Race"}
}
LABEL_OVERLAY_PL = {
did: "did:sov:59sdds248239",
type: "spec/overlay/1.0/label",
name: "Demografia - Polish Labels",
schemaDID: "did:sov:3214abcd",
schemaVersion: "1.0",
language: "pl_PL",
attr_labels: {
brthd: 'Rok urodzenia',
ageic: 'Wiek',
ageu: 'Jednotka wieku',
gender: 'Płeć',
ethnicGroup: 'Grupa etniczna',
indalk: 'rodowity Amerykanin lub Alaski',
asian: 'Azjata',
race2Specific: 'Jeśli If race is Asian, specify origin',
black: 'Czarnoskóry',
island: 'Hawajczyk lub polinezyjczyk',
white: 'Biały',
raceunk: 'Rasa nieznana'
},
attr_categories: {
race: [
:indalk, :asian, :racesp,
:black, :island, :white, :raceunk],
},
category_labels: {race: "Rasa"}
}
# Each schema can be verified (Verifiable claims) on SSI which gives user protection against bad actor and compatibility with BIT OVERLAYS
SCHEMA = {
# MANDATORY KEYS
attr_names: {
brthd: Date,
ageic: Integer,
ageUnit: String,
gender: String,
ethnicGroup: String,
indalk: TrueClass,
asian: TrueClass,
race2Specific: String,
black: TrueClass,
island: TrueClass,
white: TrueClass,
raceunk: TrueClass
},
# Attributes flagged according to the Blinding Identity Taxonomy
# by the issuer of the schema
bit_attributes: [:brthd],
did: "did:sov:3214abcd",
name: 'Demographics',
description: "Created by MEDIDATA",
version: '1.0',
# OPTIONAL KEYS
frmsrc: "DEM"
}
def show_overlay(overlay)
ap JSON.parse(overlay.to_json)
end
def show_schema(schema)
ap JSON.parse(schema.to_json)
end
def overlays
ap " ENTRY_OVERLAY LABEL_OVERLAY INFORMATION_OVERLAY SUBSET_OVERLAY SENSITIVE_OVERLAY"
end
class Schema
attr_accessor :schema, :entry_overlay, :label_overlay, :information_overlay, :subset_overlay
def initialize(schema)
@schema = schema
end
def is_bit?(attr)
if @schema[:bit_attributes].include?(attr)
return true
else
if @bit_overlay
return @bit_overlay[:attributes].include?(attr)
else
return false
end
end
end
# TODO dynamically created methods for each key within the schema
def attributes
attrs = []
if @entry_overlay
attrs << @schema[:attr_names].keys - @entry_overlay[:conditional][:hidden_attributes].keys
else
attrs << @schema[:attr_names].keys
end
if @subset_overlay
attrs = @subset_overlay[:attributes]
end
return attrs.flatten
end
def bit_attributes
attributes.each do |attr|
if is_bit?(attr)
puts attr.to_s.red
else
puts attr
end
end; nil
end
def overlays
overlays = []
overlays << @entry_overlay[:name] if @entry_overlay
overlays << @subset_overlay[:name] if @subset_overlay
overlays << @label_overlay[:name] if @label_overlay
overlays << @information_overlay[:name] if @information_overlay
overlays << @bit_overlay[:name] if @bit_overlay
ap overlays
end
def required_attributes
if @entry_overlay
r = []
@entry_overlay[:conditional][:required_attributes].each { |k,v|
if eval(v.to_s)
r << k
end
}
return r & attributes
else
[]
end
end
def remove_overlays
@entry_overlay = nil
@label_overlay = nil
@information_overlay = nil
@subset_overlay = nil
end
def apply_label_overlay(label_overlay)
@label_overlay = label_overlay
end
def apply_sensitive_overlay(bit_overlay)
@bit_overlay = bit_overlay
end
def apply_information_overlay(information_overlay)
@information_overlay = information_overlay
end
def apply_subset_overlay(subset_overlay)
@subset_overlay = subset_overlay
end
def apply_entry_overlay(entry_overlay)
@entry_overlay = entry_overlay
end
def version
@schema[:version]
end
def did
@schema[:did]
end
def name
@schema[:name]
end
def descritpion
@schema[:description]
end
def default_values
dv = {}
attributes.each do |attr|
if @entry_overlay and @entry_overlay[:default_values][attr] != nil
dv[attr] = @entry_overlay[:default_values][attr]
end
end
ap dv; nil
end
def attr_label(attr)
@label_overlay[:attr_labels][attr]
end
def attribute_labels
labels = {}
attributes.each do |attr|
if @label_overlay
labels[attr] = @label_overlay[:attr_labels][attr]
else
labels[attr] = ""
end
end
ap JSON.parse(labels.to_json);nil
end
def category_label(cat)
@label_overlay[:category_labels][cat]
end
def attr_for_category(cat)
@label_overlay[:attr_categories][cat]
end
def attr_information(attr)
@information_overlay[:attr_informations][attr]
end
def attribute_informations
labels = {}
attributes.each do |attr|
if @information_overlay
labels[attr] = @information_overlay[:attr_informations][attr]
else
labels[attr] = ""
end
end
ap JSON.parse(labels.to_json);nil
end
def category_information(cat)
@information_overlay[:category_information][cat]
end
def subset_attrs
attributes.select { |i|
@subset_overlay[:attributes].include?(i)
}
end
end
# NOTICE: Below example how to use it
# schema = Schema.new(SCHEMA)
#puts schema.default_values
#puts schema.attr_label(:brthd)
#puts schema.category_label(:race)
#puts schema.attr_information(:brthd)
#puts schema.category_information(:race)
#puts schema.subset_attrs
#puts schema.attr_for_category(:race).map { |i| schema.attr_label(i) }
##
#puts SCHEMA
#puts ENTRY_OVERLAY
#puts LABEL_OVERLAY
#puts INFORMATION_OVERLAY
#puts SUBSET_OVERLAY
#puts SENSITIVE_OVERLAY