Class: ChangeHealth::Response::ResponseData

Inherits:
Object
  • Object
show all
Defined in:
lib/change_health/response/response_data.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data: nil, response: nil) ⇒ ResponseData

Returns a new instance of ResponseData.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/change_health/response/response_data.rb', line 6

def initialize(data: nil, response: nil)
  @response = response
  @raw      = data

  begin
    @raw ||= response&.parsed_response
  rescue JSON::ParserError
  end

  @raw ||= {}
end

Instance Attribute Details

#rawObject (readonly)

Returns the value of attribute raw.



4
5
6
# File 'lib/change_health/response/response_data.rb', line 4

def raw
  @raw
end

#responseObject (readonly)

Returns the value of attribute response.



4
5
6
# File 'lib/change_health/response/response_data.rb', line 4

def response
  @response
end

Instance Method Details

#errorsObject



24
25
26
27
28
# File 'lib/change_health/response/response_data.rb', line 24

def errors
  errors = @raw.dig('errors') || []

  errors.flatten.map { |error| ChangeHealth::Response::Error.new(error) }
end

#errors?Boolean

Returns:

  • (Boolean)


18
19
20
21
22
# File 'lib/change_health/response/response_data.rb', line 18

def errors?
  field_error = errors.is_a?(Array) && false == errors.empty?

  field_error || server_error.is_a?(ChangeHealthException)
end

#recommend_retry?Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
40
41
42
43
44
# File 'lib/change_health/response/response_data.rb', line 34

def recommend_retry?
  return false unless errors?

  return true if errors.any?(&:represents_down?)

  error_codes = errors.select(&:code?)

  return false if error_codes.empty?

  error_codes.all?(&:retryable?)
end

#server_errorObject



30
31
32
# File 'lib/change_health/response/response_data.rb', line 30

def server_error
  ChangeHealthException.from_response(@response, msg: 'Request') if @raw.dig('error')
end