Class: ATD::App

Inherits:
Object show all
Includes:
Compilation, Helpers
Defined in:
lib/atd.rb

Overview

A template App that all Apps extend. When a new App is created with ATD.new it extends this class.

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Compilation

compile, #method_missing, parse, precompile, #respond_to_missing?

Methods included from InternalHelpers

#asset

Methods included from Helpers

#http, #params, #view

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
                   }
            }
}

Parameters:

  • routes (Array) (defaults to: [])

    An array of instances of Route.



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

.routesObject

An array of instances of Route that belong to this ATD::App.



158
159
160
# File 'lib/atd.rb', line 158

def routes
  @routes
end

Class Method Details

.rObject

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

.reqObject

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

.start(server = WEBrick, port = 3150) ⇒ Object

Starts the rack server

Parameters:

  • server (Class) (defaults to: WEBrick)

    The server that you would like to use.

  • port (Fixnum) (defaults to: 3150)

    The port you would like the server to run on.



179
180
181
# File 'lib/atd.rb', line 179

def start(server = WEBrick, port = 3150)
  Rack::Server.start(app: new, server: server, Port: port)
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

#request(*args, &block) ⇒ Object Also known as: req, r

Allows instance method route creation. Just another way of creating routes.



209
210
211
212
213
# File 'lib/atd.rb', line 209

def request(*args, &block)
  route = ATD::Route.new(*args, &block)
  @routes += Array(route)
  route
end

#start(server = WEBrick, port = 3150) ⇒ Object

Starts the rack server

Parameters:

  • server (Class) (defaults to: WEBrick)

    The server that you would like to use.

  • port (Fixnum) (defaults to: 3150)

    The port you would like the server to run on.



220
221
222
# File 'lib/atd.rb', line 220

def start(server = WEBrick, port = 3150)
  Rack::Server.start(app: self, server: server, Port: port)
end