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

rubocop:disable Lint/SuppressedException



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/change_health/response/response_data.rb', line 9

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.



6
7
8
# File 'lib/change_health/response/response_data.rb', line 6

def raw
  @raw
end

#responseObject (readonly)

Returns the value of attribute response.



6
7
8
# File 'lib/change_health/response/response_data.rb', line 6

def response
  @response
end

Instance Method Details

#errorsObject



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

def errors
  errors = @raw['errors'] || []

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

#errors?Boolean

rubocop:enable Lint/SuppressedException

Returns:

  • (Boolean)


22
23
24
25
26
# File 'lib/change_health/response/response_data.rb', line 22

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

  field_error || server_error.is_a?(ChangeHealthException)
end

#recommend_retry?Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
44
45
46
47
48
# File 'lib/change_health/response/response_data.rb', line 38

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

#request_bodyObject



50
51
52
# File 'lib/change_health/response/response_data.rb', line 50

def request_body
  @response.request.options[:body] unless @response&.request&.options.nil?
end

#server_errorObject



34
35
36
# File 'lib/change_health/response/response_data.rb', line 34

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