Class: RackStep::App
- Inherits:
-
Object
- Object
- RackStep::App
- Defined in:
- lib/rackstep.rb
Instance Attribute Summary collapse
-
#request ⇒ Object
readonly
We will store the request and create a router in this class initializer.
-
#router ⇒ Object
readonly
We will store the request and create a router in this class initializer.
-
#settings ⇒ Object
Settings is a hash that will be injected into the controller.
Class Method Summary collapse
-
.call(env) ⇒ Object
Static method called from config.ru (“run App”).
Instance Method Summary collapse
-
#add_route(verb, path, controller, method) ⇒ Object
Adds a new route to the application.
-
#for_all_verbs_add_route(path, controller, method) ⇒ Object
Adds new routes to the application, one for each possible http verb (GET, POST, DELETE and PUT).
-
#initialize(env) ⇒ App
constructor
A new instance of App.
- #process_request ⇒ Object
Constructor Details
#initialize(env) ⇒ App
Returns a new instance of App.
21 22 23 24 25 26 27 28 29 |
# File 'lib/rackstep.rb', line 21 def initialize(env) # TODO: Is it ok to leave request as an attribute? @request = Rack::Request.new(env) @router = RackStep::Router.new @settings = Hash.new # Adding default routes to handle page not found (404). for_all_verbs_add_route('notfound', 'RackStep::ErrorController', 'not_found') end |
Instance Attribute Details
#request ⇒ Object (readonly)
We will store the request and create a router in this class initializer.
9 10 11 |
# File 'lib/rackstep.rb', line 9 def request @request end |
#router ⇒ Object (readonly)
We will store the request and create a router in this class initializer.
9 10 11 |
# File 'lib/rackstep.rb', line 9 def router @router end |
#settings ⇒ Object
Settings is a hash that will be injected into the controller. This hash may contain “global” settings, like a connection to database, and other things that should be initiaized only once while the app is starting.
14 15 16 |
# File 'lib/rackstep.rb', line 14 def settings @settings end |
Class Method Details
.call(env) ⇒ Object
Static method called from config.ru (“run App”).
17 18 19 |
# File 'lib/rackstep.rb', line 17 def self.call(env) new(env).process_request end |
Instance Method Details
#add_route(verb, path, controller, method) ⇒ Object
Adds a new route to the application.
67 68 69 |
# File 'lib/rackstep.rb', line 67 def add_route(verb, path, controller, method) @router.add_route(verb, path, controller, method) end |
#for_all_verbs_add_route(path, controller, method) ⇒ Object
Adds new routes to the application, one for each possible http verb (GET, POST, DELETE and PUT).
59 60 61 62 63 64 |
# File 'lib/rackstep.rb', line 59 def for_all_verbs_add_route(path, controller, method) @router.add_route('GET', path, controller, method) @router.add_route('POST', path, controller, method) @router.add_route('DELETE', path, controller, method) @router.add_route('PUT', path, controller, method) end |
#process_request ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/rackstep.rb', line 31 def process_request verb = request.request_method path = request.path # In RackStep, each request is processed by a method of a controller. The # router is responsable to find, based on the given path and http verb, # the apropriate controller and method to handle the request. route = router.find_route_for(path, verb) # Initialize the correspondent controller. controller = Object.const_get(route.controller).new # Inject the request into the controller. controller.request = request # Inject the settings into the controller. controller.settings = settings # Execute the before method of this controller. controller.send(:before) # Execute the apropriate method/action. controller.send(route.method) # Execute the after method of this controller. controller.send(:after) # Get from the controller what is the response for this request. response = controller.response return response end |