Class: Onsi::ErrorResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/onsi/error_responder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status) ⇒ ErrorResponse

Returns a new instance of ErrorResponse.



113
114
115
116
# File 'lib/onsi/error_responder.rb', line 113

def initialize(status)
  @status = status
  @errors = []
end

Instance Attribute Details

#statusObject (readonly)

Returns the value of attribute status.



111
112
113
# File 'lib/onsi/error_responder.rb', line 111

def status
  @status
end

Instance Method Details

#add(status, code, title: nil, details: nil, meta: nil) ⇒ Object



118
119
120
121
122
123
124
125
126
# File 'lib/onsi/error_responder.rb', line 118

def add(status, code, title: nil, details: nil, meta: nil)
  @errors << {}.tap do |err|
    err[:status] = (status || @status).to_s
    err[:code]   = code
    err[:title]  = title      if title.present?
    err[:detail] = details    if details.present?
    err[:meta]   = Hash(meta) if meta.present?
  end
end

#as_jsonObject



128
129
130
# File 'lib/onsi/error_responder.rb', line 128

def as_json
  { errors: @errors.as_json }
end

#renderableObject



132
133
134
135
136
137
# File 'lib/onsi/error_responder.rb', line 132

def renderable
  {
    json:   as_json,
    status: status
  }
end