Class: Etna::Server
- Inherits:
-
Object
- Object
- Etna::Server
- Defined in:
- lib/etna/server.rb
Class Attribute Summary collapse
-
.routes ⇒ Object
readonly
Returns the value of attribute routes.
Class Method Summary collapse
- .delete(path, options = {}, &block) ⇒ Object
- .find_route(request) ⇒ Object
- .get(path, options = {}, &block) ⇒ Object
- .post(path, options = {}, &block) ⇒ Object
- .put(path, options = {}, &block) ⇒ Object
- .route(method, path, options = {}, &block) ⇒ Object
- .route_path(request, name, params = {}) ⇒ Object
- .with(options = {}, &block) ⇒ Object
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize ⇒ Server
constructor
A new instance of Server.
Constructor Details
#initialize ⇒ Server
Returns a new instance of Server.
74 75 76 77 |
# File 'lib/etna/server.rb', line 74 def initialize # Setup logging. application.setup_logger end |
Class Attribute Details
.routes ⇒ Object (readonly)
Returns the value of attribute routes.
51 52 53 |
# File 'lib/etna/server.rb', line 51 def routes @routes end |
Class Method Details
.delete(path, options = {}, &block) ⇒ Object
40 41 42 |
# File 'lib/etna/server.rb', line 40 def delete(path, ={}, &block) route('DELETE', path, , &block) end |
.find_route(request) ⇒ Object
16 17 18 19 20 |
# File 'lib/etna/server.rb', line 16 def find_route(request) @routes.find do |route| route.matches? request end end |
.get(path, options = {}, &block) ⇒ Object
28 29 30 |
# File 'lib/etna/server.rb', line 28 def get(path, ={}, &block) route('GET', path, , &block) end |
.post(path, options = {}, &block) ⇒ Object
32 33 34 |
# File 'lib/etna/server.rb', line 32 def post(path, ={}, &block) route('POST', path, , &block) end |
.put(path, options = {}, &block) ⇒ Object
36 37 38 |
# File 'lib/etna/server.rb', line 36 def put(path, ={}, &block) route('PUT', path, , &block) end |
.route(method, path, options = {}, &block) ⇒ Object
5 6 7 8 9 10 11 12 13 14 |
# File 'lib/etna/server.rb', line 5 def route(method, path, ={}, &block) @routes ||= [] @routes << Etna::Route.new( method, path, (@default_options || {}).merge(), &block ) end |
.route_path(request, name, params = {}) ⇒ Object
44 45 46 47 48 49 |
# File 'lib/etna/server.rb', line 44 def route_path(request,name,params={}) route = routes.find do |route| route.name.to_s == name.to_s end return route ? route.path(params) : nil end |
.with(options = {}, &block) ⇒ Object
22 23 24 25 26 |
# File 'lib/etna/server.rb', line 22 def with(={}, &block) @default_options = instance_eval(&block) @default_options = nil end |
Instance Method Details
#call(env) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/etna/server.rb', line 54 def call(env) request = Rack::Request.new(env) request.env['etna.server'] = self application.logger.log_request(request) route = self.class.find_route(request) if route @params = request.env['rack.request.params'] return route.call(self, request) end [404, {}, ["There is no such path '#{request.path}'"]] rescue => e application.logger.log_error(e) raise end |