Sha256: 7836c56a7dd1182c90a64653186521eb7c6b8661675c7f3a10d59f157a8cfe55

Contents?: true

Size: 1.63 KB

Versions: 8

Compression:

Stored size: 1.63 KB

Contents

# frozen_string_literal: true

require_relative '../../utilities/response'
require_relative './mappings'

module Crunchbase
  # Get the Organization data from API
  module Models
    # Root
    class Entity
      include ::Crunchbase::Utilities::Response
      include Mappings

      def fields
        field_ids.concat(custom_fields).map(&:to_sym)
      end

      def parse_response(response, request_field_ids = [], cards = [])
        extract_fields = (request_field_ids.empty? ? field_ids : request_field_ids)

        dynamic_attributes(self, extract_fields, response.dig('properties'))
        setup_relationships(self, cards, response.dig('cards'))
        self
      end

      def setup_relationships(object, request_card_ids, response_cards)
        request_card_ids.each do |card_id|
          card_data = response_cards.dig(card_id)
          card_model = model_mappings[card_id]
          card_objects = if card_data.is_a?(Array)
                           card_data.each_with_object([]) do |data, objects|
                             new_card_instance = card_model.new
                             objects << dynamic_attributes(new_card_instance, new_card_instance.field_ids, data)
                           end
                         else
                           card_data.nil? ? nil : dynamic_attributes(card_model.new, extract_fields, data)
                         end

          dynamic_define_method(object, card_id, card_objects)
        end
      end

      def as_json
        fields.each_with_object({}) { |item, hash| hash[item] = send(item) }
      end

      private

      def custom_fields
        []
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
crunchbase4-0.2.6 lib/crunchbase/models/concerns/entity.rb
crunchbase4-0.2.5 lib/crunchbase/models/concerns/entity.rb
crunchbase4-0.2.4 lib/crunchbase/models/concerns/entity.rb
crunchbase4-0.2.2 lib/crunchbase/models/concerns/entity.rb
crunchbase4-0.2.1 lib/crunchbase/models/concerns/entity.rb
crunchbase4-0.2.0 lib/crunchbase/models/concerns/entity.rb
crunchbase4-0.1.9 lib/crunchbase/models/concerns/entity.rb
crunchbase4-0.1.8 lib/crunchbase/models/concerns/entity.rb