Class: Plezi::Base::Route

Inherits:
Object
  • Object
show all
Defined in:
lib/plezi/router/route.rb

Direct Known Subclasses

RouteRewrite

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, controller) ⇒ Route

Returns a new instance of Route.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/plezi/router/route.rb', line 8

def initialize(path, controller)
   @route_id = "Route#{object_id.to_s(16)}".to_sym
   @params_range = (0..0)
   m = path.match(/([^\:\(\*]*)(.*)/)
   @prefix = m[1].chomp('/'.freeze)
   if @prefix.nil? || @prefix == ''.freeze
      @prefix = '/'.freeze
      @prefix_length = 1
   else
      @prefix = "/#{@prefix}" if @prefix[0] != '/'.freeze
      @prefix_length = @prefix.length + 1
   end
   @controller = controller
   @param_names = []
   @origial = path.dup.freeze
   prep_params(m[2])
   self.class.qp
   case @controller
   when Class
      prep_controller
   when Regexp
      raise "Rewrite Routes can't contain more then one parameter to collect" if @param_names.length > 1
   else
      raise 'Controller should be a class object' unless controller.is_a?(Class)
   end
end

Instance Attribute Details

#controllerObject (readonly)

Returns the value of attribute controller.



6
7
8
# File 'lib/plezi/router/route.rb', line 6

def controller
  @controller
end

#param_namesObject (readonly)

Returns the value of attribute param_names.



6
7
8
# File 'lib/plezi/router/route.rb', line 6

def param_names
  @param_names
end

#prefixObject (readonly)

Returns the value of attribute prefix.



6
7
8
# File 'lib/plezi/router/route.rb', line 6

def prefix
  @prefix
end

Class Method Details

.qpObject



98
99
100
# File 'lib/plezi/router/route.rb', line 98

def self.qp
   @qp ||= ::Rack::QueryParser.new(Hash, 65_536, 100)
end

Instance Method Details

#call(request, response) ⇒ Object



35
36
37
38
39
40
# File 'lib/plezi/router/route.rb', line 35

def call(request, response)
   params = match(request.path_info, request)
   return nil unless params
   c = @controller.new
   c._pl_respond(request, response, params)
end

#fits_params(path, request) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/plezi/router/route.rb', line 42

def fits_params(path, request)
   params = (Thread.current[@route_id] ||= {}).clear
   params.default_proc = Plezi.hash_proc_4symstr
   params.update request.params.to_h if request && request.params
   # puts "cutting: #{path[(@prefix_length)..-1] ? path[(@prefix_length + 1)..-1] : 'nil'}"
   pa = (path[@prefix_length..-1] || ''.freeze).split('/'.freeze)
   # puts "check param count: #{pa}"
   return false unless @params_range.include?(pa.length)
   @param_names.each do |key|
      next if pa[0].nil?
      self.class.qp.normalize_params(params, Plezi.try_utf8!(Rack::Utils.unescape(key)),
                                     Plezi.try_utf8!(Rack::Utils.unescape(pa.shift)), 100)
   end
   params['*'.freeze] = pa unless pa.empty?
   params
end

#match(req_path, request = nil) ⇒ Object



59
60
61
62
# File 'lib/plezi/router/route.rb', line 59

def match(req_path, request = nil)
   # puts "#{req_path} starts with #{@prefix}? #{req_path.start_with?(@prefix)}"
   req_path.start_with?(@prefix) && fits_params(req_path, request)
end

#prep_controllerObject



94
95
96
# File 'lib/plezi/router/route.rb', line 94

def prep_controller
   @controller.include Plezi::Controller
end

#prep_params(postfix) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/plezi/router/route.rb', line 64

def prep_params(postfix)
   pfa = postfix.split '/'.freeze
   start = 0; stop = 0
   optional = false
   while pfa.any?
      name = pfa.shift
      raise "#{name} is not a valid path section in #{@origial}" unless /^((\:[\w\.\[\]]+)|(\(\:[\w\.\[\]]+\))|(\*))$/.match(name)
      if name[0] == ':'
         raise "Cannot have a required parameter after an optional parameter in #{@origial}" if optional
         @param_names << name[1..-1].freeze
      elsif name[0] == '('
         optional = true
         @param_names << name[2..-2].freeze
      elsif name[0] == '*'
         stop += 999_999
         break
      else
         raise "invalid path section #{name} in #{@origial}"
      end
      optional ? (stop += 1) : (start += 1)
   end
   unless (@param_names.include? 'id'.freeze) || stop >= 999_999
      @param_names << 'id'.freeze
      stop += 1
   end
   @params_range = (start..(start + stop))
   @param_names.freeze
   @params_range.freeze
end