Class: Attio::Resources::Base Private

Inherits:
Object
  • Object
show all
Defined in:
lib/attio/resources/base.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Since:

  • 1.0.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Base

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initialize a new resource instance.

Parameters:

  • client (Client)

    The API client instance

Raises:

  • (ArgumentError)

    if client is nil

Since:

  • 1.0.0



21
22
23
24
25
# File 'lib/attio/resources/base.rb', line 21

def initialize(client)
  raise ArgumentError, "Client is required" unless client

  @client = client
end

Instance Attribute Details

#clientClient (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns The API client instance.

Returns:

  • (Client)

    The API client instance

Since:

  • 1.0.0



15
16
17
# File 'lib/attio/resources/base.rb', line 15

def client
  @client
end

Instance Method Details

#add_filter_param(params, filter) ⇒ Object (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 1.0.0



157
158
159
# File 'lib/attio/resources/base.rb', line 157

private def add_filter_param(params, filter)
  params[:filter] = filter.is_a?(String) ? filter : filter.to_json
end

#build_query_params(options = {}) ⇒ Hash (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Build query parameters with filtering and sorting support

Parameters:

  • options (Hash) (defaults to: {})

    Options including filter, sort, limit, offset

Returns:

  • (Hash)

    Formatted query parameters

Since:

  • 1.0.0



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/attio/resources/base.rb', line 136

private def build_query_params(options = {})
  params = {}

  # Add filtering
  add_filter_param(params, options[:filter]) if options[:filter]

  # Add standard parameters
  %i[sort limit offset].each do |key|
    params[key] = options[key] if options[key]
  end

  # Add any other parameters
  options.each do |key, value|
    next if %i[filter sort limit offset].include?(key)

    params[key] = value
  end

  params
end

#handle_delete_request(connection, path, params) ⇒ Object (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 1.0.0



104
105
106
# File 'lib/attio/resources/base.rb', line 104

private def handle_delete_request(connection, path, params)
  params.empty? ? connection.delete(path) : connection.delete(path, params)
end

#handle_get_request(connection, path, params) ⇒ Object (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 1.0.0



96
97
98
# File 'lib/attio/resources/base.rb', line 96

private def handle_get_request(connection, path, params)
  params.empty? ? connection.get(path) : connection.get(path, params)
end

#handle_post_request(connection, path, params) ⇒ Object (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 1.0.0



100
101
102
# File 'lib/attio/resources/base.rb', line 100

private def handle_post_request(connection, path, params)
  params.empty? ? connection.post(path) : connection.post(path, params)
end

#paginate(path, params = {}, page_size: 50) ⇒ Enumerator (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Paginate through all results for a given endpoint

Parameters:

  • path (String)

    The API endpoint path

  • params (Hash) (defaults to: {})

    Query parameters including filters

  • page_size (Integer) (defaults to: 50)

    Number of items per page (default: 50)

Returns:

  • (Enumerator)

    Yields each item from all pages

Since:

  • 1.0.0



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/attio/resources/base.rb', line 113

private def paginate(path, params = {}, page_size: 50)
  Enumerator.new do |yielder|
    offset = 0
    loop do
      page_params = params.merge(limit: page_size, offset: offset)
      # Use POST for query endpoints, GET for others
      method = path.end_with?("/query") ? :post : :get
      response = request(method, path, page_params)

      data = response["data"] || []
      data.each { |item| yielder << item }

      # Stop if we got fewer items than requested (last page)
      break if data.size < page_size

      offset += page_size
    end
  end
end

#request(method, path, params = {}, _headers = {}) ⇒ Object (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 1.0.0



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/attio/resources/base.rb', line 76

private def request(method, path, params = {}, _headers = {})
  # Path is already safely constructed by the resource methods
  connection = client.connection

  case method
  when :get
    handle_get_request(connection, path, params)
  when :post
    handle_post_request(connection, path, params)
  when :patch
    connection.patch(path, params)
  when :put
    connection.put(path, params)
  when :delete
    handle_delete_request(connection, path, params)
  else
    raise ArgumentError, "Unsupported HTTP method: #{method}"
  end
end

#validate_data!(data, operation = "Operation") ⇒ Object (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Validates that data is not empty

Parameters:

  • data (Hash)

    The data to validate

  • operation (String) (defaults to: "Operation")

    The operation name for the error message

Raises:

  • (ArgumentError)

    if data is empty

  • (ArgumentError)

Since:

  • 1.0.0



43
44
45
# File 'lib/attio/resources/base.rb', line 43

private def validate_data!(data, operation = "Operation")
  raise ArgumentError, "#{operation} data is required" if data.nil? || data.empty?
end

#validate_id!(id, resource_name = "Resource") ⇒ Object (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Validates that an ID parameter is present and not empty

Parameters:

  • id (String)

    The ID to validate

  • resource_name (String) (defaults to: "Resource")

    The resource name for the error message

Raises:

  • (ArgumentError)

    if id is nil or empty

  • (ArgumentError)

Since:

  • 1.0.0



33
34
35
36
37
# File 'lib/attio/resources/base.rb', line 33

private def validate_id!(id, resource_name = "Resource")
  return unless id.nil? || id.to_s.strip.empty?

  raise ArgumentError, "#{resource_name} ID is required"
end

#validate_parent!(parent_object, parent_record_id) ⇒ Object (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Validates parent object and record ID together

Parameters:

  • parent_object (String)

    The parent object type

  • parent_record_id (String)

    The parent record ID

Raises:

  • (ArgumentError)

    if either is missing

Since:

  • 1.0.0



71
72
73
74
# File 'lib/attio/resources/base.rb', line 71

private def validate_parent!(parent_object, parent_record_id)
  validate_required_string!(parent_object, "Parent object")
  validate_required_string!(parent_record_id, "Parent record ID")
end

#validate_required_hash!(value, field_name) ⇒ Object (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Validates that a hash parameter is present

Parameters:

  • value (Hash)

    The hash to validate

  • field_name (String)

    The field name for the error message

Raises:

  • (ArgumentError)

    if value is nil or not a hash

  • (ArgumentError)

Since:

  • 1.0.0



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

private def validate_required_hash!(value, field_name)
  return if value.is_a?(Hash) && !value.nil?

  raise ArgumentError, "#{field_name} must be a hash"
end

#validate_required_string!(value, field_name) ⇒ Object (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Validates that a string parameter is present and not empty

Parameters:

  • value (String)

    The value to validate

  • field_name (String)

    The field name for the error message

Raises:

  • (ArgumentError)

    if value is nil or empty

  • (ArgumentError)

Since:

  • 1.0.0



51
52
53
54
55
# File 'lib/attio/resources/base.rb', line 51

private def validate_required_string!(value, field_name)
  return unless value.nil? || value.to_s.strip.empty?

  raise ArgumentError, "#{field_name} is required"
end