Class: ATD::App
Overview
Class Attribute Summary collapse
Class Method Summary collapse
-
.r ⇒ Object
Generates an instance of Route.
-
.req ⇒ Object
Generates an instance of Route.
-
.request(*args, &block) ⇒ Object
Generates an instance of Route.
-
.start(server = WEBrick, port = 3150) ⇒ Object
Starts the rack server.
Instance Method Summary collapse
-
#call(env) ⇒ Object
This is the method which responds to .call, as the Rack spec requires.
-
#initialize(routes = []) ⇒ App
constructor
Sets up the @routes instance variable from the App.routes class instance variable.
-
#request(*args, &block) ⇒ Object
(also: #req, #r)
Allows instance method route creation.
-
#start(server = WEBrick, port = 3150) ⇒ Object
Starts the rack server.
Methods included from Compilation
compile, #method_missing, parse, precompile, #respond_to_missing?
Methods included from InternalHelpers
Methods included from Helpers
Constructor Details
#initialize(routes = []) ⇒ App
Sets up the @routes instance variable from the routes class instance variable. Can be passed an array of instances of Route and they will be added to @routes. The format of the new @routes instance variable is:
{"/" => {
get: {output: "Hello World",
block: Proc.new},
post: {output: "Hello World",
block: Proc.new}
},
"/hello" => {
get: {output: "Hello World",
block: Proc.new},
post: {output: "Hello World",
block: Proc.new
}
}
}
202 203 204 205 206 |
# File 'lib/atd.rb', line 202 def initialize(routes = []) @routes = (routes + Array(self.class.routes)).each do |route| Compilation.precompile(route) unless route.output.is_a? Hash end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class ATD::Compilation
Class Attribute Details
Class Method Details
.r ⇒ Object
Generates an instance of Route. Passes all arguments and the block to the constructor and sets the app where it was called from.
168 169 170 171 172 |
# File 'lib/atd.rb', line 168 def request(*args, &block) route = ATD::Route.new(*args, &block) route.app = (self == Object || self == ATD::App ? :DefaultApp : name.to_sym) route end |
.req ⇒ Object
Generates an instance of Route. Passes all arguments and the block to the constructor and sets the app where it was called from.
167 168 169 170 171 |
# File 'lib/atd.rb', line 167 def request(*args, &block) route = ATD::Route.new(*args, &block) route.app = (self == Object || self == ATD::App ? :DefaultApp : name.to_sym) route end |
.request(*args, &block) ⇒ Object
Generates an instance of Route. Passes all arguments and the block to the constructor and sets the app where it was called from.
162 163 164 165 166 |
# File 'lib/atd.rb', line 162 def request(*args, &block) route = ATD::Route.new(*args, &block) route.app = (self == Object || self == ATD::App ? :DefaultApp : name.to_sym) route end |
Instance Method Details
#call(env) ⇒ Object
This is the method which responds to .call, as the Rack spec requires. It will return status code 200 and whatever output corresponds the that route if it exists, and if it doesn’t it will return status code 404 and the message “Error 404”
227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/atd.rb', line 227 def call(env) routes = @routes.where(path: env["PATH_INFO"], method: env["REQUEST_METHOD"].downcase.to_sym) warn "WARNING: Multiple routes matched the request" if routes.length > 1 route = routes.first return error 404 if route.nil? output = route.output output = Compilation.compile(route)[:content] unless route.args[:compile] == false return [route.status_code.to_i, Hash(route.headers), Array(output)] if route.block.nil? generate_variables(env, route) return_val = instance_eval(&route.block) if route.block.is_a? Proc return_val = method(route.block).call if route.block.is_a? Method @view[:raw] = return_val if @view[:raw].nil? || @view[:raw].empty? [@status_code.to_i, Hash(@headers), Array(@view[:raw])] end |