Class: Rango::Controller
- Inherits:
-
Object
- Object
- Rango::Controller
- Extended by:
- Forwardable
- Includes:
- Exceptions, UrlHelper
- Defined in:
- lib/rango/controller.rb
Direct Known Subclasses
Constant Summary
Constants included from Exceptions
Exceptions::ActionNotFound, Exceptions::ClientError, Exceptions::Error100, Exceptions::Error101, Exceptions::Error200, Exceptions::Error201, Exceptions::Error202, Exceptions::Error203, Exceptions::Error204, Exceptions::Error205, Exceptions::Error206, Exceptions::Error300, Exceptions::Error301, Exceptions::Error302, Exceptions::Error303, Exceptions::Error304, Exceptions::Error305, Exceptions::Error307, Exceptions::Error400, Exceptions::Error401, Exceptions::Error402, Exceptions::Error403, Exceptions::Error404, Exceptions::Error405, Exceptions::Error406, Exceptions::Error407, Exceptions::Error408, Exceptions::Error409, Exceptions::Error410, Exceptions::Error411, Exceptions::Error412, Exceptions::Error413, Exceptions::Error414, Exceptions::Error415, Exceptions::Error416, Exceptions::Error417, Exceptions::Error500, Exceptions::Error501, Exceptions::Error502, Exceptions::Error503, Exceptions::Error504, Exceptions::Error505, Exceptions::Informational, Exceptions::MultiPartParseError, Exceptions::ServerError, Exceptions::Successful
Instance Attribute Summary collapse
-
#env ⇒ Object
readonly
Returns the value of attribute env.
Class Method Summary collapse
-
.call(env) ⇒ Object
- master
-
Change Merb::Controller to respond to #call and return a Rack Array.
-
.dispatcher(action) ⇒ Object
for routers.
Instance Method Summary collapse
-
#absolute_uri(path) ⇒ Object
absolute_uri “google.com” => “google.com” absolute_uri “/products” => “localhost:4000/products”.
- #action ⇒ Object
-
#initialize(env) ⇒ Controller
constructor
A new instance of Controller.
-
#invoke_action(action) ⇒ Object
default, redefine in plugin if you need to.
-
#logger ⇒ #debug, ...
Logger for logging project related stuff.
- #params ⇒ Object
-
#redirect(location, status = 303, &block) ⇒ String
Escaped URL (which is RFC recommendation).
- #render_http_error(exception) ⇒ Object
- #request ⇒ Object
-
#rescue_http_error(exception) ⇒ Object
redefine this method for your controller if you want to provide custom error pages returns response array for rack if you need to change just body of error message, define render_http_error method.
- #response ⇒ Object
- #router_params ⇒ Object
- #run_action ⇒ Object
- #to_response ⇒ Object
Methods included from UrlHelper
#crudtree_generator, #resource, #url
Constructor Details
#initialize(env) ⇒ Controller
Returns a new instance of Controller.
109 110 111 |
# File 'lib/rango/controller.rb', line 109 def initialize(env) @env = env end |
Instance Attribute Details
#env ⇒ Object (readonly)
Returns the value of attribute env.
108 109 110 |
# File 'lib/rango/controller.rb', line 108 def env @env end |
Class Method Details
.call(env) ⇒ Object
- master
-
Change Merb::Controller to respond to #call and return a Rack Array. (wycats)rubyurl.com/BhoY
27 28 29 30 31 |
# File 'lib/rango/controller.rb', line 27 def self.call(env) Rango::Router.set_rack_env(env) # TODO: this shouldn't require router stuff, it might emit an event controller = self.new(env) controller.to_response end |
.dispatcher(action) ⇒ Object
for routers
17 18 19 20 21 22 23 |
# File 'lib/rango/controller.rb', line 17 def self.dispatcher(action) lambda do |env| Rango.logger.info("Dispatching to #{self}##{action} [#{env["REQUEST_METHOD"]} #{env["PATH_INFO"]}]") env["rango.controller.action"] = action return self.call(env) end end |
Instance Method Details
#absolute_uri(path) ⇒ Object
absolute_uri “google.com” => “google.com” absolute_uri “/products” => “localhost:4000/products”
83 84 85 86 87 88 89 |
# File 'lib/rango/controller.rb', line 83 def absolute_uri(path) if path.match(/^https?:\/{2}/) path else (request.base_url.chomp("/") + path).chomp("/") end end |
#action ⇒ Object
47 48 49 50 51 |
# File 'lib/rango/controller.rb', line 47 def action env["rango.controller.action"].to_sym rescue NoMethodError raise "You have to setup env['rango.controller.action'] to name of action you want to call" end |
#invoke_action(action) ⇒ Object
default, redefine in plugin if you need to
42 43 44 45 |
# File 'lib/rango/controller.rb', line 42 def invoke_action(action) Rango.logger.debug("Calling method #{self.class.name}##{action} without arguments") self.response.body = self.send(action) end |
#logger ⇒ #debug, ...
Returns Logger for logging project related stuff.
74 75 76 |
# File 'lib/rango/controller.rb', line 74 def logger Rango.logger end |
#params ⇒ Object
127 128 129 130 131 132 133 |
# File 'lib/rango/controller.rb', line 127 def params @params ||= begin params = self.request.params params.merge!(self.router_params) if router_params params end end |
#redirect(location, status = 303, &block) ⇒ String
Returns Escaped URL (which is RFC recommendation).
94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/rango/controller.rb', line 94 def redirect(location, status = 303, &block) if (300..399).include?(status) exception = Redirection.new(absolute_uri(location)) exception.status = status if response["Set-Cookie"] exception.headers["Set-Cookie"] = response["Set-Cookie"] # otherwise it don't save cookies end block.call(exception) unless block.nil? raise exception else raise ArgumentError, "Status has to be between 300 and 399" end end |
#render_http_error(exception) ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/rango/controller.rb', line 145 def render_http_error(exception) exception.headers["Content-Type"] = "text/html" if Rango.production? <<-EOF <h1>Application Error</h1> <p> The application you are trying to reach has currently some issues. Please contact our team and tell us about the troubles. Thank you. </p> EOF else <<-EOF <h1>#{exception.class}: #{exception.}</h1> <ul> <li>#{exception.backtrace.join("</li><li>")}</li> </ul> EOF end end |
#request ⇒ Object
113 114 115 |
# File 'lib/rango/controller.rb', line 113 def request @request ||= Rango::Request.new(env) end |
#rescue_http_error(exception) ⇒ Object
redefine this method for your controller if you want to provide custom error pages returns response array for rack if you need to change just body of error message, define render_http_error method
139 140 141 142 143 |
# File 'lib/rango/controller.rb', line 139 def rescue_http_error(exception) # we need to call it before we assign the variables body = self.render_http_error(exception) [exception.status, exception.headers, body] end |
#response ⇒ Object
117 118 119 |
# File 'lib/rango/controller.rb', line 117 def response @response ||= Rack::Response.new end |
#router_params ⇒ Object
123 124 125 |
# File 'lib/rango/controller.rb', line 123 def router_params @router_params ||= self.env["rango.router.params"] end |
#run_action ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/rango/controller.rb', line 33 def run_action if self.respond_to?(self.action) self.invoke_action(self.action) else raise NotFound, "Controller #{self.class.name} doesn't have method #{self.action}" end end |
#to_response ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/rango/controller.rb', line 53 def to_response self.run_action #self.response.finish # do we need this? [response.status, response.headers, [response.body]] # this way we got real body rather than response object rescue Redirection => redirection redirection.to_response rescue HttpError => exception self.rescue_http_error(exception) rescue Exception => exception # so we can be sure that all the exceptions which occures in controller can be captured by rescue_http_error method if Rango.development? or Rango.testing? raise exception else = "#{exception.class}: #{exception.}" server_error = InternalServerError.new() server_error.backtrace = caller self.rescue_http_error(server_error) end end |