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.



9
10
11
12
13
14
15
16
17
18
# File 'lib/fleck/core/consumer/response.rb', line 9

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.



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

def body
  @body
end

#errors ⇒ Object

Returns the value of attribute errors.



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

def errors
  @errors
end

#headers ⇒ Object

Returns the value of attribute headers.



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

def headers
  @headers
end

#id ⇒ Object

Returns the value of attribute id.



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

def id
  @id
end

#status ⇒ Object

Returns the value of attribute status.



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

def status
  @status
end

Instance Method Details

#deprecated! ⇒ Object



24
25
26
# File 'lib/fleck/core/consumer/response.rb', line 24

def deprecated!
  @deprecated = true
end

#deprecated? ⇒ Boolean

Returns:

  • (Boolean)


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

def deprecated?
  @deprecated
end

#errors? ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/fleck/core/consumer/response.rb', line 20

def errors?
  !@errors.empty?
end

#not_found(msg = nil) ⇒ Object



40
41
42
43
44
# File 'lib/fleck/core/consumer/response.rb', line 40

def not_found(msg = nil)
  @status = 404
  @errors << 'Resource Not Found'
  @errors << msg if msg
end

#render_error(status, msg = []) ⇒ Object

Raises:

  • (ArgumentError)


46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/fleck/core/consumer/response.rb', line 46

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

#reset! ⇒ Object



32
33
34
35
36
37
38
# File 'lib/fleck/core/consumer/response.rb', line 32

def reset!
  @status     = 200
  @errors     = []
  @headers    = {}
  @body       = nil
  @deprecated = false
end

#to_json(filter: false) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/fleck/core/consumer/response.rb', line 59

def to_json(filter: false)
  data = {
    'status' => @status,
    'errors' => @errors,
    'headers' => @headers,
    'body' => @body,
    'deprecated' => @deprecated
  }
  data.filter! if filter

  Oj.dump(data, mode: :compat)
rescue StandardError => e
  logger.error e.inspect + "\n" + e.backtrace.join("\n")
  Oj.dump({
            'status' => 500,
            'errors' => ['Internal Server Error', 'Failed to dump the response to JSON']
          }, mode: :compat)
end

#to_s ⇒ Object



78
79
80
# File 'lib/fleck/core/consumer/response.rb', line 78

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