class Phaedra::RackApp

Public Class Methods

new(settings = {}) click to toggle source
# File lib/phaedra/rack_app.rb, line 29
def initialize(settings = {})
  @settings = {
    "root_dir" => Dir.pwd,
    "serverless_api_dir" => "api"
  }.merge(settings)
end

Public Instance Methods

call(env) click to toggle source
# File lib/phaedra/rack_app.rb, line 36
def call(env)
  full_api_path = File.expand_path(@settings["serverless_api_dir"], @settings["root_dir"])
  base_api_folder = File.basename(full_api_path)
  req = Request.new(env)
  res = Rack::Response.new

  api_folder = File.dirname(req.path).sub("/#{base_api_folder}", "")
  endpoint = File.basename(req.path)
  ruby_path = File.join(full_api_path, api_folder, "#{endpoint}.rb")
  if File.exist?(ruby_path)
    if Object.constants.include?(:PhaedraFunction)
      Object.send(:remove_const, :PhaedraFunction)
    end
    load ruby_path
    
    func = PhaedraFunction.new
    func.service(req, res)

    output = res.finish
    unless output[2].respond_to?(:each)
      output[2] = Array(output[2])
    end

    output
  else
    raise WEBrick::HTTPStatus::NotFound, "`#{req.path}' not found."
  end
rescue WEBrick::HTTPStatus::Status => e
  [e.code, { "Content-Type" => "text/plain" }, [e.reason_phrase]]
end