Class: Fleck::Core::Consumer::Response
- Inherits:
-
Object
- Object
- Fleck::Core::Consumer::Response
- Includes:
- Loggable
- Defined in:
- lib/fleck/core/consumer/response.rb
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
-
#errors ⇒ Object
Returns the value of attribute errors.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#id ⇒ Object
Returns the value of attribute id.
-
#status ⇒ Object
Returns the value of attribute status.
Instance Method Summary collapse
- #deprecated! ⇒ Object
- #deprecated? ⇒ Boolean
- #errors? ⇒ Boolean
-
#initialize(request_id) ⇒ Response
constructor
A new instance of Response.
- #not_found(msg = nil) ⇒ Object
- #render_error(status, msg = []) ⇒ Object
- #to_json(filter: false) ⇒ Object
- #to_s ⇒ Object
Methods included from Loggable
Constructor Details
#initialize(request_id) ⇒ Response
Returns a new instance of Response.
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/fleck/core/consumer/response.rb', line 10 def initialize(request_id) @id = request_id logger.progname += " #{@id}" @status = 200 @errors = [] @headers = {} @body = nil @deprecated = false end |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body.
8 9 10 |
# File 'lib/fleck/core/consumer/response.rb', line 8 def body @body end |
#errors ⇒ Object
Returns the value of attribute errors.
8 9 10 |
# File 'lib/fleck/core/consumer/response.rb', line 8 def errors @errors end |
#headers ⇒ Object
Returns the value of attribute headers.
8 9 10 |
# File 'lib/fleck/core/consumer/response.rb', line 8 def headers @headers end |
#id ⇒ Object
Returns the value of attribute id.
8 9 10 |
# File 'lib/fleck/core/consumer/response.rb', line 8 def id @id end |
#status ⇒ Object
Returns the value of attribute status.
8 9 10 |
# File 'lib/fleck/core/consumer/response.rb', line 8 def status @status end |
Instance Method Details
#deprecated! ⇒ Object
25 26 27 |
# File 'lib/fleck/core/consumer/response.rb', line 25 def deprecated! @deprecated = true end |
#deprecated? ⇒ Boolean
29 30 31 |
# File 'lib/fleck/core/consumer/response.rb', line 29 def deprecated? @deprecated end |
#errors? ⇒ Boolean
21 22 23 |
# File 'lib/fleck/core/consumer/response.rb', line 21 def errors? !@errors.empty? end |
#not_found(msg = nil) ⇒ Object
33 34 35 36 37 |
# File 'lib/fleck/core/consumer/response.rb', line 33 def not_found(msg = nil) @status = 404 @errors << 'Resource Not Found' @errors << msg if msg end |
#render_error(status, msg = []) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/fleck/core/consumer/response.rb', line 39 def render_error(status, msg = []) raise ArgumentError, "Invalid status code: #{status.inspect}" unless (400..599).cover?(status.to_i) @status = status.to_i if msg.is_a?(Array) @errors += msg else @errors << msg end @errors.compact! end |
#to_json(filter: false) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/fleck/core/consumer/response.rb', line 52 def to_json(filter: false) data = { "status" => @status, "errors" => @errors, "headers" => @headers, "body" => @body, "deprecated" => @deprecated } data.filter! if filter return Oj.dump(data, mode: :compat) rescue => e logger.error e.inspect + "\n" + e.backtrace.join("\n") return Oj.dump({ "status" => 500, "errors" => ['Internal Server Error', 'Failed to dump the response to JSON'] }, mode: :compat) end |
#to_s ⇒ Object
71 72 73 |
# File 'lib/fleck/core/consumer/response.rb', line 71 def to_s return "#<#{self.class} #{self.to_json(filter: true)}>" end |