Class: Fleck::Core::Consumer::Response

Inherits:
Object
  • Object
show all
Includes:
Loggable
Defined in:
lib/fleck/core/consumer/response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Loggable

#log_error, #logger

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

#bodyObject

Returns the value of attribute body.



8
9
10
# File 'lib/fleck/core/consumer/response.rb', line 8

def body
  @body
end

#errorsObject

Returns the value of attribute errors.



8
9
10
# File 'lib/fleck/core/consumer/response.rb', line 8

def errors
  @errors
end

#headersObject

Returns the value of attribute headers.



8
9
10
# File 'lib/fleck/core/consumer/response.rb', line 8

def headers
  @headers
end

#idObject

Returns the value of attribute id.



8
9
10
# File 'lib/fleck/core/consumer/response.rb', line 8

def id
  @id
end

#statusObject

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

Returns:

  • (Boolean)


29
30
31
# File 'lib/fleck/core/consumer/response.rb', line 29

def deprecated?
  @deprecated
end

#errors?Boolean

Returns:

  • (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

Raises:

  • (ArgumentError)


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_sObject



71
72
73
# File 'lib/fleck/core/consumer/response.rb', line 71

def to_s
  return "#<#{self.class} #{self.to_json(filter: true)}>"
end