add sanitize method to service
This commit is contained in:
parent
61642f834f
commit
8d747ff16e
2 changed files with 50 additions and 9 deletions
|
@ -12,8 +12,30 @@ module APIParticulier
|
|||
.reduce({}) { |acc, el| acc.deep_merge(el) }
|
||||
end
|
||||
|
||||
# Remove sources not available for the procedure
|
||||
def sanitize(requested_sources)
|
||||
requested_sources_a = h_to_a(requested_sources)
|
||||
available_sources_a = h_to_a(available_sources)
|
||||
|
||||
filtered_sources_a = requested_sources_a.intersection(available_sources_a)
|
||||
|
||||
a_to_h(filtered_sources_a)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# { 'cnaf' => { 'scope' => ['a', 'b'] }} => [['cnaf', 'scope', 'a'], ['cnaf', 'scope', 'b']]
|
||||
def h_to_a(h)
|
||||
h.reduce([]) { |acc, (provider, scopes)| scopes.each { |scope, values| values.each { |s, _| acc << [provider, scope, s] } }; acc }
|
||||
end
|
||||
|
||||
# [['cnaf', 'scope', 'a'], ['cnaf', 'scope', 'b']] => { 'cnaf' => { 'scope' => ['a', 'b'] }}
|
||||
def a_to_h(a)
|
||||
h = Hash.new { |h, k| h[k] = Hash.new { |h2, k2| h2[k2] = [] } }
|
||||
|
||||
a.reduce(h) { |acc, (provider, scope, source)| h[provider][scope] << source; acc }
|
||||
end
|
||||
|
||||
def extract_sources(provider, scope)
|
||||
{ provider => { scope => providers[provider][scope] } }
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue