Class: Wouter::Endpoint

Inherits:
Object
  • Object
show all
Defined in:
lib/wouter/endpoint.rb

Overview

‘Wouter::Endpoint` is a class to help define Rack compatible endpoints that can be assigned to a route path. Each `Wouter::Endpoint` instance is a Rack application of it’s own. It provides convience methods for creating endpoints.

Defined Under Namespace

Classes: NotImplementedError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(req, res) ⇒ Endpoint

Returns a new instance of Endpoint.



12
13
14
15
16
# File 'lib/wouter/endpoint.rb', line 12

def initialize(req, res)
  @req = req
  @res = res
  @config = req.env['wouter']
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



10
11
12
# File 'lib/wouter/endpoint.rb', line 10

def config
  @config
end

#reqObject (readonly)

Returns the value of attribute req.



10
11
12
# File 'lib/wouter/endpoint.rb', line 10

def req
  @req
end

#resObject (readonly)

Returns the value of attribute res.



10
11
12
# File 'lib/wouter/endpoint.rb', line 10

def res
  @res
end

Class Method Details

.call(env, config = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/wouter/endpoint.rb', line 18

def self.call(env, config = {})
  endpoint = new(Rack::Request.new(env), Rack::Response.new)
  response = endpoint.respond

  return response.finish if response.is_a?(Rack::Response)

  endpoint.res.write response
  endpoint.res.finish
end

Instance Method Details

#headers(hash = nil) ⇒ Object



36
37
38
39
# File 'lib/wouter/endpoint.rb', line 36

def headers(hash = nil)
  @res.headers.merge! hash if hash
  @res.headers
end

#not_foundObject



46
47
48
# File 'lib/wouter/endpoint.rb', line 46

def not_found
  Wouter::Util.not_found
end

#paramsObject



32
33
34
# File 'lib/wouter/endpoint.rb', line 32

def params
  @req.params
end

#redirect(location) ⇒ Object



50
51
52
53
54
# File 'lib/wouter/endpoint.rb', line 50

def redirect(location)
  status 302
  headers['Location'] = location
  @res
end

#respondObject



28
29
30
# File 'lib/wouter/endpoint.rb', line 28

def respond
  raise NotImplementedError, 'you must implemented #respond in your Wouter::Endpoint class'
end

#status(code) ⇒ Object



41
42
43
44
# File 'lib/wouter/endpoint.rb', line 41

def status(code)
  @res.status = code
  @res.status
end