Class: Respondo::ResponseBuilder
- Inherits:
-
Object
- Object
- Respondo::ResponseBuilder
- Defined in:
- lib/respondo/response_builder.rb
Overview
Builds the standardized response hash.
Every response always contains these top-level keys:
{
success: Boolean,
message: String,
data: Object | Array | nil,
meta: Hash
}
The meta block always contains:
{ timestamp: ISO8601 String }
Plus pagination keys when pagination: true and the data is a paginated collection. Plus request_id when config.include_request_id is true. Plus pagination when a pagination hash is supplied by the caller.
Instance Method Summary collapse
-
#build ⇒ Hash
The complete response payload.
-
#initialize(success:, data: nil, message: nil, meta: {}, errors: nil, pagination: nil, request: nil) ⇒ ResponseBuilder
constructor
A new instance of ResponseBuilder.
Constructor Details
#initialize(success:, data: nil, message: nil, meta: {}, errors: nil, pagination: nil, request: nil) ⇒ ResponseBuilder
Returns a new instance of ResponseBuilder.
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/respondo/response_builder.rb', line 31 def initialize(success:, data: nil, message: nil, meta: {}, errors: nil, pagination: nil, request: nil) @success = success @raw_data = data @message = @extra_meta = || {} @errors = errors @pagination = pagination @request = request end |
Instance Method Details
#build ⇒ Hash
Returns the complete response payload.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/respondo/response_builder.rb', line 43 def build # We initialize the hash in the exact order we want keys to appear payload = { success: @success, message: , data: serialize_data, } # Add errors before meta if they exist payload[:errors] = @errors if @errors && !@errors.empty? # Finally, add meta so it appears at the bottom payload[:meta] = apply_camelize(payload) end |