Class: Code0::License
- Inherits:
-
Object
show all
- Defined in:
- lib/code0/license.rb,
lib/code0/license/version.rb,
lib/code0/license/boundary.rb,
lib/code0/license/encryptor.rb,
sig/code0/license.rbs,
sig/code0/license/boundary.rbs,
sig/code0/license/encryptor.rbs
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
grace_period_days
licensee
restrictions
options
].freeze
- VERSION =
"0.4.0"
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(data) ⇒ License
Returns a new instance of License.
57
58
59
|
# File 'lib/code0/license.rb', line 57
def initialize(data)
assign_attributes(data)
end
|
Instance Attribute Details
#data ⇒ Hash[Symbol, any]
Returns the value of attribute data.
82
83
84
|
# File 'lib/code0/license.rb', line 82
def data
ATTRIBUTES.to_h { |attr| [attr, send(attr)] }
end
|
Class Method Details
.encryption_key=(key) ⇒ void
This method returns an undefined value.
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
|
42
43
44
|
# File 'lib/code0/license.rb', line 42
def encryptor
Encryptor.new(@encryption_key)
end
|
.export(license, license_name) ⇒ String
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
|
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
#assign_attributes(data) ⇒ void
This method returns an undefined value.
90
91
92
93
94
95
96
97
98
99
100
101
|
# File 'lib/code0/license.rb', line 90
def assign_attributes(data)
%i[start_date end_date].each do |property|
value = data[property]
value = parse_date(data[property]) unless data[property].is_a?(Date)
send("#{property}=", value)
end
send("grace_period_days=", data[:grace_period_days] || 0)
send("licensee=", data[:licensee])
send("restrictions=", data[:restrictions] || {})
send("options=", data[:options] || {})
end
|
#in_active_time?(allow_grace_period: true) ⇒ Boolean
70
71
72
73
74
75
76
|
# File 'lib/code0/license.rb', line 70
def in_active_time?(allow_grace_period: true)
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 + (allow_grace_period ? grace_period_days : 0) >= Date.today
end
|
#parse_date(str) ⇒ Date?
103
104
105
106
107
|
# File 'lib/code0/license.rb', line 103
def parse_date(str)
Date.parse(str)
rescue StandardError
nil
end
|
#restricted?(attribute) ⇒ Boolean
78
79
80
|
# File 'lib/code0/license.rb', line 78
def restricted?(attribute)
restrictions.key?(attribute)
end
|
#valid? ⇒ Boolean
61
62
63
64
65
66
67
68
|
# File 'lib/code0/license.rb', line 61
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]
return false if grace_period_days.nil? || !grace_period_days.is_a?(Integer)
true
end
|