Class: Blacksheep::ActionResult

Inherits:
Object
  • Object
show all
Defined in:
lib/blacksheep/action_result.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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_statusObject

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

#dataObject (readonly)

Returns the value of attribute data.



4
5
6
# File 'lib/blacksheep/action_result.rb', line 4

def data
  @data
end

#statusObject (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: message,
    ]
  }
  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(message)
  json = {
    _meta: {
      message: message
    }
  }

  new(json, :ok)
end

Instance Method Details

#render_http_statusObject



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

Returns:

  • (Boolean)


54
55
56
# File 'lib/blacksheep/action_result.rb', line 54

def success?
  @status == :ok
end