Module: Ant::Server::Response
- Extended by:
- DRY::ResourceInjector
- Includes:
- Exceptions
- Defined in:
- lib/ant/server/response.rb
Overview
This module provides a function to wrap lambdas arround grape/sinatra You can mount this module as helper in your application and wrap the block with the method ‘process_request`
Class Attribute Summary collapse
-
.format ⇒ Object
readonly
Returns the value of attribute format.
-
.logger ⇒ Object
readonly
Returns the value of attribute logger.
Class Method Summary collapse
- .configure_defaults! ⇒ Object
- .format_mode(mode) ⇒ Object
- .log_mode(mode) ⇒ Object
- .recover_from!(exception_class, level) ⇒ Object
Instance Method Summary collapse
Class Attribute Details
.format ⇒ Object (readonly)
Returns the value of attribute format.
18 19 20 |
# File 'lib/ant/server/response.rb', line 18 def format @format end |
.logger ⇒ Object (readonly)
Returns the value of attribute logger.
18 19 20 |
# File 'lib/ant/server/response.rb', line 18 def logger @logger end |
Class Method Details
.configure_defaults! ⇒ Object
32 33 34 35 36 37 38 39 40 |
# File 'lib/ant/server/response.rb', line 32 def configure_defaults! recover_from!(Exceptions::AntSuccess, :success) recover_from!(Exceptions::AntFail, :fail) recover_from!(Exceptions::AntError, :error) register(:loggers, :cute_logger, Server::Logger.new) register(:formats, :jsend, Server::Format.new) log_mode(:cute_logger) format_mode(:jsend) end |
.format_mode(mode) ⇒ Object
24 25 26 |
# File 'lib/ant/server/response.rb', line 24 def format_mode(mode) @format = resource(:formats, mode) end |
.log_mode(mode) ⇒ Object
20 21 22 |
# File 'lib/ant/server/response.rb', line 20 def log_mode(mode) @logger = resource(:loggers, mode) end |
.recover_from!(exception_class, level) ⇒ Object
28 29 30 |
# File 'lib/ant/server/response.rb', line 28 def recover_from!(exception_class, level) register(:exceptions, exception_class, level) end |
Instance Method Details
#exception_handler(exception) ⇒ Object
43 44 45 46 47 48 |
# File 'lib/ant/server/response.rb', line 43 def exception_handler(exception) Server::Response.resources(:exceptions).each do |klass, recover| return recover if exception.is_a?(klass) end exception.is_a?(StandardError) ? :fatal : nil end |
#handle(resolver, data) ⇒ Object
50 51 52 53 54 55 56 57 58 |
# File 'lib/ant/server/response.rb', line 50 def handle(resolver, data) if resolver Server::Response.logger.send(resolver, data) Server::Response.format.send(resolver, data) else Server::Response.logger.fatal(data) raise(data.exception) end end |
#process_request ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/ant/server/response.rb', line 60 def process_request params[:__init_time] = Time.now data = RequestResponse.new(request: request, params: params) resolver = :success Server::Response.logger.access(data) begin raise(AntError, 'No implementation given') unless block_given? data.result = yield # rubocop: disable RescueException rescue Exception => ex # rubocop: enable RescueException data.exception = ex resolver = exception_handler(ex) end handle(resolver, data) end |