Class: KapsoClientRuby::Resources::Contacts

Inherits:
Object
  • Object
show all
Defined in:
lib/kapso_client_ruby/resources/contacts.rb

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Contacts

Returns a new instance of Contacts.



6
7
8
# File 'lib/kapso_client_ruby/resources/contacts.rb', line 6

def initialize(client)
  @client = client
end

Instance Method Details

#add_tags(phone_number_id:, wa_id:, tags:) ⇒ Object

Add tags to contact (Kapso Proxy only)

Raises:

  • (ArgumentError)


71
72
73
74
75
76
77
78
79
80
# File 'lib/kapso_client_ruby/resources/contacts.rb', line 71

def add_tags(phone_number_id:, wa_id:, tags:)
  raise ArgumentError, 'tags cannot be empty' if tags.nil? || tags.empty?
  
  current_contact = get(phone_number_id: phone_number_id, wa_id: wa_id)
  existing_tags = (current_contact.&.[]('tags') || [])
  new_tags = (existing_tags + Array(tags)).uniq
  
  update(phone_number_id: phone_number_id, wa_id: wa_id, 
         metadata: { tags: new_tags })
end

#analytics(phone_number_id:, wa_id: nil, since: nil, until_time: nil, granularity: 'day', metrics: nil) ⇒ Object

Get contact analytics (Kapso Proxy only)



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

def analytics(phone_number_id:, wa_id: nil, since: nil, until_time: nil, 
              granularity: 'day', metrics: nil)
  assert_kapso_proxy('Contact Analytics API')
  
  query_params = {
    wa_id: wa_id,
    since: since,
    until: until_time,
    granularity: granularity
  }
  query_params[:metrics] = Array(metrics).join(',') if metrics
  query_params = query_params.compact
  
  response = @client.request(:get, "#{phone_number_id}/contacts/analytics", 
                             query: query_params, response_type: :json)
  response
end

#export(phone_number_id:, format: 'csv', filters: nil) ⇒ Object

Export contacts (Kapso Proxy only)



134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/kapso_client_ruby/resources/contacts.rb', line 134

def export(phone_number_id:, format: 'csv', filters: nil)
  assert_kapso_proxy('Contacts Export API')
  
  payload = {
    format: format,
    filters: filters
  }.compact
  
  response = @client.request(:post, "#{phone_number_id}/contacts/export", 
                             body: payload.to_json, response_type: :json)
  response
end

#get(phone_number_id:, wa_id:, fields: nil) ⇒ Object

Get contact details (Kapso Proxy only)

Raises:

  • (ArgumentError)


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/kapso_client_ruby/resources/contacts.rb', line 31

def get(phone_number_id:, wa_id:, fields: nil)
  assert_kapso_proxy('Contacts API')
  
  raise ArgumentError, 'wa_id cannot be empty' if wa_id.nil? || wa_id.strip.empty?
  
  query_params = {}
  query_params[:fields] = fields if fields
  
  response = @client.request(:get, "#{phone_number_id}/contacts/#{wa_id}", 
                             query: query_params, response_type: :json)
  
  # Handle both single object and data envelope responses

  if response.is_a?(Hash) && response.key?('data')
    Types::ContactRecord.new(response['data'])
  else
    Types::ContactRecord.new(response)
  end
end

#import(phone_number_id:, file:, format: 'csv', mapping: nil, duplicate_handling: 'skip') ⇒ Object

Import contacts (Kapso Proxy only)



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/kapso_client_ruby/resources/contacts.rb', line 148

def import(phone_number_id:, file:, format: 'csv', mapping: nil, 
           duplicate_handling: 'skip')
  assert_kapso_proxy('Contacts Import API')
  
  # Build multipart form data

  form_data = {
    'format' => format,
    'duplicate_handling' => duplicate_handling
  }
  
  # Handle file parameter

  file_obj = case file
             when String
               File.open(file, 'rb')
             when File, IO, StringIO
               file
             else
               raise ArgumentError, 'file must be a File, IO object, or file path string'
             end
  
  form_data['file'] = Faraday::UploadIO.new(file_obj, 'text/csv', 'contacts.csv')
  form_data['mapping'] = mapping.to_json if mapping
  
  headers = { 'Content-Type' => 'multipart/form-data' }
  
  response = @client.request(:post, "#{phone_number_id}/contacts/import", 
                             body: form_data, headers: headers, response_type: :json)
  
  # Close file if we opened it

  file_obj.close if file.is_a?(String) && file_obj.respond_to?(:close)
  
  response
end

#list(phone_number_id:, customer_id: nil, phone_number: nil, profile_name: nil, limit: nil, after: nil, before: nil, fields: nil) ⇒ Object

List contacts (Kapso Proxy only)



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/kapso_client_ruby/resources/contacts.rb', line 11

def list(phone_number_id:, customer_id: nil, phone_number: nil, 
         profile_name: nil, limit: nil, after: nil, before: nil, fields: nil)
  assert_kapso_proxy('Contacts API')
  
  query_params = {
    customer_id: customer_id,
    phone_number: phone_number,
    profile_name: profile_name,
    limit: limit,
    after: after,
    before: before,
    fields: fields
  }.compact
  
  response = @client.request(:get, "#{phone_number_id}/contacts", 
                             query: query_params, response_type: :json)
  Types::PagedResponse.new(response, Types::ContactRecord)
end

#remove_tags(phone_number_id:, wa_id:, tags:) ⇒ Object

Remove tags from contact (Kapso Proxy only)

Raises:

  • (ArgumentError)


83
84
85
86
87
88
89
90
91
92
# File 'lib/kapso_client_ruby/resources/contacts.rb', line 83

def remove_tags(phone_number_id:, wa_id:, tags:)
  raise ArgumentError, 'tags cannot be empty' if tags.nil? || tags.empty?
  
  current_contact = get(phone_number_id: phone_number_id, wa_id: wa_id)
  existing_tags = (current_contact.&.[]('tags') || [])
  remaining_tags = existing_tags - Array(tags)
  
  update(phone_number_id: phone_number_id, wa_id: wa_id, 
         metadata: { tags: remaining_tags })
end

#search(phone_number_id:, query:, search_in: ['profile_name', 'phone_number'], limit: nil, after: nil, before: nil) ⇒ Object

Search contacts by various criteria (Kapso Proxy only)

Raises:

  • (ArgumentError)


95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/kapso_client_ruby/resources/contacts.rb', line 95

def search(phone_number_id:, query:, search_in: ['profile_name', 'phone_number'], 
           limit: nil, after: nil, before: nil)
  assert_kapso_proxy('Contacts Search API')
  
  raise ArgumentError, 'query cannot be empty' if query.nil? || query.strip.empty?
  
  query_params = {
    q: query,
    search_in: Array(search_in).join(','),
    limit: limit,
    after: after,
    before: before
  }.compact
  
  response = @client.request(:get, "#{phone_number_id}/contacts/search", 
                             query: query_params, response_type: :json)
  Types::PagedResponse.new(response, Types::ContactRecord)
end

#update(phone_number_id:, wa_id:, metadata: nil, tags: nil, customer_id: nil, notes: nil) ⇒ Object

Update contact metadata (Kapso Proxy only)

Raises:

  • (ArgumentError)


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/kapso_client_ruby/resources/contacts.rb', line 51

def update(phone_number_id:, wa_id:, metadata: nil, tags: nil, 
           customer_id: nil, notes: nil)
  assert_kapso_proxy('Contacts API')
  
  raise ArgumentError, 'wa_id cannot be empty' if wa_id.nil? || wa_id.strip.empty?
  
  payload = {}
  payload[:metadata] =  if 
  payload[:tags] = tags if tags
  payload[:customer_id] = customer_id if customer_id
  payload[:notes] = notes if notes
  
  return if payload.empty?
  
  response = @client.request(:patch, "#{phone_number_id}/contacts/#{wa_id}", 
                             body: payload.to_json, response_type: :json)
  Types::GraphSuccessResponse.new(response)
end