Skip to content

Commit

Permalink
SDK-1133: Retrieve the same attribute with different constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
emmas-yoti committed Dec 4, 2019
1 parent 198c95b commit 5f4ece1
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 12 deletions.
37 changes: 32 additions & 5 deletions lib/yoti/activity_details.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,12 @@ def initialize(receipt, decrypted_profile = nil, decrypted_application_profile =
@parent_remember_me_id = receipt['parent_remember_me_id']
@outcome = receipt['sharing_outcome']
@timestamp = receipt['timestamp'] ? Time.parse(receipt['timestamp']) : nil
@extended_user_profile = process_decrypted_profile(decrypted_profile)
@extended_application_profile = process_decrypted_profile(decrypted_application_profile)
@user_profile = @extended_user_profile.map do |name, attribute|
[name, attribute.value]
end.to_h
@extended_user_profile = process_decrypted_profile_as_list(decrypted_profile)
@extended_application_profile = process_decrypted_profile_as_list(decrypted_application_profile)
@user_profile = {}
@extended_user_profile.each do |attribute|
@user_profile[attribute.name] = attribute.value
end
end

#
Expand Down Expand Up @@ -136,6 +137,9 @@ def application_profile
#
# Process the decrypted profile into key-value hash
#
# Deprecated, to be removed in version 2.0. Use
# process_decrypted_profile_as_list instead
#
# @param [Yoti::Protobuf::Attrpubapi::AttributeList] decrypted_profile
#
# @return [Hash]
Expand All @@ -156,6 +160,29 @@ def process_decrypted_profile(decrypted_profile)
profile_data
end

#
# Process the decrypted profile into a list
#
# @param [Yoti::Protobuf::Attrpubapi::AttributeList] decrypted_profile
#
# @return [Array<Attribute>]
#
def process_decrypted_profile_as_list(decrypted_profile)
return {} unless decrypted_profile.is_a? Object
return {} unless decrypted_profile.respond_to? :attributes

profile_data = []
decrypted_profile.attributes.each do |attribute|
begin
profile_data.push process_attribute attribute
process_age_verified attribute
rescue StandardError => e
Yoti::Log.logger.warn("#{e.message} (Attribute: #{attribute.name})")
end
end
profile_data
end

#
# Converts protobuf attribute into Attribute
#
Expand Down
44 changes: 37 additions & 7 deletions lib/yoti/data_type/base_profile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,32 @@ module Yoti
#
class BaseProfile
#
# Return all attributes for the profile.
# Return the first attribute for each attribute in the profile.
#
# @return [Hash{String => Yoti::Attribute}]
#
attr_reader :attributes
def attributes
@attributes.map do |key,values|
[key, values[0]]
end.to_h
end

#
# @param [Hash{String => Yoti::Attribute}] profile_data
# @param [Hash{String => Yoti::Attribute},Array<Yoti::Attribute>] profile_data
#
def initialize(profile_data)
@attributes = profile_data.is_a?(Object) ? profile_data : {}
@attributes = {}

if profile_data.is_a? Hash
@attributes = profile_data.map do |key, value|
[key, [value]]
end.to_h
elsif profile_data.is_a? Array
profile_data.reject(&:nil?).each do |attr|
@attributes[attr.name] = [] unless @attributes[attr.name]
@attributes[attr.name].push attr
end
end
end

#
Expand All @@ -27,7 +42,20 @@ def initialize(profile_data)
def get_attribute(attr_name)
return nil unless @attributes.key? attr_name

@attributes[attr_name]
@attributes[attr_name][0]
end

#
# Get all attributes with the given attribute name
#
# @param [String] name The name of the attribute
#
# @return [Array<Attribute>]
#
def get_all_attributes_by_name(name)
return [] unless @attributes.key? name

@attributes[name]
end

protected
Expand All @@ -37,10 +65,12 @@ def get_attribute(attr_name)
#
# @param [String] name
#
# @returns [Array]
# @returns [Hash]
#
def find_attributes_starting_with(name)
@attributes.select { |key| key.to_s.start_with?(name) }
@attributes.select { |key| key.to_s.start_with?(name) }.map do |key, values|
[key, values[0]]
end.to_h
end
end
end
31 changes: 31 additions & 0 deletions spec/yoti/data_type/profile_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,35 @@
expect(verification).to be_nil
end
end

describe '.get_all_attributes_by_name' do
context 'with multiple attributes of the same name' do
let :multiname_profile do
Yoti::Profile.new([
Yoti::Attribute.new(Yoti::Attribute::FULL_NAME, 'Name 1', {}, {}),
Yoti::Attribute.new(Yoti::Attribute::FULL_NAME, 'Name 2', {}, {}),
Yoti::Attribute.new(Yoti::Attribute::FULL_NAME, 'Name 3', {}, {})
])
end

it 'returns all the names' do
expect(
multiname_profile.get_all_attributes_by_name(Yoti::Attribute::FULL_NAME)
.length
).to eql 3
expect(
multiname_profile.get_all_attributes_by_name(Yoti::Attribute::FULL_NAME)[0]
.value
).to eql 'Name 1'
expect(
multiname_profile.get_all_attributes_by_name(Yoti::Attribute::FULL_NAME)[1]
.value
).to eql 'Name 2'
expect(
multiname_profile.get_all_attributes_by_name(Yoti::Attribute::FULL_NAME)[2]
.value
).to eql 'Name 3'
end
end
end
end

0 comments on commit 5f4ece1

Please sign in to comment.