Class: Crunchbase::Models::Entity

Inherits:
Object
  • Object
show all
Includes:
Mappings, Utilities::Response
Defined in:
lib/crunchbase/models/concerns/entity.rb

Overview

Root

Instance Method Summary collapse

Methods included from Mappings

#card_num_field, #model_mappings

Methods included from Utilities::Response

dynamic_attributes

Instance Method Details

#as_jsonObject



43
44
45
# File 'lib/crunchbase/models/concerns/entity.rb', line 43

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

#fieldsObject



14
15
16
# File 'lib/crunchbase/models/concerns/entity.rb', line 14

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

#parse_response(response, request_field_ids = [], cards = []) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/crunchbase/models/concerns/entity.rb', line 18

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

#setup_relationships(object, request_card_ids, response_cards) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/crunchbase/models/concerns/entity.rb', line 26

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