- Adbot
-
ADBOT LOVES YOU
|
|
#
?
Apr 25, 2024 18:31
|
|
- pram
- Jun 10, 2001
-
|
my terrible code from 5 years ago is still running at a bank and thats exactly what id say about it and id even make that face
|
#
?
Dec 20, 2015 08:05
|
|
- elite_garbage_man
- Apr 3, 2010
-
I THINK THAT "PRIMA DONNA" IS "PRE-MADONNA". I MAY BE ILLITERATE.
|
I hosed up hello world
|
#
?
Dec 20, 2015 08:12
|
|
- DONT THREAD ON ME
- Oct 1, 2002
-
by Nyc_Tattoo
-
Floss Finder
|
code:require "./logger"
require "http"
require "./html/*"
require "./html/builder2"
abstract class Dorker::Controller
include Dorker::Logger
@headers :: HTTP::Headers
struct RestMethod; end
struct Get < RestMethod; end
struct Post < RestMethod; end
alias GET = Get.class
alias POST = Post.class
enum Method
GET
POST
def rest
case self
when GET; Get
when POST; Post
else
RestMethod
end
end
end
def respond(id, action, method)
log.info "no method for #{id}, #{action}, #{method} on #{self.class}"
HTTP::Response.not_found
end
def respond(action, method)
log.info "no method for #{action}, #{method}"
HTTP::Response.not_found
end
def respond(method)
log.info "no method for #{method}"
HTTP::Response.not_found
end
macro action(name, method, id = nil, &block)
{% if !block.args.empty? %}
def respond(%id, %m : {{name.id.upcase}}, %t : {{method}})
{{block.args.first}} = %id
{{block.body}}
end
{% else %} def respond(%m : {{name.id.upcase}}, %t : {{method}})
{{block.body}}
end
{% end %}
end
abstract def parse_endpoint
def dynamic_dispatcher
path = @request.path
tuple = @dispatch_tuple
id = tuple[0]
action = tuple[1]
method = tuple[2]
if id && action
log.info("Dispatching #{path} with #{id}, #{action}, #{method}" )
respond(id, action, method)
elsif id && !action
log.info("Dispatching #{path} with #{id}, #{method}" )
respond(id, method)
elsif action && !id
log.info("Dispatching #{path} with #{action}, #{method}")
respond(action, method)
else
log.info("Dispatching #{path} with #{method}")
respond(method)
end
end
property :headers, :status, :body
def initialize(req : Dorker::RequestObject, match_data)
@request = req
@headers = HTTP::Headers.new
@status = 200
@body = ""
@params = req.params
action = match_data["method"]? || "index"
action = action ? parse_endpoint(action).meth : nil
id = match_data["id"]?
method = Method.parse(@request.method).rest
@dispatch_tuple = Tuple.new(id, action, method)
@context = RenderContext.new()
end
def dispatch
dynamic_dispatcher
to_response
end
macro endpoints(*endpoints)
enum Endpoint
{% for endpoint in endpoints %}
{{endpoint.id.upcase}}
{% end %}
def meth
case self
{% for endpoint in endpoints %}
when {{endpoint.id.upcase}}
{{@type}}::{{endpoint.id.capitalize}}
{% end %}
else
nil
end
end
end
{% for endpoint in endpoints %}
record {{endpoint.id.capitalize}}
alias {{endpoint.id.upcase}} = {{@type}}::{{endpoint.id.capitalize}}.class
{% end %}
end
macro inherited
macro def parse_endpoint(endpoint) : Endpoint
Endpoint.parse(endpoint)
end
Dorker::Router.routes[{{PATH}}] = {{@type}}
end
def render(k : Symbol)
Attr.modifier(:active_tab) do |attr, args, ctx|
ctx.active_tab == args ? attr.append_attr(:class, "active") : attr
end
case k
when :body
@body = Dorker::HTML::Body.yield_into(@context) do |body|
yield(body)
end
end
end
def to_response : HTTP::Response
headers.add("Content-type", "text/html") if !headers.has_key?("Content-type")
HTTP::Response.new(status, body, headers )
end
end
|
#
?
Dec 20, 2015 08:24
|
|
- echinopsis
- Apr 13, 2004
-
by Fluffdaddy
|
same but structural foundations
|
#
?
Dec 20, 2015 09:37
|
|
- pram
- Jun 10, 2001
-
|
lmao
|
#
?
Dec 21, 2015 15:32
|
|
- Adbot
-
ADBOT LOVES YOU
|
|
#
?
Apr 25, 2024 18:31
|
|