Module: KindeSdk::Client::Entitlements

Included in:
KindeSdk::Client
Defined in:
lib/kinde_sdk/client/entitlements.rb

Instance Method Summary collapse

Instance Method Details

#get_entitlements(options = {}) ⇒ Array

Get all entitlements for the authenticated user Matches the JavaScript SDK API: getEntitlements(options?)

Parameters:

  • options (Hash) (defaults to: {})

    Options for retrieving entitlements

Options Hash (options):

  • :force_api (Boolean) — default: false

    If true, calls the API to get fresh entitlements, otherwise uses existing getAllEntitlements method

Returns:

  • (Array)

    Array of entitlement objects



11
12
13
14
15
# File 'lib/kinde_sdk/client/entitlements.rb', line 11

def get_entitlements(options = {})
  # Entitlements are always fetched from API (they don't exist in tokens)
  # The options parameter is kept for consistency with other methods
  getAllEntitlements
end

#has_billing_entitlements?(billing_entitlements, options = {}) ⇒ Boolean Also known as: hasBillingEntitlements

Check if user has billing entitlements Matches the JavaScript SDK API: hasBillingEntitlements(params)

Parameters:

  • billing_entitlements (Array<String>)

    Array of billing entitlement price names to check

  • options (Hash) (defaults to: {})

    Options for retrieving entitlements

Returns:

  • (Boolean)

    True if user has all specified billing entitlements, false otherwise



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/kinde_sdk/client/entitlements.rb', line 23

def has_billing_entitlements?(billing_entitlements, options = {})
  return true if billing_entitlements.nil? || billing_entitlements.empty?
  
  begin
    entitlements = get_entitlements(options)
    billing_entitlements_array = Array(billing_entitlements)
    
    # Extract price names from entitlements (billing entitlements use price_name)
    entitlement_price_names = entitlements.map do |ent|
      ent.respond_to?(:price_name) ? ent.price_name : ent['price_name']
    end.compact
    
    billing_entitlements_array.all? { |price_name| entitlement_price_names.include?(price_name) }
  rescue StandardError => e
    log_error("Error checking billing entitlements: #{e.message}")
    false
  end
end

#has_entitlements?(entitlement_keys, options = {}) ⇒ Boolean Also known as: hasEntitlements

Check if user has feature entitlements

Parameters:

  • entitlement_keys (Array<String>)

    Array of entitlement feature keys to check

  • options (Hash) (defaults to: {})

    Options for retrieving entitlements

Returns:

  • (Boolean)

    True if user has all specified entitlements, false otherwise



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/kinde_sdk/client/entitlements.rb', line 47

def has_entitlements?(entitlement_keys, options = {})
  return true if entitlement_keys.nil? || entitlement_keys.empty?
  
  begin
    entitlements = get_entitlements(options)
    entitlement_keys_array = Array(entitlement_keys)
    
    # Extract feature keys from entitlements
    entitlement_feature_keys = entitlements.map do |ent|
      ent.respond_to?(:feature_key) ? ent.feature_key : ent['feature_key']
    end.compact
    
    entitlement_keys_array.all? { |key| entitlement_feature_keys.include?(key) }
  rescue StandardError => e
    log_error("Error checking entitlements: #{e.message}")
    false
  end
end