Class: RackJwtAegis::ResponseBuilder
- Inherits:
-
Object
- Object
- RackJwtAegis::ResponseBuilder
- Defined in:
- lib/rack_jwt_aegis/response_builder.rb
Overview
Instance Method Summary collapse
- #build_error_body(message, status_code) ⇒ Object private
- #error_response(message, status_code) ⇒ Object
- #forbidden_response(message = nil) ⇒ Object
-
#initialize(config) ⇒ ResponseBuilder
constructor
A new instance of ResponseBuilder.
- #unauthorized_response(message = nil) ⇒ Object
Constructor Details
#initialize(config) ⇒ ResponseBuilder
Returns a new instance of ResponseBuilder.
7 8 9 |
# File 'lib/rack_jwt_aegis/response_builder.rb', line 7 def initialize(config) @config = config end |
Instance Method Details
#build_error_body(message, status_code) ⇒ Object (private)
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/rack_jwt_aegis/response_builder.rb', line 43 def build_error_body(, status_code) error_data = { error: , status: status_code, timestamp: Time.now.iso8601, } # Add additional context in debug mode if @config.debug_mode? error_data[:middleware] = 'rack_jwt_aegis' error_data[:version] = RackJwtAegis::VERSION end JSON.generate(error_data) end |
#error_response(message, status_code) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/rack_jwt_aegis/response_builder.rb', line 25 def error_response(, status_code) response_body = build_error_body(, status_code) [ status_code, { 'Content-Type' => 'application/json', 'Content-Length' => response_body.bytesize.to_s, 'Cache-Control' => 'no-cache, no-store, must-revalidate', 'Pragma' => 'no-cache', 'Expires' => '0', }, [response_body], ] end |
#forbidden_response(message = nil) ⇒ Object
18 19 20 21 22 23 |
# File 'lib/rack_jwt_aegis/response_builder.rb', line 18 def forbidden_response( = nil) error_response( || @config.forbidden_response[:error] || 'Access denied', 403, ) end |
#unauthorized_response(message = nil) ⇒ Object
11 12 13 14 15 16 |
# File 'lib/rack_jwt_aegis/response_builder.rb', line 11 def ( = nil) error_response( || @config.[:error] || 'Authentication required', 401, ) end |