Class: Attio::List

Inherits:
APIResource show all
Defined in:
lib/attio/resources/list.rb

Overview

Represents a list in Attio for organizing records

Constant Summary

Constants inherited from APIResource

APIResource::SKIP_KEYS

Instance Attribute Summary collapse

Attributes inherited from APIResource

#created_at, #id, #metadata

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from APIResource

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

Constructor Details

#initialize(attributes = {}, opts = {}) ⇒ List

Returns a new instance of List.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/attio/resources/list.rb', line 31

def initialize(attributes = {}, opts = {})
  super

  # Now we can safely use symbol keys only since parent normalized them
  normalized_attrs = normalize_attributes(attributes)
  @api_slug = normalized_attrs[:api_slug]
  @name = normalized_attrs[:name]
  @attio_object_id = normalized_attrs[:object_id]
  @object_api_slug = normalized_attrs[:object_api_slug]
  @created_by_actor = normalized_attrs[:created_by_actor]
  @workspace_id = normalized_attrs[:workspace_id]
  @workspace_access = normalized_attrs[:workspace_access]
  @parent_object = normalized_attrs[:parent_object] || normalized_attrs[:object]
  @filters = normalized_attrs[:filters]
end

Instance Attribute Details

#api_slugObject (readonly)

Read-only attributes



21
22
23
# File 'lib/attio/resources/list.rb', line 21

def api_slug
  @api_slug
end

#attio_object_idObject (readonly)

Read-only attributes



21
22
23
# File 'lib/attio/resources/list.rb', line 21

def attio_object_id
  @attio_object_id
end

#created_by_actorObject (readonly)

Read-only attributes



21
22
23
# File 'lib/attio/resources/list.rb', line 21

def created_by_actor
  @created_by_actor
end

#filtersObject (readonly)

Read-only attributes



21
22
23
# File 'lib/attio/resources/list.rb', line 21

def filters
  @filters
end

#object_api_slugObject (readonly)

Read-only attributes



21
22
23
# File 'lib/attio/resources/list.rb', line 21

def object_api_slug
  @object_api_slug
end

#parent_objectObject (readonly)

Read-only attributes



21
22
23
# File 'lib/attio/resources/list.rb', line 21

def parent_object
  @parent_object
end

#workspace_idObject (readonly)

Read-only attributes



21
22
23
# File 'lib/attio/resources/list.rb', line 21

def workspace_id
  @workspace_id
end

Class Method Details

.create(**kwargs) ⇒ Object

Override create to handle keyword arguments properly



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

def create(**kwargs)
  # Extract options from kwargs
  opts = {}
  opts[:api_key] = kwargs.delete(:api_key) if kwargs.key?(:api_key)

  prepared_params = prepare_params_for_create(kwargs)
  response = execute_request(:POST, resource_path, prepared_params, opts)
  new(response["data"] || response, opts)
end

.find_by_slug(slug, **opts) ⇒ Object

Find list by API slug



179
180
181
182
# File 'lib/attio/resources/list.rb', line 179

def find_by_slug(slug, **opts)
  list(**opts).find { |lst| lst.api_slug == slug } ||
    raise(NotFoundError, "List with slug '#{slug}' not found")
end

.for_object(object, params = {}) ⇒ Object

Get lists for a specific object



185
186
187
# File 'lib/attio/resources/list.rb', line 185

def for_object(object, params = {}, **)
  list(params.merge(object: object), **)
end

.prepare_params_for_create(params) ⇒ Object

Override create to handle special parameters



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/attio/resources/list.rb', line 153

def prepare_params_for_create(params)
  validate_object_identifier!(params[:object])

  # Generate api_slug from name if not provided
  api_slug = params[:api_slug] || params[:name].downcase.gsub(/[^a-z0-9]+/, "_")

  {
    data: {
      name: params[:name],
      parent_object: params[:object],
      api_slug: api_slug,
      workspace_access: params[:workspace_access] || "full-access",
      workspace_member_access: params[:workspace_member_access] || [],
      filters: params[:filters]
    }.compact
  }
end

.prepare_params_for_update(params) ⇒ Object

Override update to handle data wrapper



172
173
174
175
176
# File 'lib/attio/resources/list.rb', line 172

def prepare_params_for_update(params)
  {
    data: params
  }
end

.resource_pathString

API endpoint path for lists

Returns:

  • (String)

    The API path



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

def self.resource_path
  "lists"
end

.retrieve(id, **opts) ⇒ Object

Override retrieve to handle complex IDs



135
136
137
138
139
# File 'lib/attio/resources/list.rb', line 135

def retrieve(id, **opts)
  list_id = id.is_a?(Hash) ? id["list_id"] : id
  response = execute_request(:GET, "#{resource_path}/#{list_id}", {}, opts)
  new(response["data"] || response, opts)
end

Instance Method Details

#add_record(record_id, **opts) ⇒ Object

Add a record to this list



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/attio/resources/list.rb', line 83

def add_record(record_id, **opts)
  list_id = id.is_a?(Hash) ? id["list_id"] : id
  client = Attio.client(api_key: opts[:api_key])

  # The API expects parent_record_id, parent_object, and entry_values
  request_data = {
    data: {
      parent_record_id: record_id,
      parent_object: object, # Get the parent object from the list
      entry_values: {}
    }
  }

  response = client.post("lists/#{list_id}/entries", request_data)
  # Return the entry data
  response["data"] || response
end

#contains_record?(record_id) ⇒ Boolean

Check if a record is in this list

Returns:

  • (Boolean)


109
110
111
# File 'lib/attio/resources/list.rb', line 109

def contains_record?(record_id, **)
  entries({record_id: record_id}, **).any?
end

#destroy(**opts) ⇒ Object

Lists cannot be deleted via API

Raises:

  • (NotImplementedError)


69
70
71
# File 'lib/attio/resources/list.rb', line 69

def destroy(**opts)
  raise NotImplementedError, "Lists cannot be deleted via the Attio API"
end

#entries(params = {}, **opts) ⇒ Object

Get all entries in this list



74
75
76
77
78
79
80
# File 'lib/attio/resources/list.rb', line 74

def entries(params = {}, **opts)
  list_id = id.is_a?(Hash) ? (id[:list_id] || id["list_id"]) : id
  client = Attio.client(api_key: opts[:api_key])
  # Use POST query endpoint to get entries
  response = client.post("lists/#{list_id}/entries/query", params)
  response["data"] || []
end

#entry_countObject

Get the count of entries



114
115
116
117
# File 'lib/attio/resources/list.rb', line 114

def entry_count(**)
  # Just get the entries and count them
  entries(**).length
end

#id_for_pathObject

Override the default id extraction for API paths



54
55
56
57
# File 'lib/attio/resources/list.rb', line 54

def id_for_path
  return nil unless persisted?
  id.is_a?(Hash) ? (id[:list_id] || id["list_id"]) : id
end

#objectObject

Get the parent object as a string



25
26
27
28
29
# File 'lib/attio/resources/list.rb', line 25

def object
  # parent_object is returned as an array from the API
  return nil unless @parent_object
  @parent_object.is_a?(Array) ? @parent_object.first : @parent_object
end

#remove_record(entry_id, **opts) ⇒ Object

Remove a record from this list



102
103
104
105
106
# File 'lib/attio/resources/list.rb', line 102

def remove_record(entry_id, **opts)
  list_id = id.is_a?(Hash) ? id["list_id"] : id
  client = Attio.client(api_key: opts[:api_key])
  client.delete("lists/#{list_id}/entries/#{entry_id}")
end

#resource_pathObject



47
48
49
50
51
# File 'lib/attio/resources/list.rb', line 47

def resource_path
  raise InvalidRequestError, "Cannot generate path without an ID" unless persisted?
  list_id = id.is_a?(Hash) ? (id[:list_id] || id["list_id"]) : id
  "#{self.class.resource_path}/#{list_id}"
end

#saveObject

Override save to handle nested ID



60
61
62
63
64
65
66
# File 'lib/attio/resources/list.rb', line 60

def save(**)
  raise InvalidRequestError, "Cannot save a list without an ID" unless persisted?
  return self unless changed?

  list_id = id.is_a?(Hash) ? (id[:list_id] || id["list_id"]) : id
  self.class.update(list_id, changed_attributes, **)
end

#to_hHash

Convert list to hash representation

Returns:

  • (Hash)

    List data as a hash



121
122
123
124
125
126
127
128
129
130
131
# File 'lib/attio/resources/list.rb', line 121

def to_h
  super.merge(
    api_slug: api_slug,
    name: name,
    object_id: attio_object_id,
    object_api_slug: object_api_slug,
    created_by_actor: created_by_actor,
    workspace_id: workspace_id,
    workspace_access: workspace_access
  ).compact
end