Sha256: 34be2f3a492c4e7e390448274e3ae999a1205f91cba96b55131575f98bc6125c

Contents?: true

Size: 1.14 KB

Versions: 1

Compression:

Stored size: 1.14 KB

Contents

require 'cogitate/models/identifiers/with_attribute_hash'

module Cogitate
  module Client
    # Responsible for transforming an encoded identifier into an identifier
    # then decorating that identifier with attributes that may be found in the
    # array of :included objects
    #
    # @see Cogitate::Models::Identifier
    # @see Cogitate::Client::IdentifierBuilder.call
    module IdentifierBuilder
      # @api public
      def self.call(encoded_identifier:, included: [], identifier_decoder: default_identifier_decoder)
        base_identifier = identifier_decoder.call(encoded_identifier: encoded_identifier)
        included_object = included.detect { |obj| obj['id'] == encoded_identifier }
        return base_identifier unless included_object
        Models::Identifiers::WithAttributeHash.new(identifier: base_identifier, attributes: included_object.fetch('attributes', {}))
      end

      def self.default_identifier_decoder
        require 'cogitate/services/identifiers_decoder' unless defined? Services::IdentifiersDecoder
        ->(encoded_identifier:) { Services::IdentifiersDecoder.call(encoded_identifier).first }
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cogitate-0.0.2 lib/cogitate/client/identifier_builder.rb