Class: Wouter::Endpoint

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

Overview

Endpoint class to make life easier

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(req, res) ⇒ Endpoint

Returns a new instance of Endpoint.



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

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

Instance Attribute Details

#reqObject

Returns the value of attribute req.



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

def req
  @req
end

#resObject

Returns the value of attribute res.



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

def res
  @res
end

Class Method Details

.call(env) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/wouter.rb', line 21

def self.call(env)
  endpoint = new(Rack::Request.new(env), Rack::Response.new)
  final_resp = endpoint.respond
  # Return a `Rack::Response` or write to the body of the response if the return value of `response` is not `Rack::Response`
  if final_resp.is_a?(Rack::Response)
    final_resp
  else
    endpoint.res.write final_resp
    endpoint.res
  end
end

Instance Method Details

#json(body) ⇒ Object



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

def json(body)
  @res.set_header('Content-Type', 'application/json')
  @res.write body
end

#paramsObject

~~~~~ Convience methods



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

def params
  @req.params
end

#respondObject

Users of ‘Wouter::Endpoint` should implement their own `#response`



34
35
36
# File 'lib/wouter.rb', line 34

def respond
  @res
end

#status(code) ⇒ Object



49
50
51
# File 'lib/wouter.rb', line 49

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