Sha256: 8be2c4f30622ad35b75d13f70ba8102b629bd17cd433d820424d55abd8b42179

Contents?: true

Size: 1.3 KB

Versions: 9

Compression:

Stored size: 1.3 KB

Contents

# frozen_string_literal: true

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

      def parse_response_items(domain, 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, 'domain', domain)
        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

9 entries across 9 versions & 1 rubygems

Version Path
sourcescrub-0.1.2 lib/sourcescrub/models/concerns/company_items.rb
sourcescrub-0.1.1 lib/sourcescrub/models/concerns/company_items.rb
sourcescrub-0.1.0 lib/sourcescrub/models/concerns/company_items.rb
sourcescrub-0.0.9 lib/sourcescrub/models/concerns/company_items.rb
sourcescrub-0.0.8 lib/sourcescrub/models/concerns/company_items.rb
sourcescrub-0.0.7 lib/sourcescrub/models/concerns/company_items.rb
sourcescrub-0.0.6 lib/sourcescrub/models/concerns/company_items.rb
sourcescrub-0.0.5 lib/sourcescrub/models/concerns/company_items.rb
sourcescrub-0.0.4 lib/sourcescrub/models/concerns/company_items.rb