Class: Code0::License

Inherits:
Object
  • Object
show all
Defined in:
lib/code0/license.rb,
lib/code0/license/version.rb,
lib/code0/license/boundary.rb,
lib/code0/license/encryptor.rb

Overview

Logic relating to license. Delegates encryption to Encryptor

Defined Under Namespace

Modules: Boundary Classes: Encryptor, Error, ValidationError

Constant Summary collapse

ATTRIBUTES =
%i[
  start_date
  end_date
  licensee
  restrictions
  options
].freeze
VERSION =
"0.2.0"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ License

Returns a new instance of License.



56
57
58
# File 'lib/code0/license.rb', line 56

def initialize(data)
  assign_attributes(data)
end

Class Method Details

.encryption_key=(key) ⇒ Object

Raises:



18
19
20
21
22
# File 'lib/code0/license.rb', line 18

def encryption_key=(key)
  raise Error, "No RSA key provided." if key && !key.is_a?(OpenSSL::PKey::RSA)

  @encryption_key = key
end

.encryptorObject



42
43
44
# File 'lib/code0/license.rb', line 42

def encryptor
  Encryptor.new(@encryption_key)
end

.export(license, license_name) ⇒ Object



36
37
38
39
40
# File 'lib/code0/license.rb', line 36

def export(license, license_name)
  license_data = JSON.dump(license.data)
  encrypted_license = encryptor.encrypt(license_data)
  Boundary.add_boundary(encrypted_license, license_name)
end

.load(data) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/code0/license.rb', line 24

def load(data)
  raise ValidationError, "No data" if data.nil?

  data = Boundary.remove_boundary(data)

  decrypted_license = encryptor.decrypt(data)

  new(JSON.parse(decrypted_license, symbolize_names: true))
rescue JSON::ParserError
  raise ValidationError, "License data is invalid JSON"
end

Instance Method Details

#dataObject



80
81
82
# File 'lib/code0/license.rb', line 80

def data
  ATTRIBUTES.to_h { |attr| [attr, send(attr)] }
end

#in_active_time?Boolean

Returns:

  • (Boolean)


68
69
70
71
72
73
74
# File 'lib/code0/license.rb', line 68

def in_active_time?
  return false if start_date > Date.today
  return true if !end_date && options[:allow_missing_end_date]
  return false if !end_date && !options[:allow_missing_end_date]

  end_date >= Date.today
end

#restricted?(attribute) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/code0/license.rb', line 76

def restricted?(attribute)
  restrictions.key?(attribute)
end

#valid?Boolean

Returns:

  • (Boolean)


60
61
62
63
64
65
66
# File 'lib/code0/license.rb', line 60

def valid?
  return false if !licensee || !licensee.is_a?(Hash) || licensee.empty?
  return false if !start_date || !start_date.is_a?(Date)
  return false if (!end_date || !end_date.is_a?(Date)) && !options[:allow_missing_end_date]

  true
end