Module: SourceLicenseSDK

Defined in:
lib/source_license_sdk.rb,
lib/source_license_sdk/version.rb,
lib/source_license_sdk/exceptions.rb

Defined Under Namespace

Classes: ActivationError, Client, Configuration, ConfigurationError, Error, LicenseError, LicenseExpiredError, LicenseNotFoundError, LicenseValidationResult, MachineError, MachineIdentifier, NetworkError, RateLimitError

Constant Summary collapse

VERSION =
'1.0.2'

Class Method Summary collapse

Class Method Details

.activate_license(license_key = nil, machine_id: nil) ⇒ Object

Activate license (method 2)



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/source_license_sdk.rb', line 41

def self.activate_license(license_key = nil, machine_id: nil)
  license_key ||= configuration.license_key
  machine_id ||= configuration.machine_id ||
                 (configuration.auto_generate_machine_id ? SourceLicenseSDK::MachineIdentifier.generate : nil)

  raise SourceLicenseSDK::ConfigurationError, 'License key is required' if license_key.nil? || license_key.empty?
  raise SourceLicenseSDK::ConfigurationError, 'Machine ID is required for activation' if machine_id.nil? || machine_id.empty?

  client = SourceLicenseSDK::Client.new(configuration)
  client.activate_license(license_key, machine_id: machine_id)
end

.configurationObject



15
16
17
# File 'lib/source_license_sdk.rb', line 15

def self.configuration
  @configuration ||= Configuration.new
end

.configure {|configuration| ... } ⇒ Object

Configure the SDK with your Source-License server details

Yields:



11
12
13
# File 'lib/source_license_sdk.rb', line 11

def self.configure
  yield(configuration)
end

.enforce_license!(license_key = nil, machine_id: nil, exit_code: 1, custom_message: nil) ⇒ Object

Enforce license check - exits application if license is invalid (method 3)



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

def self.enforce_license!(license_key = nil, machine_id: nil, exit_code: 1, custom_message: nil)
  license_key ||= configuration.license_key
  machine_id ||= configuration.machine_id

  begin
    result = validate_license(license_key, machine_id: machine_id)

    unless result.valid?
      message = custom_message || "License validation failed: #{result.error_message}"
      puts "[LICENSE ERROR] #{message}"
      exit(exit_code)
    end

    result
  rescue SourceLicenseSDK::Error => e
    message = custom_message || "License check failed: #{e.message}"
    puts "[LICENSE ERROR] #{message}"
    exit(exit_code)
  end
end

.setup(server_url:, license_key:, machine_id: nil, auto_generate_machine_id: true) ⇒ Object

Quick setup method for common use cases



20
21
22
23
24
25
26
27
# File 'lib/source_license_sdk.rb', line 20

def self.setup(server_url:, license_key:, machine_id: nil, auto_generate_machine_id: true)
  configure do |config|
    config.server_url = server_url
    config.license_key = license_key
    config.machine_id = machine_id
    config.auto_generate_machine_id = auto_generate_machine_id
  end
end

.validate_license(license_key = nil, machine_id: nil) ⇒ Object

Validate license (method 1)



30
31
32
33
34
35
36
37
38
# File 'lib/source_license_sdk.rb', line 30

def self.validate_license(license_key = nil, machine_id: nil)
  license_key ||= configuration.license_key
  machine_id ||= configuration.machine_id

  raise SourceLicenseSDK::ConfigurationError, 'License key is required' if license_key.nil? || license_key.empty?

  client = SourceLicenseSDK::Client.new(configuration)
  client.validate_license(license_key, machine_id: machine_id)
end