class Freedom::BaseApplication

Public Class Methods

new() click to toggle source
# File lib/freedom/core/application.rb, line 3
def initialize
  @routing_table = {}
  routes
end

Public Instance Methods

call(env) click to toggle source
# File lib/freedom/core/application.rb, line 8
def call(env)
  req = Freedom::Request.new(env)
  resolve(req)
end
delete(table_data) click to toggle source
# File lib/freedom/core/application.rb, line 29
def delete(table_data)
  route("DELETE", table_data)
end
get(table_data) click to toggle source
# File lib/freedom/core/application.rb, line 17
def get(table_data)
  route("GET", table_data)
end
head(table_data) click to toggle source
# File lib/freedom/core/application.rb, line 33
def head(table_data)
  route("HEAD", table_data)
end
options(table_data) click to toggle source
# File lib/freedom/core/application.rb, line 37
def options(table_data)
  route("OPTIONS", table_data)
end
patch(table_data) click to toggle source
# File lib/freedom/core/application.rb, line 41
def patch(table_data)
  route("PATCH", table_data)
end
post(table_data) click to toggle source
# File lib/freedom/core/application.rb, line 21
def post(table_data)
  route("POST", table_data)
end
put(table_data) click to toggle source
# File lib/freedom/core/application.rb, line 25
def put(table_data)
  route("PUT", table_data)
end
routes() click to toggle source
# File lib/freedom/core/application.rb, line 13
def routes
  raise NotImplementedError.new
end

Private Instance Methods

match_routing_table(request) click to toggle source
# File lib/freedom/core/application.rb, line 84
def match_routing_table(request)
  @routing_table.each do |key, val|
    http_method, path = *key
    next unless http_method == request.request_method
    if path.is_a?(String)
      return [val, nil] if path == request.path_info
    elsif path.is_a?(Regexp)
      match_data = request.path_info.match(path)
      return [val, match_data] if match_data
    end
  end
  [nil, nil]
end
resolve(request) click to toggle source
# File lib/freedom/core/application.rb, line 63
def resolve(request)
  result, match_data = match_routing_table(request)
  if result
    controller, action = *result
    begin
      res = controller.call(action, request)
      res.finish
    rescue => e
      STDERR.puts e.full_message
      status_500
    end
  else
    if File.exist?("#{Dir.pwd}/#{Config.public_dir}#{request.path_info}")
      files = Rack::Files.new(Config.public_dir)
      files.get(request.env)
    else
      status_404
    end
  end
end
route(http_method, table_data) click to toggle source
# File lib/freedom/core/application.rb, line 55
def route(http_method, table_data)
  table_data.each do |path, controller_action|
    controller_name, action_name = controller_action.split("#")
    controller_class = Kernel.const_get(controller_name)
    @routing_table[[http_method, path]] = [controller_class.new, action_name.to_sym]
  end
end
status_404() click to toggle source
# File lib/freedom/core/application.rb, line 98
def status_404
  res = Response.new(["404 Not Found"], 404)
  res.finish
end
status_500() click to toggle source
# File lib/freedom/core/application.rb, line 103
def status_500
  res = Response.new(["500 Internal Server Error"], 500)
  res.finish
end