require_relative "atd/version" require "rack" require "webrick" require_relative "atd/builtin_class_modifications" require_relative "atd/routes" # Extension packs require_relative "extensions/precompilers" # The assistant technical director of your website. It does the dirty work so you can see the big picture. module ATD # Creates a new ATD App based on the template of {ATD::App}. # @return [ATD::App] # @param [Symbol] name The name of the new app and new class generated. def new(name) app = Class.new(App) Object.const_set(name.to_sym, app) app end # So called because each instance stores a route, and will be called if that route is reached. # A route for the purposes of {ATD} is a parser that will be fed env in {ATD::App#call the rack app}. class Route attr_accessor :args, :method, :block, :path, :output, :app, :actions # The first two arguments must me the path and the output. def initialize(*args, &block) @method = [:get, :post, :put, :patch, :delete] @method = [] if args.last.is_a?(Hash) && !(args.last[:respond_to].nil? || args.last[:ignore].nil?) @app = :DefaultApp parse_args(*args, &block) end # This works differently from a standard setter because is makes sure that a {Route} can belong to only one {App}. def app=(app_name) old_app = Object.const_get(@app) new_app = Object.const_get(app_name.to_sym) old_app.routes -= self if old_app.routes.is_a?(Array) && old_app.routes.include?(self) new_app.routes.nil? ? new_app.routes = Array(self) : new_app.routes += Array(self) @app = app_name end # @!method get(path = nil,*args) # @param [String] path The path at which the route should receive from. # @return ATD::Route # Sets route to receive a get request to path and execute the block provided (if one is provided) # @!method post(path = nil,*args) # @param [String] path The path at which the route should receive from. # @return ATD::Route # Sets route to receive a post request to path and execute the block provided (if one is provided) # @!method put(path = nil,*args) # @param [String] path The path at which the route should receive from. # @return ATD::Route # Sets route to receive a put request to path and execute the block provided (if one is provided) # @!method patch(path = nil,*args) # @param [String] path The path at which the route should receive from. # @return ATD::Route # Sets route to receive a patch request to path and execute the block provided (if one is provided) # @!method delete(path = nil,*args) # @param [String] path The path at which the route should receive from. # @return ATD::Route # Sets route to receive a delete request to path and execute the block provided (if one is provided) [:get, :post, :put, :delete, :patch].each do |method| define_method(method) do |*args, &block| @method = [method] if @method.length == 5 @method += [method] @method.uniq! parse_args(*args, &block) end end # Converts an instance of {ATD::Route} into it's Hash representation. # The format for the Hash is listed {ATD::App#initialize here} # @api private def to_h routes = {} routes[@path] = {} routes[@path][@method] = {} routes[@path][@method] = { output: @output, block: @block, args: @args, route: self } routes end private # This should also manage @message at some point def parse_args(*args, &block) args.compact! args.flatten! args.reject! { |arg| arg.is_a?(File) ? false : arg.empty? } # File doesn't respond to empty @block = block @path ||= args.shift @output ||= args.shift @args = Array(@args).concat(args) unless args.nil? if @output =~ /^\w*#\w*$/ # Check if @path is a controller#action combo controller, action = @output.split("#") @action = Object.const_get(controller.to_sym).method(action.to_sym) @output = @action.call end @method += Array(args.last[:respond_to]) if args.last.is_a?(Hash) && !args.last[:respond_to].nil? @method -= Array(args.last[:ignore]) if args.last.is_a?(Hash) && !args.last[:ignore].nil? self end end # A template {App} that all Apps extend. When a new App is created with {ATD.new ATD.new} it extends this class. class App attr_accessor :http class << self attr_accessor :routes # An array of instances of {ATD::Route} that belong to this {App}. # 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. def request(*args, &block) route = ATD::Route.new(*args, &block) route.app = (self == Object || self == ATD::App ? :DefaultApp : name.to_sym) route end alias req request alias r request [:get, :post, :put, :patch, :delete].each do |i| define_method(i) do |*args, &block| request.send(i, *args, &block) end 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. def start(server = WEBrick, port = 3150) Rack::Server.start(app: new, server: server, Port: port) end end # 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}. def initialize(routes = []) @routes = {} Array(routes + self.class.routes).each do |route| filename = ATD::Compilation.pre_parse(route) if route.args.last.nil? || route.args.last[:precompile].nil? || route.args.last[:precompile] route_hash = route.to_h current_route = route_hash[route.path][route.method] current_route[:filename] = filename block = current_route[:block] current_route[:block] = define_singleton_method(block.object_id.to_s.tr("0-9", "a-j").to_sym, &block) unless block.nil? current_route[:block] = route.actions unless route.actions.nil? @routes = @routes.to_h.deep_merge(route_hash) end end def request(*args, &block) route = ATD::Route.new(*args, &block) filename = ATD::Compilation.pre_parse(route) if route.args.last.nil? || route.args.last[:precompile].nil? || route.args.last[:precompile] route_hash = route.to_h route_hash[route.path][route.method][:filename] = filename @routes = @routes.to_h.deep_merge(route_hash) route end alias req request alias r request # 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. def start(server = WEBrick, port = 3150) Rack::Server.start(app: self, server: server, Port: port) end # 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" def call(env) @http = nil route = route(env) return error(404) if route.nil? route[:output] = ATD::Compilation.compile(route[:filename], route[:output]) return [route[:status_code].to_i, Hash(route[:headers]), Array(route[:output])] if route[:block].nil? http output: route[:output], request: Rack::Request.new(env), method: env["REQUEST_METHOD"] run_block(route[:block]) end private def route(env) return nil if @routes[env["PATH_INFO"]].nil? # return @routes[env["PATH_INFO"]][[]] unless @routes[env["PATH_INFO"]][[]].nil? @routes[env["PATH_INFO"]].include_in_key?(env["REQUEST_METHOD"].downcase.to_sym) end def run_block(block) method(block).call [@http[:status_code].to_i, Hash(@http[:headers]), Array(@http[:output])] end def http(additional_params) @http = { status_code: 200, headers: {} }.merge(additional_params) end def error(number) [number, {}, ["Error #{number}"]] end end module_function :new end # @return [ATD::Route] def request(*args, &block) ATD::App.request(args, block) end alias req request alias r request # Starts the rack server # @param [Class] app The app you would like to start # @param [Class] server The server that you would like to use. # @param [Fixnum] port The port you would like the server to run on. def start(app = DefaultApp, server = WEBrick, port = 3150) Rack::Server.start(app: app.new, server: server, Port: port) end [:get, :post, :put, :patch, :delete].each do |i| define_method(i) do |*args, &block| request.send(i, *args, &block) end end Object.const_set(:DefaultApp, Class.new(ATD::App)) # Create DefaultApp