Class: Attio::Resources::Base Private
- Inherits:
-
Object
- Object
- Attio::Resources::Base
- 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.
Direct Known Subclasses
Attributes, Bulk, Comments, Deals, Lists, Meta, Notes, Objects, Records, Tasks, Threads, Users, WorkspaceMembers, Workspaces
Instance Attribute Summary collapse
-
#client ⇒ Client
readonly
private
The API client instance.
Instance Method Summary collapse
- #add_filter_param(params, filter) ⇒ Object private private
-
#build_query_params(options = {}) ⇒ Hash
private
private
Build query parameters with filtering and sorting support.
- #handle_delete_request(connection, path, params) ⇒ Object private private
- #handle_get_request(connection, path, params) ⇒ Object private private
- #handle_post_request(connection, path, params) ⇒ Object private private
-
#initialize(client) ⇒ Base
constructor
private
Initialize a new resource instance.
-
#paginate(path, params = {}, page_size: 50) ⇒ Enumerator
private
private
Paginate through all results for a given endpoint.
- #request(method, path, params = {}, _headers = {}) ⇒ Object private private
-
#validate_data!(data, operation = "Operation") ⇒ Object
private
private
Validates that data is not empty.
-
#validate_id!(id, resource_name = "Resource") ⇒ Object
private
private
Validates that an ID parameter is present and not empty.
-
#validate_parent!(parent_object, parent_record_id) ⇒ Object
private
private
Validates parent object and record ID together.
-
#validate_required_hash!(value, field_name) ⇒ Object
private
private
Validates that a hash parameter is present.
-
#validate_required_string!(value, field_name) ⇒ Object
private
private
Validates that a string parameter is present and not empty.
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.
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
#client ⇒ Client (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.
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.
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
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( = {}) params = {} # Add filtering add_filter_param(params, [:filter]) if [:filter] # Add standard parameters %i[sort limit offset].each do |key| params[key] = [key] if [key] end # Add any other parameters .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.
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.
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.
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
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.
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
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
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
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
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
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 |