Sha256: 9ed1c15fec282fa9d91f8a8cddfa27de647da723d993915d139d35520036c6da
Contents?: true
Size: 1.17 KB
Versions: 3
Compression:
Stored size: 1.17 KB
Contents
require 'httparty' require 'dry-struct' require 'dry-types' module Nova module API class ListResponse < Nova::API::Utils::BaseStruct attribute :records, Dry::Types['strict.array'].of(Dry::Types['nominal.any']).optional attribute :errors, Dry::Types['strict.array'].of(Dry::Types['coercible.string']) attribute :success, Dry::Types['strict.bool'] def self.build(response, klass) success = response.success? parsed_response = response.parsed_response records = nil if parsed_response.is_a?(Array) records = klass.nil? ? nil : parsed_response.map { |object| klass.new(object) } 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) end private def self.extract_error_from_response(field, response) return unless response.has_key?(field) response[field].is_a?(Array) ? response[field] : [response[field]] end end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
nova-api-0.6.0 | lib/nova/api/list_response.rb |
nova-api-0.5.0 | lib/nova/api/list_response.rb |
nova-api-0.4.0 | lib/nova/api/list_response.rb |