Class: BlueDoc::License

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

Defined Under Namespace

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

Constant Summary collapse

VERSION =
"1.2.0"

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.



62
63
64
# File 'lib/bluedoc-license.rb', line 62

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

Class Attribute Details

.encryption_keyObject

Returns the value of attribute encryption_key.



19
20
21
# File 'lib/bluedoc-license.rb', line 19

def encryption_key
  @encryption_key
end

Instance Attribute Details

#expires_atObject

Returns the value of attribute expires_at.



59
60
61
# File 'lib/bluedoc-license.rb', line 59

def expires_at
  @expires_at
end

#featuresObject

Returns the value of attribute features.



60
61
62
# File 'lib/bluedoc-license.rb', line 60

def features
  @features
end

#licenseeObject

Returns the value of attribute licensee.



59
60
61
# File 'lib/bluedoc-license.rb', line 59

def licensee
  @licensee
end

#restrictionsObject

Returns the value of attribute restrictions.



60
61
62
# File 'lib/bluedoc-license.rb', line 60

def restrictions
  @restrictions
end

#starts_atObject

Returns the value of attribute starts_at.



59
60
61
# File 'lib/bluedoc-license.rb', line 59

def starts_at
  @starts_at
end

#versionObject (readonly)

Returns the value of attribute version.



58
59
60
# File 'lib/bluedoc-license.rb', line 58

def version
  @version
end

Class Method Details

.encryptorObject



31
32
33
# File 'lib/bluedoc-license.rb', line 31

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

.import(data) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/bluedoc-license.rb', line 35

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

#allow_feature?(key) ⇒ Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/bluedoc-license.rb', line 96

def allow_feature?(key)
  self.features.include?(key.to_s)
end

#expired?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/bluedoc-license.rb', line 84

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

#restricted?(key = nil) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
91
92
93
94
# File 'lib/bluedoc-license.rb', line 88

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

#valid?Boolean

Returns:

  • (Boolean)


66
67
68
69
70
71
72
73
74
# File 'lib/bluedoc-license.rb', line 66

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)
  return false if features && !features.is_a?(Array)

  true
end

#validate!Object

Raises:



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

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

#will_expire?Boolean

Returns:

  • (Boolean)


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

def will_expire?
  self.expires_at == nil ? false : true
end