Class: Derailleur::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/derailleur/base/context.rb

Overview

A context is the place where we handle an incoming HTTP Request. much like a Rack application, it has an env and the result must be an array of three items: [status, headers, content] The content is just an instance_evaluation of a callback passed during initialization.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env, ctx, &blk) ⇒ Context

Returns a new instance of Context.



28
29
30
31
32
33
34
35
# File 'lib/derailleur/base/context.rb', line 28

def initialize(env, ctx, &blk)
  @status = 200
  @env = env
  @ctx = ctx
  @blk = blk
  @content = nil
  @headers = {'Content-Type' => 'text/plain'}
end

Instance Attribute Details

#blkObject (readonly)

The block of code that will be instance_evaled to produce the HTTP response’s body



26
27
28
# File 'lib/derailleur/base/context.rb', line 26

def blk
  @blk
end

#contentObject

The body of the HTTP response, must comply with Rack’s specification



22
23
24
# File 'lib/derailleur/base/context.rb', line 22

def content
  @content
end

#ctxObject (readonly)

The Derailleur context



13
14
15
# File 'lib/derailleur/base/context.rb', line 13

def ctx
  @ctx
end

#envObject (readonly)

The Rack environment



10
11
12
# File 'lib/derailleur/base/context.rb', line 10

def env
  @env
end

#headersObject (readonly)

A hash representing the HTTP response’s headers



16
17
18
# File 'lib/derailleur/base/context.rb', line 16

def headers
  @headers
end

#statusObject

The HTTP response’s status



19
20
21
# File 'lib/derailleur/base/context.rb', line 19

def status
  @status
end

Instance Method Details

#appObject

The Derailleur::Application for this context



43
44
45
# File 'lib/derailleur/base/context.rb', line 43

def app
  ctx['derailleur']
end

#evaluate!Object

Simply instance_evaluates the block blk in the context of self



38
39
40
# File 'lib/derailleur/base/context.rb', line 38

def evaluate!
  @content = instance_eval &blk
end

#extnameObject

Returns the extension name of the HTTP path



59
60
61
# File 'lib/derailleur/base/context.rb', line 59

def extname
  File.extname(env['PATH_INFO'])
end

#paramsObject

The parameters as parsed from the URL/HTTP-path chunks



48
49
50
# File 'lib/derailleur/base/context.rb', line 48

def params
  ctx['derailleur.params']
end

#resultObject

Method that wraps the status, headers and content into an array for Rack specification.



54
55
56
# File 'lib/derailleur/base/context.rb', line 54

def result
  [status, headers, content]
end