Class: Blacksheep::ActionResult
- Inherits:
-
Object
- Object
- Blacksheep::ActionResult
- Defined in:
- lib/blacksheep/action_result.rb
Class Attribute Summary collapse
-
.render_http_status ⇒ Object
Returns the value of attribute render_http_status.
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Class Method Summary collapse
- .error(title: 'Error', message:, status: :internal_server_error, pointer: 'unspecified') ⇒ Object
- .success(message) ⇒ Object
Instance Method Summary collapse
-
#initialize(data, status) ⇒ ActionResult
constructor
A new instance of ActionResult.
- #render_http_status ⇒ Object
- #render_json(json_wrap: 'data', render_http_status: self.render_http_status, to: nil) ⇒ Object
- #set_data(value) ⇒ Object
- #set_status(value) ⇒ Object
- #success? ⇒ Boolean
Constructor Details
#initialize(data, status) ⇒ ActionResult
Returns a new instance of ActionResult.
8 9 10 11 |
# File 'lib/blacksheep/action_result.rb', line 8 def initialize(data, status) @data = data @status = status end |
Class Attribute Details
.render_http_status ⇒ Object
Returns the value of attribute render_http_status.
14 15 16 |
# File 'lib/blacksheep/action_result.rb', line 14 def render_http_status @render_http_status end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
4 5 6 |
# File 'lib/blacksheep/action_result.rb', line 4 def data @data end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
4 5 6 |
# File 'lib/blacksheep/action_result.rb', line 4 def status @status end |
Class Method Details
.error(title: 'Error', message:, status: :internal_server_error, pointer: 'unspecified') ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/blacksheep/action_result.rb', line 26 def error(title: 'Error', message:, status: :internal_server_error, pointer: 'unspecified') json = { errors: [ pointer: { source: pointer }, title: title, detail: , ] } status = :internal_server_error new(json, status) end |
.success(message) ⇒ Object
16 17 18 19 20 21 22 23 24 |
# File 'lib/blacksheep/action_result.rb', line 16 def success() json = { _meta: { message: } } new(json, :ok) end |
Instance Method Details
#render_http_status ⇒ Object
58 59 60 |
# File 'lib/blacksheep/action_result.rb', line 58 def render_http_status self.class.render_http_status end |
#render_json(json_wrap: 'data', render_http_status: self.render_http_status, to: nil) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/blacksheep/action_result.rb', line 62 def render_json(json_wrap: 'data', render_http_status: self.render_http_status, to: nil) if to.nil? return { json: wrap_json(json_wrap) } end if to.respond_to?(:render) if render_http_status to.render json: wrap_json(json_wrap), status: status else to.render json: wrap_json(json_wrap) end else raise ArgumentError, "render target #{to.class} does not respond to #render" end end |
#set_data(value) ⇒ Object
42 43 44 45 46 |
# File 'lib/blacksheep/action_result.rb', line 42 def set_data(value) @data = value self end |
#set_status(value) ⇒ Object
48 49 50 51 52 |
# File 'lib/blacksheep/action_result.rb', line 48 def set_status(value) @status = value self end |
#success? ⇒ Boolean
54 55 56 |
# File 'lib/blacksheep/action_result.rb', line 54 def success? @status == :ok end |