Module: CognitoAttributesConverter

Included in:
CognitoSyncService
Defined in:
lib/cognito_attributes_converter.rb

Instance Method Summary collapse

Instance Method Details

#cognito_custom_attr_keysObject

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



65
66
67
# File 'lib/cognito_attributes_converter.rb', line 65

def cognito_custom_attr_keys
  %w[]
end

#cognito_default_attr_keysObject

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



60
61
62
# File 'lib/cognito_attributes_converter.rb', line 60

def cognito_default_attr_keys
  %w[email phone_number]
end

#cognito_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# 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) ⇒ Object

name of attribute in cognito pool



13
14
15
16
17
# 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) ⇒ Object



19
20
21
22
23
# 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) ⇒ Object



4
5
6
7
8
9
10
# 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_keysObject



47
48
49
# 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_keysObject



55
56
57
# 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_keysObject



51
52
53
# 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) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# 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