Class: Junior::Dispatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/junior/dispatcher.rb

Class Method Summary collapse

Class Method Details

.dispatch!(app) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/junior/dispatcher.rb', line 7

def dispatch!(app)
  @env = app.env
  @response = app.response
  @request = app.request
  
  invoke do
    response = app.response
  
    #puts app.env['rack.input'].read
    
    controller = app.env[ 'usher.response' ] ? app.env[ 'usher.response' ].last.destination[ :controller ].to_s : nil
    if controller
      
      if controller[ '/' ] # a nested route
        controller = controller[(controller.rindex( '/' ) + 1)..controller.length]
        puts controller
      end
    
      action     = app.env[ 'usher.response' ].last.destination[ :action ].to_s
      id         = app.env[ 'usher.params' ][ :id ].to_s
  
      controller_instance = controller.camelize.to_class.new(app, id)

      if controller_instance.respond_to?(action)
        controller_instance.send(action)
      else
        controller_instance.not_found('Not found')
      end
    else
      Junior::Controller.new(app).not_found('Not found')
    end
  end
end

.invoke(&block) ⇒ Object

Run the block with ‘throw :halt’ support and apply result to the response.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/junior/dispatcher.rb', line 43

def invoke(&block)
  res = catch(:halt) { instance_eval(&block) }
  return if res.nil?

  case
  when res.respond_to?(:to_str)
    @response.body = [res]
  when res.respond_to?(:to_ary)
    res = res.to_ary
    if Fixnum === res.first
      if res.length == 3
        @response.status, headers, body = res
        @response.body = body if body
        headers.each { |k, v| @response.headers[k] = v } if headers
      elsif res.length == 2
        @response.status = res.first
        @response.body   = res.last
      else
        raise TypeError, "#{res.inspect} not supported"
      end
    else
      @response.body = res
    end
  when res.respond_to?(:each)
    @response.body = res
  when (100...599) === res
    @response.status = res
  end

  res
end