Class: Vinyl::Rule

Inherits:
Object
  • Object
show all
Defined in:
lib/vinyl/rule.rb

Defined Under Namespace

Classes: RegExpHash

Constant Summary collapse

@@acl_routes_collection =
RegExpHash.new

Class Method Summary collapse

Class Method Details

.acl_routes_collectionObject



85
86
87
# File 'lib/vinyl/rule.rb', line 85

def self.acl_routes_collection
    @@acl_routes_collection
end

.add(route, *args) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/vinyl/rule.rb', line 25

def self.add(route, *args) 
  begin
    method = args[0][:with_method]
    access_level = args[0][:get_access_level]
    validators = args[0][:if_pass]
    if(route.to_s.empty? || method.empty? || access_level.to_s.empty?) then
      raise InvalidAclRule, "Vinyl.rule is invalid"
    end
  rescue NoMethodError => e
    raise InvalidAclRule, "Vinyl.rule is invalid"
  rescue InvalidAclRule => e
    puts e.message
    puts e.backtrace
  end
  pattern = generate_pattern(route)
  @@acl_routes_collection[method] ||= RegExpHash.new
  @@acl_routes_collection[method][pattern] ||= Hash.new
  @@acl_routes_collection[method][pattern][access_level] = validators
end

.encoded(char) ⇒ Object



78
79
80
81
82
83
# File 'lib/vinyl/rule.rb', line 78

def self.encoded(char)
  enc = URI.escape(char)
  enc = "(?:#{escaped(char, enc).join('|')})" if enc == char
  enc = "(?:#{enc}|#{encoded('+')})" if char == " "
  enc
end

.escaped(char, enc = URI.escape(char)) ⇒ Object



74
75
76
# File 'lib/vinyl/rule.rb', line 74

def self.escaped(char, enc = URI.escape(char))
  [Regexp.escape(enc), URI.escape(char, /./)]
end

.generate_pattern(path) ⇒ Object

The ideas for this method were extracted from Sinatra’s source github.com/sinatra/sinatra/blob/master/lib/sinatra/base.rb Copyright © 2007, 2008, 2009, 2010, 2011 Blake Mizerany



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/vinyl/rule.rb', line 48

def self.generate_pattern(path)
  if path.respond_to? :to_str
    ignore = ""
    pattern = path.to_str.gsub(/[^\?\%\\\/\:\*\w]/) do |c|
      ignore << escaped(c).join if c.match(/[\.@]/)
      encoded(c)
    end
    pattern.gsub!(/((:\w+)|\*)/) do |match|
      if match == "*"
        "(.*?)"
      else
        "([^#{ignore}/?#]+)"
      end
    end
    /\A#{pattern}\z/
  elsif path.respond_to?(:keys) && path.respond_to?(:match)
    path
  elsif path.respond_to?(:names) && path.respond_to?(:match)
    path
  elsif path.respond_to? :match
    path
  else
    raise TypeError, path
  end
end