Class: Wouter::Endpoint
- Inherits:
-
Object
- Object
- Wouter::Endpoint
- Defined in:
- lib/wouter.rb
Overview
Endpoint class to make life easier
Instance Attribute Summary collapse
-
#req ⇒ Object
Returns the value of attribute req.
-
#res ⇒ Object
Returns the value of attribute res.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(req, res) ⇒ Endpoint
constructor
A new instance of Endpoint.
- #json(body) ⇒ Object
-
#params ⇒ Object
~~~~~ Convience methods.
-
#respond ⇒ Object
Users of
Wouter::Endpointshould implement their own#response. - #status(code) ⇒ Object
Constructor Details
#initialize(req, res) ⇒ Endpoint
Returns a new instance of Endpoint.
15 16 17 18 |
# File 'lib/wouter.rb', line 15 def initialize(req, res) @req = req @res = res end |
Instance Attribute Details
#req ⇒ Object
Returns the value of attribute req.
11 12 13 |
# File 'lib/wouter.rb', line 11 def req @req end |
#res ⇒ Object
Returns the value of attribute res.
11 12 13 |
# File 'lib/wouter.rb', line 11 def res @res end |
Class Method Details
.call(env) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/wouter.rb', line 20 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 final_resp.is_a?(Rack::Response) final_resp else endpoint.res.write final_resp endpoint.res end end |
Instance Method Details
#json(body) ⇒ Object
43 44 45 46 47 |
# File 'lib/wouter.rb', line 43 def json(body) @res.set_header('Content-Type', 'application/json') @res.write body @res end |
#params ⇒ Object
~~~~~ Convience methods
39 40 41 |
# File 'lib/wouter.rb', line 39 def params @req.params end |
#respond ⇒ Object
Users of Wouter::Endpoint should implement their own #response
33 34 35 |
# File 'lib/wouter.rb', line 33 def respond @res end |
#status(code) ⇒ Object
49 50 51 52 |
# File 'lib/wouter.rb', line 49 def status(code) @res.status = code @res end |