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
# File 'lib/wouter/endpoint.rb', line 12

def initialize(req, res)
  @req = req
  @res = res
end

Instance Attribute Details

#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) ⇒ Object



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

def self.call(env)
  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



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

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

#not_foundObject



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

def not_found
  Wouter::Util.not_found
end

#paramsObject



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

def params
  @req.params
end

#respondObject



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

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

#status(code) ⇒ Object



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

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