Class: Nova::API::ListResponse

Inherits:
Utils::BaseStruct show all
Defined in:
lib/nova/api/list_response.rb

Constant Summary

Constants inherited from Utils::BaseStruct

Utils::BaseStruct::DATE_REGEX

Class Method Summary collapse

Methods inherited from Utils::BaseStruct

#allowed_attributes, generate_valid_value_for, initialize_empty_model_with_id, value_for_field

Class Method Details

.build(response, klass) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/nova/api/list_response.rb', line 13

def self.build(response, klass)
  success = response.success?
  status = response.code

  parsed_response = response.parsed_response

  records = nil

  if parsed_response.is_a?(Array)
    records = build_records(klass, parsed_response)
  else
    parsed_response = parsed_response.to_h

    errors = extract_error_from_response('error', parsed_response)
    errors ||= extract_error_from_response('errors', parsed_response)
  end

  errors ||= []

  new(success: success, errors: errors, records: records, status: status)
end

.build_records(klass, response) ⇒ Object



43
44
45
# File 'lib/nova/api/list_response.rb', line 43

def self.build_records(klass, response)
  response.map { |object| klass.new(object) } unless klass.nil?
end

.extract_error_from_response(field, response) ⇒ Object



37
38
39
40
41
# File 'lib/nova/api/list_response.rb', line 37

def self.extract_error_from_response(field, response)
  return unless response.has_key?(field)

  response[field].is_a?(Array) ? response[field] : [response[field]]
end