Class: Fleck::Client::Response

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Loggable

#log_error, #logger

Constructor Details

#initialize(payload) ⇒ Response

Returns a new instance of Response.



7
8
9
10
11
12
13
14
# File 'lib/fleck/client/response.rb', line 7

def initialize(payload)
  @data       = Oj.load(payload, mode: :compat).to_hash_with_indifferent_access.filtered!
  @status     = @data["status"]
  @headers    = @data["headers"] || {}
  @body       = @data["body"]
  @errors     = @data["errors"] || []
  @deprecated = !!@data["deprecated"]
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



6
7
8
# File 'lib/fleck/client/response.rb', line 6

def body
  @body
end

#deprecatedObject

Returns the value of attribute deprecated.



6
7
8
# File 'lib/fleck/client/response.rb', line 6

def deprecated
  @deprecated
end

#errorsObject

Returns the value of attribute errors.



6
7
8
# File 'lib/fleck/client/response.rb', line 6

def errors
  @errors
end

#headersObject

Returns the value of attribute headers.



6
7
8
# File 'lib/fleck/client/response.rb', line 6

def headers
  @headers
end

#statusObject

Returns the value of attribute status.



6
7
8
# File 'lib/fleck/client/response.rb', line 6

def status
  @status
end

Instance Method Details

#deprecated?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/fleck/client/response.rb', line 16

def deprecated?
  @deprecated
end

#inspectObject



43
44
45
# File 'lib/fleck/client/response.rb', line 43

def inspect
  self.to_s
end

#to_json(filter: false) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/fleck/client/response.rb', line 20

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



39
40
41
# File 'lib/fleck/client/response.rb', line 39

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