class Attio::Util::IdExtractor
across different Attio resources
Centralized ID extraction utility for handling various ID formats
def common_keys
def common_keys @common_keys ||= %i[ id record_id workspace_member_id webhook_id attribute_id object_id list_id note_id comment_id task_id entry_id thread_id ].freeze end
def extract(id, key = nil)
-
(String, nil)
- The extracted ID
Parameters:
-
key
(Symbol, String
) -- The key to extract from a hash ID -
id
(String, Hash, nil
) -- The ID in various formats
def extract(id, key = nil) return nil if id.nil? case id when String id when Hash extract_from_hash(id, key) else id.to_s if id.respond_to?(:to_s) end end
def extract_for_resource(id, resource_type)
-
(String, nil)
- The extracted ID
Parameters:
-
resource_type
(Symbol
) -- The resource type (:record, :webhook, :attribute, etc.) -
id
(String, Hash, nil
) -- The ID structure
def extract_for_resource(id, resource_type) return nil if id.nil? key = resource_key_map[resource_type] return id if id.is_a?(String) && key.nil? extract(id, key) end
def extract_from_hash(hash, key = nil)
def extract_from_hash(hash, key = nil) return nil unless hash.is_a?(Hash) if key # Try both symbol and string keys hash[key] || hash[key.to_s] || hash[key.to_sym] else # Try common ID keys in order of preference common_keys.each do |k| value = hash[k] || hash[k.to_s] return value if value end nil end end
def hash_format_resources
def hash_format_resources @hash_format_resources ||= %i[record].freeze end
def normalize(id, resource_type)
-
(Hash, String, nil)
- The normalized ID
Parameters:
-
resource_type
(Symbol
) -- The resource type -
id
(String, Hash, nil
) -- The ID to normalize
def normalize(id, resource_type) return nil if id.nil? extracted = extract_for_resource(id, resource_type) return nil if extracted.nil? # For resources that need hash format if hash_format_resources.include?(resource_type) key = resource_key_map[resource_type] key ? {key => extracted} : extracted else extracted end end
def resource_key_map
def resource_key_map @resource_key_map ||= { record: :record_id, workspace_member: :workspace_member_id, webhook: :webhook_id, attribute: :attribute_id, object: :object_id, list: :list_id, note: :note_id, comment: :comment_id, task: :task_id, entry: :entry_id, thread: :thread_id }.freeze end