Class: Clavis::UserInfoNormalizer

Inherits:
Object
  • Object
show all
Defined in:
lib/clavis/user_info_normalizer.rb

Overview

Normalizes user information from different OAuth providers into a standard format

Class Method Summary collapse

Class Method Details

.extract_avatar(provider, user_info) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/clavis/user_info_normalizer.rb', line 52

def self.extract_avatar(provider, )
  # Handle various avatar field names across providers
  case provider.to_sym
  when :google, :facebook
    [:picture] || ["picture"]
  when :github
    [:avatar_url] || ["avatar_url"]
  when :microsoft
    [:avatar] || ["avatar"]
  else
    # Try common field names
    [:picture] ||
      ["picture"] ||
      [:avatar_url] ||
      ["avatar_url"] ||
      [:avatar] ||
      ["avatar"] ||
      [:image] ||
      ["image"]
  end
end

.extract_email(provider, user_info) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/clavis/user_info_normalizer.rb', line 17

def self.extract_email(provider, )
  # Handle both string and symbol keys
  email = [:email] || ["email"]

  # Provider-specific handling
  case provider.to_sym
  when :apple
    email || [:email_verified] || ["email_verified"]
  else
    # Default behavior for all other providers (google, github, facebook, microsoft)
    email
  end
end

.extract_name(provider, user_info) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/clavis/user_info_normalizer.rb', line 31

def self.extract_name(provider, )
  # Handle both string and symbol keys
  name = [:name] || ["name"]
  first_name = [:first_name] || ["first_name"]
  last_name = [:last_name] || ["last_name"]

  # Provider-specific handling
  case provider.to_sym
  when :google, :github, :facebook, :microsoft
    name
  when :apple
    if name && !name.empty?
      name
    elsif (first_name && !first_name.empty?) || (last_name && !last_name.empty?)
      [first_name, last_name].compact.join(" ")
    end
  else
    name || [first_name, last_name].compact.join(" ")
  end
end

.normalize(provider_name, user_info) ⇒ Object

Takes raw provider user_info and extracts standard fields



7
8
9
10
11
12
13
14
15
# File 'lib/clavis/user_info_normalizer.rb', line 7

def self.normalize(provider_name, )
  return {} unless .is_a?(Hash)

  {
    email: extract_email(provider_name, ),
    name: extract_name(provider_name, ),
    avatar_url: extract_avatar(provider_name, )
  }
end