class Subledger::Domain::Control

Constants

ACCEPTABLE_MODES
ACCEPTABLE_VERBS

Attributes

identity[R]
mode[R]
path[R]
verbs[R]

Public Class Methods

active_klass() click to toggle source
# File lib/subledger/domain/control.rb, line 37
def self.active_klass
  ActiveControl
end
archived_klass() click to toggle source
# File lib/subledger/domain/control.rb, line 41
def self.archived_klass
  ArchivedControl
end
new(args) click to toggle source
# File lib/subledger/domain/control.rb, line 45
def initialize args
  identifiable args
  storable args

  @identity = args[:identity]
  @verbs    = args[:verbs]
  @path     = args[:path]
  @mode     = args[:mode]
end
patch_keys() click to toggle source
# File lib/subledger/domain/control.rb, line 29
def self.patch_keys
  [  ]
end
post_keys() click to toggle source
# File lib/subledger/domain/control.rb, line 25
def self.post_keys
  [ :id, :identity, :verbs, :path, :mode ]
end
root_klass() click to toggle source
# File lib/subledger/domain/control.rb, line 21
def self.root_klass
  Control
end
sub_klasses() click to toggle source
# File lib/subledger/domain/control.rb, line 33
def self.sub_klasses
  [ active_klass, archived_klass ]
end

Private Class Methods

raise_unless_creatable(args) click to toggle source
# File lib/subledger/domain/control.rb, line 108
def self.raise_unless_creatable args

  identity = args[:identity]

  if identity.nil? or not identity.kind_of? Identity
    raise ControlError, ':identity is required and must be an Identity'
  elsif UUID.invalid? identity.id
    raise ControlError, ':identity must have a valid :id'
  end

  # TODO :verb(s) should be a domain object

  verbs = args[:verbs]

  if verbs.nil?
    raise ControlError, ':verbs is required'
  end

  verbs.split('|').each do |verb|
    unless ACCEPTABLE_VERBS.include? verb
      raise ControlError, ":verbs must contain one or more of #{ACCEPTABLE_VERBS.join(', ')} separated by |"
    end
  end

  # TODO :path should be a domain object

  path = args[:path]

  if path.nil?
    raise ControlError, ':path is required'
  end

  api_version = path[1,2]

  unless api_version == API_VERSION
    raise ControlError, ":path must begin with /#{API_VERSION}/"
  end

  path[4,].split( '/' ).each do |part|
    unless part =~ /[*_0-9A-Za-z]+/
      raise ControlError, 'invalid :path'
    end
  end

  mode = args[:mode]

  if mode.nil? or not ACCEPTABLE_MODES.include? mode
    raise ControlError, ":mode must contain one or more of :allow, :deny}"
  end
end

Public Instance Methods

match?(verb, url) click to toggle source

def is verb, url

verb == @verbs and url == @path

end

# File lib/subledger/domain/control.rb, line 71
def match? verb, url
  @verbs.split('|').include?(verb) and match_wildcard(url, @path)
end

Private Instance Methods

match_wildcard(actual, authorized) click to toggle source
# File lib/subledger/domain/control.rb, line 92
def match_wildcard actual, authorized
  actual     = strip_version actual
  authorized = strip_version authorized

  regx = authorized.gsub(/\*(.)/, '[^\1]*\1').gsub(/\*$/, '.*')

  actual == authorized or ( actual =~ /^#{regx}$/ ) == 0
end
strip_version(path) click to toggle source
# File lib/subledger/domain/control.rb, line 101
def strip_version path
  path[3..-1]
end