Sha256: 1c4142ad7d1bd0ef8a65b72af590dc6f182f9b75899470641d8d222fe3b85a5c

Contents?: true

Size: 1.37 KB

Versions: 1

Compression:

Stored size: 1.37 KB

Contents

# frozen_string_literal: true

module Sourcescrub
  # Models
  module Models
    # Tag
    class CompanyItems < Entity
      attr_accessor :identifier, :total, :items, :type

      # the identifier can be domain or SS ID
      def parse_response_items(identifier, kclass_name, response)
        headers = response.dig('headers')
        headers&.keys&.each do |attr_name|
          self.class.send(:define_method, attr_name.gsub('-', '_').to_sym) do
            headers[attr_name]
          end
        end

        dynamic_define_method(self, 'identifier', identifier)
        dynamic_define_method(self, 'type', kclass_name)
        dynamic_define_method(self, 'total', response.dig(total_key) || 0)
        dynamic_define_method(self, 'items', company_items(kclass_name, response.dig(items_key) || []))
        self
      end

      private

      def company_items(kclass_name, items)
        items.each_with_object([]) do |item, results|
          results << kclass_name.new.parse_response(item)
        end
      end

      def total_key
        case type_name
        when 'Person'
          'totalPeople'
        else
          'total'
        end
      end

      def items_key
        case type_name
        when 'Person'
          'peopleAllocations'
        else
          'items'
        end
      end

      def type_name
        type.name.split('::')[-1]
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sourcescrub-0.1.3 lib/sourcescrub/models/concerns/company_items.rb