Class: RunApi::Core::Response

Inherits:
Hash
  • Object
show all
Defined in:
lib/runapi/core/response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(body:, headers: nil) ⇒ Response

Returns a new instance of Response.



47
48
49
50
51
52
# File 'lib/runapi/core/response.rb', line 47

def initialize(body:, headers: nil)
  super()
  @body = body unless body.is_a?(Hash)
  @response_headers = headers.is_a?(ResponseHeaders) ? headers : ResponseHeaders.new(headers)
  update(body) if body.is_a?(Hash)
end

Instance Attribute Details

#response_headersObject (readonly) Also known as: headers

Returns the value of attribute response_headers.



45
46
47
# File 'lib/runapi/core/response.rb', line 45

def response_headers
  @response_headers
end

Instance Method Details

#==(other) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/runapi/core/response.rb', line 78

def ==(other)
  if @body
    @body == (other.is_a?(Response) ? other.body : other)
  else
    super(other.is_a?(Response) ? other.to_h : other)
  end
end

#[](key) ⇒ Object



60
61
62
63
64
# File 'lib/runapi/core/response.rb', line 60

def [](key)
  return @body[key] if @body.respond_to?(:[])

  super
end

#bodyObject



56
57
58
# File 'lib/runapi/core/response.rb', line 56

def body
  @body.nil? ? self : @body
end

#dig(*keys) ⇒ Object



66
67
68
69
70
# File 'lib/runapi/core/response.rb', line 66

def dig(*keys)
  return @body.dig(*keys) if @body.respond_to?(:dig)

  super
end

#to_hObject



72
73
74
75
76
# File 'lib/runapi/core/response.rb', line 72

def to_h
  return @body.to_h if !@body.nil? && @body.respond_to?(:to_h)

  super
end