Class: Attio::Company

Inherits:
TypedRecord show all
Defined in:
lib/attio/resources/company.rb

Overview

Represents a company record in Attio Provides convenient methods for working with companies and their attributes

Constant Summary

Constants inherited from APIResource

APIResource::SKIP_KEYS

Instance Attribute Summary

Attributes inherited from Internal::Record

#attio_object_id, #object_api_slug

Attributes inherited from APIResource

#created_at, #id, #metadata

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from TypedRecord

all, delete, #destroy, find, find_by, #initialize, list, object_type, retrieve, #save, search, update

Methods inherited from Internal::Record

#add_to_list, #destroy, #initialize, #inspect, list, #lists, resource_path, #resource_path, retrieve, #save, search, #to_h, update

Methods inherited from APIResource

#==, #[], #[]=, api_operations, attr_attio, #changed, #changed?, #changed_attributes, #changes, #destroy, #each, execute_request, #fetch, #hash, id_param_name, #initialize, #inspect, #key?, #keys, #persisted?, prepare_params_for_create, prepare_params_for_update, #reset_changes!, resource_name, resource_path, #resource_path, #revert!, #save, #to_h, #to_json, #update_attributes, #update_from, validate_id!, #values

Constructor Details

This class inherits a constructor from Attio::TypedRecord

Class Method Details

.create(name:, domain: nil, domains: nil, description: nil, employee_count: nil, values: {}, **opts) ⇒ Object

Create a company with a simplified interface



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/attio/resources/company.rb', line 160

def create(name:, domain: nil, domains: nil, description: nil,
  employee_count: nil, values: {}, **opts)
  # Name is required and simple for companies
  values[:name] = name

  # Handle domains
  if domain || domains
    domain_list = []
    domain_list << domain if domain
    domain_list += Array(domains) if domains
    values[:domains] = domain_list.uniq unless domain_list.empty?
  end

  values[:description] = description if description
  values[:employee_count] = employee_count.to_s if employee_count

  super(values: values, **opts)
end

.find_by_size(min, max = nil, **opts) ⇒ Object

Find companies by employee count range



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/attio/resources/company.rb', line 182

def find_by_size(min, max = nil, **opts)
  filter = if max
    {
      employee_count: {
        "$gte": min.to_s,
        "$lte": max.to_s
      }
    }
  else
    {
      employee_count: {"$gte": min.to_s}
    }
  end

  list(**opts.merge(params: {filter: filter}))
end

Instance Method Details

#add_domain(domain) ⇒ Object

Add a domain



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/attio/resources/company.rb', line 25

def add_domain(domain)
  domains = self[:domains] || []
  # Ensure it's an array
  domains = [domains] unless domains.is_a?(Array)

  # Normalize domain (remove protocol if present)
  domain = domain.sub(/^https?:\/\//, "")

  # Check if domain already exists
  exists = domains.any? { |d|
    d.is_a?(Hash) ? (d["domain"] == domain || d[:domain] == domain) : d == domain
  }

  unless exists
    # Extract just the domain strings if we have hashes
    domain_strings = domains.filter_map { |d|
      d.is_a?(Hash) ? (d["domain"] || d[:domain]) : d
    }

    # Add the new domain
    domain_strings << domain

    # Set as simple array of strings
    self[:domains] = domain_strings
  end
end

#add_team_member(person) ⇒ Object

Add a team member (person) to this company



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/attio/resources/company.rb', line 122

def add_team_member(person)
  # This would typically be done from the Person side
  # but we can provide a convenience method
  if person.is_a?(Person)
    person.company = self
    person.save
  elsif person.is_a?(String)
    # If it's an ID, we need to fetch and update the person
    retrieved_person = Person.retrieve(person)
    retrieved_person.company = self
    retrieved_person.save
  else
    raise ArgumentError, "Team member must be a Person instance or ID string"
  end
end

#description=(desc) ⇒ Object

Set the company description



110
111
112
# File 'lib/attio/resources/company.rb', line 110

def description=(desc)
  self[:description] = desc
end

#domainString?

Get the primary domain



54
55
56
57
58
59
# File 'lib/attio/resources/company.rb', line 54

def domain
  domains = self[:domains]
  return nil unless domains

  extract_primary_value(domains, "domain")
end

#domains_listArray<String>

Get all domains



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/attio/resources/company.rb', line 63

def domains_list
  domains = self[:domains]
  return [] unless domains

  case domains
  when Array
    domains.filter_map { |d| extract_field_value(d, "domain") }
  else
    [domain].compact
  end
end

#employee_count=(count) ⇒ Object

Set the employee count



116
117
118
# File 'lib/attio/resources/company.rb', line 116

def employee_count=(count)
  self[:employee_count] = count.to_s
end

#nameString?

Get the company name



19
20
21
# File 'lib/attio/resources/company.rb', line 19

def name
  self[:name]
end

#name=(name) ⇒ Object

Set the company name (much simpler than person names!)



13
14
15
# File 'lib/attio/resources/company.rb', line 13

def name=(name)
  self[:name] = name
end

#team_members(**opts) ⇒ Attio::ListObject

Get all people associated with this company



140
141
142
143
144
145
146
147
148
149
150
# File 'lib/attio/resources/company.rb', line 140

def team_members(**opts)
  company_id = id.is_a?(Hash) ? id["record_id"] : id
  Person.list(**opts.merge(params: {
    filter: {
      company: {
        target_object: "companies",
        target_record_id: company_id
      }
    }
  }))
end