class Object

Public Instance Methods

_call(env) click to toggle source
# File lib/bigsword/base.rb, line 44
def _call(env)
  $env = env
  $request = Request.new $env
  $response = Response.new
  catch(:halt) { route_eval }
end
after() click to toggle source
# File lib/bigsword/base.rb, line 4
def after; end
before() click to toggle source
# File lib/bigsword/base.rb, line 3
def before; end
compile_route(path, &block) click to toggle source
# File lib/bigsword/base.rb, line 32
def compile_route(path, &block)
  route = {block: block, compiled_path: nil, extra_params: [], path: path}

  compiled_path = path.gsub(/:\w+/) do |match|
    route[:extra_params] << match.gsub(':', '').to_sym
    '([^/?#]+)'
  end

  route[:compiled_path] = /^#{compiled_path}$/
  route
end
convert_to_lambda(&block) click to toggle source
# File lib/bigsword/base.rb, line 6
def convert_to_lambda &block
  obj = Object.new
  obj.define_singleton_method(:_, &block)
  return obj.method(:_).to_proc
end
die(status = 200, header = {}, response_body) click to toggle source
# File lib/bigsword/base.rb, line 59
def die(status = 200, header = {}, response_body)
  halt status, header, [response_body]
end
env() click to toggle source
# File lib/bigsword/base.rb, line 106
def env
  $env
end
filters() click to toggle source
# File lib/bigsword/base.rb, line 28
def filters
  $filters ||= Hash.new { |hash, key| hash[key] = [] }
end
filters_eval(type) click to toggle source
# File lib/bigsword/base.rb, line 63
def filters_eval(type)
  filters[type].map { |filter| convert_to_lambda(&filter[:block]).call}
end
find_route() click to toggle source
# File lib/bigsword/base.rb, line 83
def find_route
  route = routes[$request.request_method].detect do |r|
    r[:compiled_path] =~ $request.path_info
  end

  if route
    $~.captures.each_with_index do |value, index|
      param = route[:extra_params][index]
      $request.params[param] = value
    end
  end

  route
end
halt(*response) click to toggle source

def halt(response)

throw :halt, response

end

# File lib/bigsword/base.rb, line 55
def halt(*response)
  throw :halt, response
end
params() click to toggle source
# File lib/bigsword/base.rb, line 98
def params
  $request.params.deep_symbolize_keys!
end
response() click to toggle source
# File lib/bigsword/base.rb, line 102
def response
  $response
end
route_eval() click to toggle source
# File lib/bigsword/base.rb, line 67
def route_eval
  filters_eval(:before)
  puts "after before..."
  route = find_route

  if route
    proc = convert_to_lambda &route[:block]
    $response.body = proc.call
    # $response.write instance_eval(&route[:block])
  else
    $response.status = 404
  end
  filters_eval(:after)
  $response.finish
end
routes() click to toggle source
# File lib/bigsword/base.rb, line 18
def routes
  $routes ||= Hash.new { |hash, key| hash[key] = [] }
end