Class: Attio::Util::IdExtractor

Inherits:
Object
  • Object
show all
Defined in:
lib/attio/util/id_extractor.rb

Overview

Centralized ID extraction utility for handling various ID formats across different Attio resources

Class Method Summary collapse

Class Method Details

.extract(id, key = nil) ⇒ String?

Extract an ID from various formats

Parameters:

  • id (String, Hash, nil)

    The ID in various formats

  • key (Symbol, String) (defaults to: nil)

    The key to extract from a hash ID

Returns:

  • (String, nil)

    The extracted ID



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/attio/util/id_extractor.rb', line 13

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

.extract_for_resource(id, resource_type) ⇒ String?

Extract a specific ID type from a potentially nested structure

Parameters:

  • id (String, Hash, nil)

    The ID structure

  • resource_type (Symbol)

    The resource type (:record, :webhook, :attribute, etc.)

Returns:

  • (String, nil)

    The extracted ID



30
31
32
33
34
35
36
37
# File 'lib/attio/util/id_extractor.rb', line 30

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

.normalize(id, resource_type) ⇒ Hash, ...

Normalize an ID structure to a consistent format

Parameters:

  • id (String, Hash, nil)

    The ID to normalize

  • resource_type (Symbol)

    The resource type

Returns:

  • (Hash, String, nil)

    The normalized ID



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/attio/util/id_extractor.rb', line 43

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