class BlueDoc::License

Constants

VERSION

Attributes

encryption_key[R]
expires_at[RW]
features[RW]
licensee[RW]
restrictions[RW]
starts_at[RW]
version[R]

Public Class Methods

encryption_key=(key) click to toggle source
# File lib/bluedoc-license.rb, line 22
def encryption_key=(key)
  if key && !key.is_a?(OpenSSL::PKey::RSA)
    raise ArgumentError, "No RSA encryption key provided."
  end

  @encryption_key = key
  @encryptor = nil
end
encryptor() click to toggle source
# File lib/bluedoc-license.rb, line 31
def encryptor
  @encryptor ||= Encryptor.new(self.encryption_key)
end
import(data) click to toggle source
# 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
new(attributes = {}) click to toggle source
# File lib/bluedoc-license.rb, line 62
def initialize(attributes = {})
  load_attributes(attributes)
end

Public Instance Methods

allow_feature?(key) click to toggle source
# File lib/bluedoc-license.rb, line 96
def allow_feature?(key)
  self.features.include?(key.to_s)
end
expired?() click to toggle source
# File lib/bluedoc-license.rb, line 84
def expired?
  will_expire? && Date.today >= self.expires_at
end
restricted?(key = nil) click to toggle source
# 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?() click to toggle source
# 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!() click to toggle source
# File lib/bluedoc-license.rb, line 76
def validate!
  raise ValidationError, "License is invalid" unless valid?
end
will_expire?() click to toggle source
# File lib/bluedoc-license.rb, line 80
def will_expire?
  self.expires_at == nil ? false : true
end

Private Instance Methods

load_attributes(attributes) click to toggle source
# File lib/bluedoc-license.rb, line 102
def load_attributes(attributes)
  attributes = Hash[attributes.map { |k, v| [k.to_s, v] }]

  version = attributes["version"] || 1
  unless version && version == 1
    raise ArgumentError, "Version is too new"
  end

  @version = version

  @licensee = attributes["licensee"]
  @features = attributes["features"] || []

  %w(starts_at expires_at).each do |attr|
    value = attributes[attr]
    value = Date.parse(value) rescue nil if value.is_a?(String)

    next unless value

    send("#{attr}=", value)
  end

  restrictions = attributes["restrictions"]
  if restrictions && restrictions.is_a?(Hash)
    restrictions = Hash[restrictions.map { |k, v| [k.to_sym, v] }]
    @restrictions = restrictions
  end
end