Class: ATD::App

Inherits:
Object
  • Object
show all
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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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.



162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/atd.rb', line 162

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

Class Attribute Details

.routesObject

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



118
119
120
# File 'lib/atd.rb', line 118

def routes
  @routes
end

Instance Attribute Details

#http=(value) ⇒ Object

Sets the attribute http

Parameters:

  • value

    the value to set the attribute http to.



116
117
118
# File 'lib/atd.rb', line 116

def http=(value)
  @http = value
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.



128
129
130
131
132
# File 'lib/atd.rb', line 128

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.



127
128
129
130
131
# File 'lib/atd.rb', line 127

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.



122
123
124
125
126
# File 'lib/atd.rb', line 122

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.



139
140
141
# File 'lib/atd.rb', line 139

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”



197
198
199
200
201
202
203
204
205
# File 'lib/atd.rb', line 197

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

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



176
177
178
179
180
181
182
183
# File 'lib/atd.rb', line 176

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

#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.



190
191
192
# File 'lib/atd.rb', line 190

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