class Boxlet::Router

Attributes

action[RW]
method[RW]

Public Class Methods

new(method, action) click to toggle source
# File lib/boxlet/app/router.rb, line 11
def initialize(method, action)
  @method = method
  @action = action
end

Public Instance Methods

call(env) click to toggle source
# File lib/boxlet/app/router.rb, line 16
def call(env)
  request = Rack::Request.new(env)

  Boxlet.log(:info, "INFO: #{env["REMOTE_ADDR"]} - [#{Time.now.to_s}] #{@method.upcase} #{env["SERVER_PROTOCOL"]} => #{env["REQUEST_PATH"]}")

  response = Rack::Response.new
  controller = Boxlet::Controller.new(request)
  if @method == :* || (request.get? && @method == :get) || (request.post? && @method == :post)
    Boxlet.log(:info, "INFO: Responding: #{@method.upcase} => #{@action}")
    action_response = controller.action(@action)
    response.status = action_response[:status]
    action_response[:headers].each do |key, value|
      response.header[key] = value
    end
  else
    response.status = 404
    action_response = {format: :html, content: "404 not found"}
  end

  if action_response[:format] == :json
    response.write(action_response[:content].to_json)
  else
    response.write(action_response[:content])
  end

  response.finish
end