Class: Kaffe::Base

Inherits:
Object
  • Object
show all
Includes:
Actions, Error, Render, Routes, Settings
Defined in:
lib/kaffe/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Render

#find_template, #render, #templates

Methods included from Settings

included, #settings

Methods included from Routes

included, #route!

Methods included from Actions

#action!, #dispatch_action!, included

Methods included from Error

#dispatch_error!, #error!, included, #register_error

Constructor Details

#initialize(app = nil) ⇒ Base

Returns a new instance of Base.



16
17
18
19
# File 'lib/kaffe/base.rb', line 16

def initialize(app=nil)
  @app = app
  @templates = {}
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



12
13
14
# File 'lib/kaffe/base.rb', line 12

def env
  @env
end

#paramsObject (readonly)

Returns the value of attribute params.



12
13
14
# File 'lib/kaffe/base.rb', line 12

def params
  @params
end

#requestObject (readonly)

Returns the value of attribute request.



12
13
14
# File 'lib/kaffe/base.rb', line 12

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response.



12
13
14
# File 'lib/kaffe/base.rb', line 12

def response
  @response
end

Class Method Details

.call(env) ⇒ Object



71
# File 'lib/kaffe/base.rb', line 71

def call(env); instance.call(env) end

.instanceObject



72
# File 'lib/kaffe/base.rb', line 72

def instance; @instance ||= new end

.middlewareObject



73
# File 'lib/kaffe/base.rb', line 73

def middleware; @middleware ||= [] end

.new(*args) ⇒ Object



64
65
66
67
68
69
# File 'lib/kaffe/base.rb', line 64

def new(*args)
  builder = Rack::Builder.new
  middleware.each {|m, args, block| builder.use(m,*args,&block) }
  builder.run super
  builder.to_app
end

.use(m, *args, &block) ⇒ Object



75
76
77
# File 'lib/kaffe/base.rb', line 75

def use(m, *args, &block)
  middleware << [m, args, block]
end

Instance Method Details

#call(env) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/kaffe/base.rb', line 21

def call(env)
  @env = env
  @request = Rack::Request.new(env)
  @response = Rack::Response.new
  run! {
    # handle if errors were sent from route
      env['kaffe.error'] = catch(:error) {
        route!
        action!
      }
      error!
  }

  if @app
    @app.call(@response.finish)
  else
    @response.finish
  end
end

#run!(&block) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/kaffe/base.rb', line 41

def run! &block
  begin
    res = catch(:success) { block.call }
    case res
      when String
        @response.body = [res]
      when Array
        @response.status = res[0]
        res[1].each {|h, v| @response.headers[h] = v }
        @response.body = res[2]
      else
        puts "Should not be here"
        @response.status = 500
    end
  rescue # Something gone wrong and havent been handeled.
    puts "WARN: Define your error handlers!!!"
    @response.status = env['kaffe.error'].first
    @response.body = [env['kaffe.error'].last]
  end
end