class ATD::App
A template {App} that all Apps extend. When a new App
is created with {ATD.new ATD.new
} it extends this class.
Attributes
Public Class Methods
Sets up the @routes instance variable from the {.routes} class instance variable. Can be passed an array of instances of {ATD::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 } } }
@param [Array] routes An array of instances of {ATD::Route}.
# 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
Generates an instance of {ATD::Route}. Passes all arguments and the block to {Route.new the constructor} and sets the app where it was called from.
# 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
Starts the rack server @param [Class] server The server that you would like to use. @param [Fixnum] port The port you would like the server to run on.
# File lib/atd.rb, line 179 def start(server = WEBrick, port = 3150) Rack::Server.start(app: new, server: server, Port: port) end
Public Instance Methods
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”
# 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
Allows instance method route creation. Just another way of creating routes.
# File lib/atd.rb, line 209 def request(*args, &block) route = ATD::Route.new(*args, &block) @routes += Array(route) route end
Starts the rack server @param [Class] server The server that you would like to use. @param [Fixnum] port The port you would like the server to run on.
# File lib/atd.rb, line 220 def start(server = WEBrick, port = 3150) Rack::Server.start(app: self, server: server, Port: port) end
Private Instance Methods
# File lib/atd.rb, line 253 def error(number) [number, {}, ["Error #{number}"]] end
# File lib/atd.rb, line 244 def generate_variables(env, route) @status_code = 200 @headers = {} @view = { raw: route.output } @request = Rack::Request.new(env) @method = env["REQUEST_METHOD"] @response = Rack::Response.new(env) end