module Crunchbase::Utilities::Response

Parse the response. build with object

Public Instance Methods

dynamic_attributes(object, attribute_names, response) click to toggle source
# File lib/crunchbase/utilities/response.rb, line 10
def dynamic_attributes(object, attribute_names, response)
  attribute_names.each do |attribute_name|
    attribute_value = field_value(attribute_name, response)
    dynamic_define_method(object, attribute_name, attribute_value)
  end

  [attribute_names & special_attributes].flatten.each do |attribute_name|
    attribute_names.delete(attribute_name)
    hash_datas = response&.dig(attribute_name)

    values = hash_datas&.map { |k, v| v if %w[uuid permalink value].include?(k) }&.compact || []
    dynamic_define_method(object, attribute_name, values)
    hash_datas&.keys&.each do |key|
      next unless %w[uuid permalink].include?(key)

      dynamic_define_method(object, key, hash_datas&.dig(key))
    end
  end

  object
end

Private Instance Methods

dynamic_define_method(object, attribute_name, attribute_value) click to toggle source
# File lib/crunchbase/utilities/response.rb, line 34
def dynamic_define_method(object, attribute_name, attribute_value)
  # Manually creates methods for both getter and setter and then
  #   sends a message to the new setter with the attribute_value
  object.class.send(:define_method, "#{attribute_name}=".to_sym) do |value|
    instance_variable_set('@' + attribute_name, value)
  end

  object.class.send(:define_method, attribute_name.to_sym) do
    instance_variable_get('@' + attribute_name.to_s)
  end

  object.send("#{attribute_name}=".to_sym, attribute_value)
end
field_value(name, data) click to toggle source

rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity

# File lib/crunchbase/utilities/response.rb, line 56
def field_value(name, data)
  value = data.dig(name)

  return value if value.nil? || value.is_a?(String)
  return parse_items(value, name) if value.is_a?(Array) && value[0].is_a?(Hash) && value[0].keys.include?('value')
  return value.dig('value_usd') if value.is_a?(Hash) && value.keys.include?('value_usd')
  return value.dig('value') if value.is_a?(Hash) && value.keys.include?('value')

  value
end
parse_items(items, field_name) click to toggle source

rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity

# File lib/crunchbase/utilities/response.rb, line 68
def parse_items(items, field_name)
  return items.collect { |e| e.dig('value') } unless field_name == 'activity_entities'

  # Sepcial case for activity_entities
  items.each_with_object([]) do |item, objects|
    card_model = case item['entity_def_id']
                 when 'person' then ::Crunchbase::Models::Person
                 when 'organization' then ::Crunchbase::Models::Organization
                 end
    # Alias: value is name
    item['name'] ||= item['value']

    next if card_model.nil?

    new_card_instance = card_model.new
    objects << dynamic_attributes(new_card_instance, new_card_instance.basis_fields, item)
  end
end
special_attributes() click to toggle source

This is hash attributes

1. identifier
# File lib/crunchbase/utilities/response.rb, line 51
def special_attributes
  %w[identifier]
end