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.
86 87 88 89 90 91 92 93 94 |
# File 'lib/etna/server.rb', line 86 def initialize # Setup logging. application.setup_logger # This needs to be required before yabeda invocation, but cannot belong at the top of any module since clients # do not install yabeda. require 'yabeda' application.setup_yabeda end |
Class Attribute Details
.routes ⇒ Object (readonly)
Returns the value of attribute routes.
63 64 65 |
# File 'lib/etna/server.rb', line 63 def routes @routes end |
Class Method Details
.delete(path, options = {}, &block) ⇒ Object
52 53 54 |
# File 'lib/etna/server.rb', line 52 def delete(path, ={}, &block) route('DELETE', path, , &block) end |
.find_route(request) ⇒ Object
28 29 30 31 32 |
# File 'lib/etna/server.rb', line 28 def find_route(request) @routes.find do |route| route.matches? request end end |
.get(path, options = {}, &block) ⇒ Object
40 41 42 |
# File 'lib/etna/server.rb', line 40 def get(path, ={}, &block) route('GET', path, , &block) end |
.post(path, options = {}, &block) ⇒ Object
44 45 46 |
# File 'lib/etna/server.rb', line 44 def post(path, ={}, &block) route('POST', path, , &block) end |
.put(path, options = {}, &block) ⇒ Object
48 49 50 |
# File 'lib/etna/server.rb', line 48 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 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/etna/server.rb', line 5 def route(method, path, ={}, &block) # For healthchecks, set up servers # with an OPTIONS route on /, with noauth @routes ||= [ Etna::Route.new( 'OPTIONS', '/', { auth: { noauth: true } } ) ] @routes << Etna::Route.new( method, path, (@default_options || {}).merge(), &block ) end |
.route_path(request, name, params = {}) ⇒ Object
56 57 58 59 60 61 |
# File 'lib/etna/server.rb', line 56 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
34 35 36 37 38 |
# File 'lib/etna/server.rb', line 34 def with(={}, &block) @default_options = instance_eval(&block) @default_options = nil end |
Instance Method Details
#call(env) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/etna/server.rb', line 66 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 |