module CognitoAttributesConverter

Public Instance Methods

cognito_custom_attr_keys() click to toggle source

redefine this methods in you model if you want to store special custom attributes

# File lib/cognito_attributes_converter.rb, line 65
def cognito_custom_attr_keys
  %w[]
end
cognito_default_attr_keys() click to toggle source

redefine this methods in you model if you want to store special attributes

# File lib/cognito_attributes_converter.rb, line 60
def cognito_default_attr_keys
  %w[email phone_number]
end
cognito_key?(key) click to toggle source
# File lib/cognito_attributes_converter.rb, line 43
def cognito_key?(key)
  list_cognito_attr_keys.include?(key.to_s)
end
cognito_key_name(key) click to toggle source

name of attribute in cognito pool

# File lib/cognito_attributes_converter.rb, line 13
def cognito_key_name(key)
  return "custom:#{key}" if list_cognito_custom_attr_keys.include?(key.to_s)

  key.to_s
end
convert_from_cognito(user_struct) click to toggle source
# File lib/cognito_attributes_converter.rb, line 19
def convert_from_cognito(user_struct)
  cognito_attrs = user_struct.to_h

  user_attributes(cognito_attrs)
end
convert_to_cognito(attrs) click to toggle source
# File lib/cognito_attributes_converter.rb, line 4
def convert_to_cognito(attrs)
  cognito_attrs = attrs.map { |k, v| [k, v] }.to_h

  cognito_attrs.map do |k, v|
    { name: cognito_key_name(k), value: v } if cognito_key?(k) && v
  end.compact
end
list_cognito_attr_keys() click to toggle source
# File lib/cognito_attributes_converter.rb, line 47
def list_cognito_attr_keys
  list_cognito_default_attr_keys + list_cognito_custom_attr_keys
end
list_cognito_custom_attr_keys() click to toggle source
# File lib/cognito_attributes_converter.rb, line 55
def list_cognito_custom_attr_keys
  cognito_custom_attr_keys.map(&:to_s)
end
list_cognito_default_attr_keys() click to toggle source
# File lib/cognito_attributes_converter.rb, line 51
def list_cognito_default_attr_keys
  cognito_default_attr_keys.map(&:to_s)
end
user_attributes(cognito_attrs) click to toggle source
# File lib/cognito_attributes_converter.rb, line 25
def user_attributes(cognito_attrs)
  if cognito_attrs.key?(:user_attributes)
    user_attrs = cognito_attrs.delete(:user_attributes)
    common_attrs = cognito_attrs
  elsif cognito_attrs.key?(:attributes)
    user_attrs = cognito_attrs.delete(:attributes)
    common_attrs = cognito_attrs
  end

  list_cognito_attr_keys.map do |key|
    (user_attrs.find do |a|
      common_attrs[key.to_s] = a[:value] if a[:name] == cognito_key_name(key)
    end)
  end

  Hash[common_attrs.map { |k, v| [k.to_s, v] }]
end