Class: Heya::License

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

Defined Under Namespace

Modules: Boundary Classes: Encryptor, Error, ImportError, ValidationError

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ License

Returns a new instance of License.



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

def initialize(attributes = {})
  load_attributes(attributes)
end

Class Attribute Details

.encryption_keyObject

Returns the value of attribute encryption_key.



40
41
42
# File 'lib/heya/license.rb', line 40

def encryption_key
  @encryption_key
end

Instance Attribute Details

#expires_atObject

Returns the value of attribute expires_at.



77
78
79
# File 'lib/heya/license.rb', line 77

def expires_at
  @expires_at
end

#licenseeObject

Returns the value of attribute licensee.



77
78
79
# File 'lib/heya/license.rb', line 77

def licensee
  @licensee
end

#restrictionsObject

Returns the value of attribute restrictions.



78
79
80
# File 'lib/heya/license.rb', line 78

def restrictions
  @restrictions
end

#starts_atObject

Returns the value of attribute starts_at.



77
78
79
# File 'lib/heya/license.rb', line 77

def starts_at
  @starts_at
end

#versionObject (readonly)

Returns the value of attribute version.



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

def version
  @version
end

Class Method Details

.encryptorObject



49
50
51
# File 'lib/heya/license.rb', line 49

def encryptor
  @encryptor ||= Encryptor.new(encryption_key)
end

.import(data) ⇒ Object



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

def import(data)
  if data.nil?
    raise ImportError, "No license data."
  end

  data = Boundary.remove_boundary(data)

  begin
    license_json = encryptor.decrypt(data)
  rescue Encryptor::Error
    raise ImportError, "License data could not be decrypted."
  end

  begin
    attributes = JSON.parse(license_json)
  rescue JSON::ParseError
    raise ImportError, "License data is invalid JSON."
  end

  new(attributes)
end

Instance Method Details

#attributesObject



113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/heya/license.rb', line 113

def attributes
  hash = {}

  hash["version"] = version
  hash["licensee"] = licensee

  hash["starts_at"] = starts_at
  hash["expires_at"] = expires_at if will_expire?

  hash["restrictions"] = restrictions if restricted?

  hash
end

#expired?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/heya/license.rb', line 101

def expired?
  will_expire? && Date.today >= expires_at
end

#export(boundary: nil) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
# File 'lib/heya/license.rb', line 131

def export(boundary: nil)
  validate!

  data = self.class.encryptor.encrypt(to_json)

  if boundary
    data = Boundary.add_boundary(data, boundary)
  end

  data
end

#restricted?(key = nil) ⇒ Boolean

Returns:

  • (Boolean)


105
106
107
108
109
110
111
# File 'lib/heya/license.rb', line 105

def restricted?(key = nil)
  if key
    restricted? && restrictions.has_key?(key)
  else
    restrictions && restrictions.length >= 1
  end
end

#to_jsonObject



127
128
129
# File 'lib/heya/license.rb', line 127

def to_json
  JSON.dump(attributes)
end

#valid?Boolean

Returns:

  • (Boolean)


84
85
86
87
88
89
90
91
# File 'lib/heya/license.rb', line 84

def valid?
  return false if !licensee || !licensee.is_a?(Hash) || licensee.length == 0
  return false if !starts_at || !starts_at.is_a?(Date)
  return false if expires_at && !expires_at.is_a?(Date)
  return false if restrictions && !restrictions.is_a?(Hash)

  true
end

#validate!Object

Raises:



93
94
95
# File 'lib/heya/license.rb', line 93

def validate!
  raise ValidationError, "License is invalid" unless valid?
end

#will_expire?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/heya/license.rb', line 97

def will_expire?
  expires_at
end