Class: Cuprum::Rails::Responses::JsonResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/cuprum/rails/responses/json_response.rb

Overview

Encapsulates a JSON response that returns the given serialized data.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data:, status: 200) ⇒ JsonResponse

Returns a new instance of JsonResponse.

Parameters:

  • data (Object)

    The JSON data to return.

  • status (Integer) (defaults to: 200)

    The HTTP status of the response.



10
11
12
13
# File 'lib/cuprum/rails/responses/json_response.rb', line 10

def initialize(data:, status: 200)
  @data   = data
  @status = status
end

Instance Attribute Details

#dataObject (readonly)

Returns the JSON data to return.

Returns:

  • (Object)

    the JSON data to return.



16
17
18
# File 'lib/cuprum/rails/responses/json_response.rb', line 16

def data
  @data
end

#statusInteger (readonly)

Returns the HTTP status of the response.

Returns:

  • (Integer)

    the HTTP status of the response.



19
20
21
# File 'lib/cuprum/rails/responses/json_response.rb', line 19

def status
  @status
end

Instance Method Details

#call(renderer) ⇒ Object

Calls the renderer’s #render method with the serialized data and status.

Parameters:

  • renderer (#render)

    The context for executing the response, such as a Rails controller.



25
26
27
# File 'lib/cuprum/rails/responses/json_response.rb', line 25

def call(renderer)
  renderer.render(json: data, status: status)
end