Class: ATD::App
- Inherits:
-
Object
- Object
- ATD::App
- Defined in:
- lib/atd.rb
Overview
Class Attribute Summary collapse
Instance Attribute Summary collapse
-
#http ⇒ Object
writeonly
Sets the attribute http.
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)
-
#start(server = WEBrick, port = 3150) ⇒ Object
Starts the rack server.
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
}
}
}
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
Instance Attribute Details
#http=(value) ⇒ Object
Sets the attribute http
116 117 118 |
# File 'lib/atd.rb', line 116 def http=(value) @http = value end |
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.
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 |
.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.
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
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
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 |