Class: Boxlet::Router

Inherits:
Object
  • Object
show all
Defined in:
lib/boxlet/app/router.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method, action) ⇒ Router

Returns a new instance of Router.



11
12
13
14
# File 'lib/boxlet/app/router.rb', line 11

def initialize(method, action)
  @method = method
  @action = action
end

Instance Attribute Details

#actionObject

Returns the value of attribute action.



9
10
11
# File 'lib/boxlet/app/router.rb', line 9

def action
  @action
end

#methodObject

Returns the value of attribute method.



9
10
11
# File 'lib/boxlet/app/router.rb', line 9

def method
  @method
end

Instance Method Details

#call(env) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# 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