Module: Plezi::Base::Router

Defined in:
lib/plezi/router/router.rb,
lib/plezi/router/adclient.rb

Defined Under Namespace

Classes: ADClient

Class Method Summary collapse

Class Method Details

.call(env) ⇒ Object


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/plezi/router/router.rb', line 23

def call(env)
  request = Rack::Request.new(env)
  response = Rack::Response.new
  ret = nil
  @routes.each { |route| ret = route.call(request, response); break if ret }
  unless ret
    return @app.call(env) if @app
    ret = ::Plezi::Base::Err404Ctrl.new._pl_respond(request, response, request.params)
  end
  response.write(ret) if ret.is_a?(String)
  return response.finish
rescue => e
  puts e.message, e.backtrace
  response = Rack::Response.new
  response.write ::Plezi::Base::Err500Ctrl.new._pl_respond(request, response, request.params)
  return response.finish
end

.call_methodObject

returns the `call` method. Used repeatedly in middleware mode and only once in application mode.


42
43
44
# File 'lib/plezi/router/router.rb', line 42

def call_method
  @call_method ||= Plezi::Base::Router.method(:call)
end

.listObject


61
62
63
# File 'lib/plezi/router/router.rb', line 61

def list
  @routes
end

.new(app) ⇒ Object


15
16
17
18
19
20
21
# File 'lib/plezi/router/router.rb', line 15

def new(app)
  if app && app != call_method
    puts 'Plezi as Middleware'
    @app = app
  end
  Plezi.app
end

.route(path, controller) ⇒ Object


46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/plezi/router/router.rb', line 46

def route(path, controller)
  path = path.chomp('/'.freeze) unless path == '/'.freeze
  case controller
  when :client
    controller = ::Plezi::Base::Router::ADClient
  when :assets
    controller = ::Plezi::Base::Assets
    path << '/*'.freeze unless path[-1] == '*'.freeze
  when Regexp
    path << '/*'.freeze unless path[-1] == '*'.freeze
    return @routes << RouteRewrite.new(path, controller)
  end
  @routes << Route.new(path, controller)
end

.url_for(controller, method_sym, params = {}) ⇒ Object


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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/plezi/router/router.rb', line 65

def url_for(controller, method_sym, params = {})
  # GET,PUT,POST,DELETE
  r = nil
  url = '/'.dup
  @routes.each do |tmp|
    case tmp.controller
    when Class
      next if tmp.controller != controller
      r = tmp
      break
    when Regexp
      nm = nil
      nm = tmp.param_names[0] if params[tmp.param_names[0]]
      nm ||= tmp.param_names[0].to_sym
      url << "#{params.delete nm}/" if params[nm] && params[nm].to_s =~ tmp.controller
    else
      next
    end
  end
  return nil if r.nil?
  case method_sym.to_sym
  when :new
    params.delete :id
    params.delete :_method
    params.delete '_method'.freeze
    params['id'.freeze] = :new
  when :create
    params['id'.freeze] = :new
    params.delete :id
    params['_method'.freeze] = :post
    params.delete :_method
  when :update
    params.delete :_method
    params['_method'.freeze] = :put
  when :delete
    params.delete :_method
    params['_method'.freeze] = :delete
  when :index
    params.delete 'id'.freeze
    params.delete '_method'.freeze
    params.delete :id
    params.delete :_method
  when :show
    raise "The URL for ':show' MUST contain a valid 'id' parameter for the object's index to display." unless params['id'.freeze].nil? && params[:id].nil?
    params.delete '_method'.freeze
    params.delete :_method
  else
    params.delete :id
    params['id'.freeze] = method_sym
  end
  names = r.param_names
  url.chomp! '/'.freeze
  url << r.prefix
  url.clear if url == '/'.freeze
  while names.any? && params[name[0]]
    url << "/#{Rack::Utils.escape params[names.shift]}"
  end
  url << '/'.freeze if url.empty?
  (url << '?') << Rack::Utils.build_nested_query(params) if params.any?
  url
end