Class: Utopia::Middleware::Controller::Base
- Inherits:
-
Object
- Object
- Utopia::Middleware::Controller::Base
- Defined in:
- lib/utopia/middleware/controller.rb
Class Method Summary collapse
Instance Method Summary collapse
- #action(path, options = {}, &block) ⇒ Object
- #call(env) ⇒ Object
-
#initialize(controller) ⇒ Base
constructor
A new instance of Base.
- #lookup(path) ⇒ Object
-
#passthrough(path, request) ⇒ Object
Given a request, call an associated action if one exists.
- #process!(path, request) ⇒ Object
- #redirect(target, status = 302) ⇒ Object
- #respond_with(*args) ⇒ Object
Constructor Details
#initialize(controller) ⇒ Base
Returns a new instance of Base.
36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/utopia/middleware/controller.rb', line 36 def initialize(controller) @controller = controller @actions = {} methods.each do |method_name| next unless method_name.match(/on_(.*)$/) action($1.split("_")) do |path, request| # LOG.debug("Controller: #{method_name}") self.send(method_name, path, request) end end end |
Class Method Details
.require_local(path) ⇒ Object
136 137 138 |
# File 'lib/utopia/middleware/controller.rb', line 136 def self.require_local(path) require(File.join(const_get('BASE_PATH'), path)) end |
Instance Method Details
#action(path, options = {}, &block) ⇒ Object
50 51 52 53 54 55 56 57 58 |
# File 'lib/utopia/middleware/controller.rb', line 50 def action(path, = {}, &block) cur = @actions path.reverse.each do |name| cur = cur[name] ||= {} end cur[:action] = Proc.new(&block) end |
#call(env) ⇒ Object
85 86 87 |
# File 'lib/utopia/middleware/controller.rb', line 85 def call(env) @controller.app.call(env) end |
#lookup(path) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/utopia/middleware/controller.rb', line 60 def lookup(path) cur = @actions path.components.reverse.each do |name| cur = cur[name] return nil if cur == nil if action = cur[:action] return action end end end |
#passthrough(path, request) ⇒ Object
Given a request, call an associated action if one exists
75 76 77 78 79 80 81 82 83 |
# File 'lib/utopia/middleware/controller.rb', line 75 def passthrough(path, request) action = lookup(path) if action return respond_with(action.call(path, request)) end return nil end |
#process!(path, request) ⇒ Object
132 133 134 |
# File 'lib/utopia/middleware/controller.rb', line 132 def process!(path, request) passthrough(path, request) end |
#redirect(target, status = 302) ⇒ Object
89 90 91 |
# File 'lib/utopia/middleware/controller.rb', line 89 def redirect(target, status = 302) {:redirect => target, :status => status} end |
#respond_with(*args) ⇒ Object
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 126 127 128 129 130 |
# File 'lib/utopia/middleware/controller.rb', line 93 def respond_with(*args) return args[0] if args[0] == nil || Array === args[0] status = 200 = nil if Numeric === args[0] || Symbol === args[0] status = args[0] = args[1] || {} else = args[0] status = [:status] || status end status = Utopia::HTTP_STATUS_CODES[status] || status headers = [:headers] || {} if [:type] headers['Content-Type'] ||= [:type] end if [:redirect] headers["Location"] = [:redirect] status = 302 if status < 300 || status >= 400 end body = [] if [:body] body = [:body] elsif [:content] body = [[:content]] elsif status >= 300 body = [Utopia::HTTP_STATUS_DESCRIPTIONS[status] || 'Status #{status}'] end # Utopia::LOG.debug([status, headers, body].inspect) return [status, headers, body] end |