Sha256: 329000a51df1fff1f531b472b3c2b53a808422aed143d6d4e5913d665e7a8587
Contents?: true
Size: 1.04 KB
Versions: 4
Compression:
Stored size: 1.04 KB
Contents
require 'httparty' require 'dry-struct' require 'dry-types' module Nova module API class Response < Nova::API::Utils::BaseStruct attribute? :record, Dry::Types['nominal.any'] attribute :errors, Dry::Types['strict.array'].of(Dry::Types['coercible.string']) attribute :success, Dry::Types['strict.bool'] def self.build(response, object = nil) success = response.success? parsed_response = response.parsed_response.to_h record = nil errors = extract_error_from_response('error', parsed_response) errors ||= extract_error_from_response('errors', parsed_response) errors ||= [] if object record = object.class.new(object.attributes.merge(parsed_response)) end new(success: success, errors: errors, record: record) 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
4 entries across 4 versions & 1 rubygems
Version | Path |
---|---|
nova-api-0.7.0 | lib/nova/api/response.rb |
nova-api-0.6.0 | lib/nova/api/response.rb |
nova-api-0.5.0 | lib/nova/api/response.rb |
nova-api-0.4.0 | lib/nova/api/response.rb |