Sha256: 74a4b9e2cb448528205603d58aff2b39e4ce6cde30f5668f8a6ca7909e648a0e

Contents?: true

Size: 1.74 KB

Versions: 6

Compression:

Stored size: 1.74 KB

Contents

# frozen_string_literal: true
# typed: true


require 'json'

module WorkOS
  # The Profile class provides a lighweight wrapper around
  # a normalized response from the various IDPs WorkOS
  # supports as part of the SSO integration. This class
  # is not meant ot be instantiated in user space, and
  # is instantiated internally but exposed.
  class Profile
    extend T::Sig

    sig { returns(String) }
    attr_accessor :id, :email, :first_name, :last_name,
                  :connection_type, :idp_id

    sig { params(profile_json: String).void }
    def initialize(profile_json)
      raw = parse_json(profile_json)

      @id              = T.let(raw.id, String)
      @email           = T.let(raw.email, String)
      @first_name      = T.let(raw.first_name, String)
      @last_name       = T.let(raw.last_name, String)
      @connection_type = T.let(raw.connection_type, String)
      @idp_id          = T.let(raw.idp_id, String)
    end

    sig { returns(String) }
    def full_name
      [first_name, last_name].compact.join(' ')
    end

    def to_json(*)
      {
        id: id,
        email: email,
        first_name: first_name,
        last_name: last_name,
        connection_type: connection_type,
        idp_id: idp_id,
      }
    end

    private

    sig { params(json_string: String).returns(WorkOS::Types::ProfileStruct) }
    def parse_json(json_string)
      hash = JSON.parse(json_string, symbolize_names: true)

      WorkOS::Types::ProfileStruct.new(
        id: hash[:profile][:id],
        email: hash[:profile][:email],
        first_name: hash[:profile][:first_name],
        last_name: hash[:profile][:last_name],
        connection_type: hash[:profile][:connection_type],
        idp_id: hash[:profile][:idp_id],
      )
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
workos-0.2.2 lib/workos/profile.rb
workos-0.2.1 lib/workos/profile.rb
workos-0.2.0 lib/workos/profile.rb
workos-0.1.1 lib/workos/profile.rb
workos-0.1.0 lib/workos/profile.rb
workos-0.0.2 lib/workos/profile.rb