whatsapp_sdk 0.13.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. checksums.yaml +4 -4
  2. data/.cursorrules +53 -0
  3. data/CHANGELOG.md +10 -0
  4. data/Gemfile +1 -0
  5. data/Gemfile.lock +21 -11
  6. data/README.md +115 -170
  7. data/example.rb +197 -113
  8. data/lib/whatsapp_sdk/api/business_profile.rb +12 -14
  9. data/lib/whatsapp_sdk/api/client.rb +26 -2
  10. data/lib/whatsapp_sdk/api/medias.rb +23 -31
  11. data/lib/whatsapp_sdk/api/messages.rb +24 -61
  12. data/lib/whatsapp_sdk/api/phone_numbers.rb +32 -22
  13. data/lib/whatsapp_sdk/api/request.rb +2 -0
  14. data/lib/whatsapp_sdk/api/responses/generic_error_response.rb +4 -4
  15. data/lib/whatsapp_sdk/api/responses/http_response_error.rb +18 -0
  16. data/lib/whatsapp_sdk/api/responses/{message_error_response.rb → id_response.rb} +6 -3
  17. data/lib/whatsapp_sdk/api/responses/message_data_response.rb +17 -20
  18. data/lib/whatsapp_sdk/api/responses/pagination_records.rb +17 -0
  19. data/lib/whatsapp_sdk/api/responses/success_response.rb +3 -16
  20. data/lib/whatsapp_sdk/api/templates.rb +27 -36
  21. data/lib/whatsapp_sdk/resource/business_profile.rb +28 -0
  22. data/lib/whatsapp_sdk/resource/errors.rb +2 -0
  23. data/lib/whatsapp_sdk/resource/media.rb +18 -68
  24. data/lib/whatsapp_sdk/resource/media_component.rb +89 -0
  25. data/lib/whatsapp_sdk/resource/message_template_namespace.rb +17 -0
  26. data/lib/whatsapp_sdk/resource/phone_number.rb +41 -11
  27. data/lib/whatsapp_sdk/resource/phone_number_component.rb +25 -0
  28. data/lib/whatsapp_sdk/resource/template.rb +20 -0
  29. data/lib/whatsapp_sdk/version.rb +1 -1
  30. data/whatsapp_sdk.gemspec +2 -2
  31. metadata +18 -17
  32. data/lib/whatsapp_sdk/api/response.rb +0 -32
  33. data/lib/whatsapp_sdk/api/responses/business_profile_data_response.rb +0 -33
  34. data/lib/whatsapp_sdk/api/responses/data_response.rb +0 -17
  35. data/lib/whatsapp_sdk/api/responses/error_response.rb +0 -25
  36. data/lib/whatsapp_sdk/api/responses/media_data_response.rb +0 -30
  37. data/lib/whatsapp_sdk/api/responses/message_template_namespace_data_response.rb +0 -27
  38. data/lib/whatsapp_sdk/api/responses/phone_number_data_response.rb +0 -42
  39. data/lib/whatsapp_sdk/api/responses/phone_numbers_data_response.rb +0 -32
  40. data/lib/whatsapp_sdk/api/responses/read_message_data_response.rb +0 -29
  41. data/lib/whatsapp_sdk/api/responses/template_data_response.rb +0 -46
  42. data/lib/whatsapp_sdk/api/responses/templates_data_response.rb +0 -32
@@ -4,9 +4,6 @@ require "faraday"
4
4
  require "faraday/multipart"
5
5
 
6
6
  require_relative "request"
7
- require_relative "response"
8
- require_relative 'responses/media_data_response'
9
- require_relative 'responses/success_response'
10
7
  require_relative '../resource/media_types'
11
8
 
12
9
  module WhatsappSdk
@@ -37,17 +34,19 @@ module WhatsappSdk
37
34
  # Get Media by ID.
38
35
  #
39
36
  # @param media_id [String] Media Id.
40
- # @return [Api::Response] Response object.
41
- def media(media_id:)
37
+ # @return [Resource::Media] Media object.
38
+ def get(media_id:)
42
39
  response = send_request(
43
40
  http_method: "get",
44
41
  endpoint: "/#{media_id}"
45
42
  )
46
43
 
47
- Api::Response.new(
48
- response: response,
49
- data_class_type: Api::Responses::MediaDataResponse
50
- )
44
+ Resource::Media.from_hash(response)
45
+ end
46
+
47
+ def media(media_id:)
48
+ warn "[DEPRECATION] `media` is deprecated. Please use `get` instead."
49
+ get(media_id: media_id)
51
50
  end
52
51
 
53
52
  # Download Media by URL.
@@ -57,7 +56,7 @@ module WhatsappSdk
57
56
  # @param media_type [String] The media type e.g. "audio/mp4". See possible types in the official
58
57
  # documentation https://developers.facebook.com/docs/whatsapp/cloud-api/reference/media#supported-media-types,
59
58
  # but note that the API may allow more depending on the client.
60
- # @return [Api::Response] Response object.
59
+ # @return [Boolean] Whether the media was downloaded successfully.
61
60
  def download(url:, file_path:, media_type:)
62
61
  # Allow download of unsupported media types, since Cloud API may decide to let it through.
63
62
  # https://github.com/ignacio-chiazzo/ruby_whatsapp_sdk/discussions/127
@@ -66,17 +65,16 @@ module WhatsappSdk
66
65
  content_type_header = map_media_type_to_content_type_header(media_type)
67
66
 
68
67
  response = download_file(url: url, file_path: file_path, content_type_header: content_type_header)
69
- response = if response.code.to_i == 200
70
- { "success" => true }
71
- else
72
- { "error" => true, "status" => response.code }
73
- end
74
-
75
- Api::Response.new(
76
- response: response,
77
- data_class_type: Api::Responses::SuccessResponse,
78
- error_class_type: Api::Responses::ErrorResponse
79
- )
68
+
69
+ return true if response.code.to_i == 200
70
+
71
+ begin
72
+ body = JSON.parse(response.body)
73
+ rescue JSON::ParserError
74
+ body = { "message" => response.body }
75
+ end
76
+
77
+ raise Api::Responses::HttpResponseError.new(http_status: response.code, body: body)
80
78
  end
81
79
 
82
80
  # Upload a media.
@@ -85,7 +83,7 @@ module WhatsappSdk
85
83
  # @param type [String] Media type e.g. text/plain, video/3gp, image/jpeg, image/png. For more information,
86
84
  # see the official documentation https://developers.facebook.com/docs/whatsapp/cloud-api/reference/media#supported-media-types.
87
85
  #
88
- # @return [Api::Response] Response object.
86
+ # @return [Api::Responses::IdResponse] IdResponse object.
89
87
  def upload(sender_id:, file_path:, type:, headers: {})
90
88
  raise FileNotFoundError.new(file_path: file_path) unless File.file?(file_path)
91
89
 
@@ -103,26 +101,20 @@ module WhatsappSdk
103
101
  multipart: true
104
102
  )
105
103
 
106
- Api::Response.new(
107
- response: response,
108
- data_class_type: Api::Responses::MediaDataResponse
109
- )
104
+ Api::Responses::IdResponse.new(response["id"])
110
105
  end
111
106
 
112
107
  # Delete a Media by ID.
113
108
  #
114
109
  # @param media_id [String] Media Id.
115
- # @return [Api::Response] Response object.
110
+ # @return [Boolean] Whether the media was deleted successfully.
116
111
  def delete(media_id:)
117
112
  response = send_request(
118
113
  http_method: "delete",
119
114
  endpoint: "/#{media_id}"
120
115
  )
121
116
 
122
- Api::Response.new(
123
- response: response,
124
- data_class_type: Api::Responses::SuccessResponse
125
- )
117
+ Api::Responses::SuccessResponse.success_response?(response: response)
126
118
  end
127
119
 
128
120
  private
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "request"
4
- require_relative "response"
5
4
 
6
5
  module WhatsappSdk
7
6
  module Api
@@ -14,7 +13,7 @@ module WhatsappSdk
14
13
  # @param recipient_number [Integer] Recipient' Phone number.
15
14
  # @param message [String] Text to send.
16
15
  # @param message_id [String] The id of the message to reply to.
17
- # @return [Api::Response] Response object.
16
+ # @return [MessageDataResponse] Response object.
18
17
  def send_text(sender_id:, recipient_number:, message:, message_id: nil)
19
18
  params = {
20
19
  messaging_product: "whatsapp",
@@ -31,10 +30,7 @@ module WhatsappSdk
31
30
  headers: DEFAULT_HEADERS
32
31
  )
33
32
 
34
- Api::Response.new(
35
- response: response,
36
- data_class_type: Api::Responses::MessageDataResponse
37
- )
33
+ Api::Responses::MessageDataResponse.build_from_response(response: response)
38
34
  end
39
35
 
40
36
  # Send location.
@@ -46,7 +42,7 @@ module WhatsappSdk
46
42
  # @param name [String] Location name.
47
43
  # @param address [String] Location address.
48
44
  # @param message_id [String] The id of the message to reply to.
49
- # @return [Api::Response] Response object.
45
+ # @return [MessageDataResponse] Response object.
50
46
  def send_location(
51
47
  sender_id:, recipient_number:, longitude:, latitude:, name:, address:, message_id: nil
52
48
  )
@@ -70,10 +66,7 @@ module WhatsappSdk
70
66
  headers: DEFAULT_HEADERS
71
67
  )
72
68
 
73
- Api::Response.new(
74
- response: response,
75
- data_class_type: Api::Responses::MessageDataResponse
76
- )
69
+ Api::Responses::MessageDataResponse.build_from_response(response: response)
77
70
  end
78
71
 
79
72
  # Send an image.
@@ -84,7 +77,7 @@ module WhatsappSdk
84
77
  # @param link [String] Image link.
85
78
  # @param caption [String] Image caption.
86
79
  # @param message_id [String] The id of the message to reply to.
87
- # @return [Api::Response] Response object.
80
+ # @return [MessageDataResponse] Response object.
88
81
  def send_image(
89
82
  sender_id:, recipient_number:, image_id: nil, link: nil, caption: "", message_id: nil
90
83
  )
@@ -109,10 +102,7 @@ module WhatsappSdk
109
102
  headers: DEFAULT_HEADERS
110
103
  )
111
104
 
112
- Api::Response.new(
113
- response: response,
114
- data_class_type: Api::Responses::MessageDataResponse
115
- )
105
+ Api::Responses::MessageDataResponse.build_from_response(response: response)
116
106
  end
117
107
 
118
108
  # Send an audio.
@@ -122,7 +112,7 @@ module WhatsappSdk
122
112
  # @param audio_id [String] Audio ID.
123
113
  # @param link [String] Audio link.
124
114
  # @param message_id [String] The id of the message to reply to.
125
- # @return [Api::Response] Response object.
115
+ # @return [MessageDataResponse] Response object.
126
116
  def send_audio(sender_id:, recipient_number:, audio_id: nil, link: nil, message_id: nil)
127
117
  raise Resource::Errors::MissingArgumentError, "audio_id or link is required" if !audio_id && !link
128
118
 
@@ -142,10 +132,7 @@ module WhatsappSdk
142
132
  multipart: true
143
133
  )
144
134
 
145
- Api::Response.new(
146
- response: response,
147
- data_class_type: Api::Responses::MessageDataResponse
148
- )
135
+ Api::Responses::MessageDataResponse.build_from_response(response: response)
149
136
  end
150
137
 
151
138
  # Send a video.
@@ -156,7 +143,7 @@ module WhatsappSdk
156
143
  # @param link [String] Image link.
157
144
  # @param caption [String] Image caption.
158
145
  # @param message_id [String] The id of the message to reply to.
159
- # @return [Api::Response] Response object.
146
+ # @return [MessageDataResponse] Response object.
160
147
  def send_video(
161
148
  sender_id:, recipient_number:, video_id: nil, link: nil, caption: "", message_id: nil
162
149
  )
@@ -181,10 +168,7 @@ module WhatsappSdk
181
168
  headers: DEFAULT_HEADERS
182
169
  )
183
170
 
184
- Api::Response.new(
185
- response: response,
186
- data_class_type: Api::Responses::MessageDataResponse
187
- )
171
+ Api::Responses::MessageDataResponse.build_from_response(response: response)
188
172
  end
189
173
 
190
174
  # Send a document.
@@ -195,7 +179,7 @@ module WhatsappSdk
195
179
  # @param link [String] Image link.
196
180
  # @param caption [String] Image caption.
197
181
  # @param message_id [String] The id of the message to reply to.
198
- # @return [Api::Response] Response object.
182
+ # @return [MessageDataResponse] Response object.
199
183
  def send_document(
200
184
  sender_id:, recipient_number:, document_id: nil, link: nil, caption: "", message_id: nil, filename: nil
201
185
  )
@@ -224,10 +208,7 @@ module WhatsappSdk
224
208
  headers: DEFAULT_HEADERS
225
209
  )
226
210
 
227
- Api::Response.new(
228
- response: response,
229
- data_class_type: Api::Responses::MessageDataResponse
230
- )
211
+ Api::Responses::MessageDataResponse.build_from_response(response: response)
231
212
  end
232
213
 
233
214
  # Send a document.
@@ -237,7 +218,7 @@ module WhatsappSdk
237
218
  # @param sticker_id [String] The sticker ID.
238
219
  # @param link [String] Image link.
239
220
  # @param message_id [String] The id of the message to reply to.
240
- # @return [Api::Response] Response object.
221
+ # @return [MessageDataResponse] Response object.
241
222
  def send_sticker(sender_id:, recipient_number:, sticker_id: nil, link: nil, message_id: nil)
242
223
  raise Resource::Errors::MissingArgumentError, "sticker or link is required" if !sticker_id && !link
243
224
 
@@ -256,10 +237,7 @@ module WhatsappSdk
256
237
  headers: DEFAULT_HEADERS
257
238
  )
258
239
 
259
- Api::Response.new(
260
- response: response,
261
- data_class_type: Api::Responses::MessageDataResponse
262
- )
240
+ Api::Responses::MessageDataResponse.build_from_response(response: response)
263
241
  end
264
242
 
265
243
  # Send contacts.
@@ -270,7 +248,7 @@ module WhatsappSdk
270
248
  # @param contacts [Array<Contact>] Contacts.
271
249
  # @param contacts_json [Json] Contacts.
272
250
  # @param message_id [String] The id of the message to reply to.
273
- # @return [Api::Response] Response object.
251
+ # @return [MessageDataResponse] Response object.
274
252
  def send_contacts(
275
253
  sender_id:, recipient_number:, contacts: nil, contacts_json: {}, message_id: nil
276
254
  )
@@ -289,10 +267,7 @@ module WhatsappSdk
289
267
  headers: DEFAULT_HEADERS
290
268
  )
291
269
 
292
- Api::Response.new(
293
- response: response,
294
- data_class_type: Api::Responses::MessageDataResponse
295
- )
270
+ Api::Responses::MessageDataResponse.build_from_response(response: response)
296
271
  end
297
272
 
298
273
  # def send_interactive_button
@@ -309,7 +284,7 @@ module WhatsappSdk
309
284
  # @param interactive_json [Json] The interactive object as a Json.
310
285
  # If you pass interactive_json, you can't pass interactive.
311
286
  # @param message_id [String] The id of the message to reply to.
312
- # @return [Api::Response] Response object.
287
+ # @return [MessageDataResponse] Response object.
313
288
  def send_interactive_message(
314
289
  sender_id:, recipient_number:, interactive: nil, interactive_json: nil, message_id: nil
315
290
  )
@@ -338,10 +313,7 @@ module WhatsappSdk
338
313
  headers: DEFAULT_HEADERS
339
314
  )
340
315
 
341
- Api::Response.new(
342
- response: response,
343
- data_class_type: Api::Responses::MessageDataResponse
344
- )
316
+ Api::Responses::MessageDataResponse.build_from_response(response: response)
345
317
  end
346
318
 
347
319
  alias send_interactive_reply_buttons send_interactive_message
@@ -351,7 +323,7 @@ module WhatsappSdk
351
323
  #
352
324
  # @param sender_id [Integer] Sender' phone number.
353
325
  # @param message_id [Integer] Message ID.
354
- # @return [Api::Response] Response object.
326
+ # @return [Boolean] Whether the message was marked as read.
355
327
  def read_message(sender_id:, message_id:)
356
328
  params = {
357
329
  messaging_product: "whatsapp",
@@ -365,10 +337,7 @@ module WhatsappSdk
365
337
  headers: DEFAULT_HEADERS
366
338
  )
367
339
 
368
- Api::Response.new(
369
- response: response,
370
- data_class_type: Api::Responses::ReadMessageDataResponse
371
- )
340
+ Api::Responses::SuccessResponse.success_response?(response: response)
372
341
  end
373
342
 
374
343
  # Send template
@@ -379,7 +348,7 @@ module WhatsappSdk
379
348
  # @param language [String] template language.
380
349
  # @param components [Component] Component.
381
350
  # @param components_json [Json] The component as a Json. If you pass components_json, you can't pass components.
382
- # @return [Api::Response] Response object.
351
+ # @return [MessageDataResponse] Response object.
383
352
  def send_template(
384
353
  sender_id:, recipient_number:, name:, language:, components: nil, components_json: nil
385
354
  )
@@ -411,10 +380,7 @@ module WhatsappSdk
411
380
  headers: DEFAULT_HEADERS
412
381
  )
413
382
 
414
- Api::Response.new(
415
- response: response,
416
- data_class_type: Api::Responses::MessageDataResponse
417
- )
383
+ Api::Responses::MessageDataResponse.build_from_response(response: response)
418
384
  end
419
385
 
420
386
  # Send reaction
@@ -423,7 +389,7 @@ module WhatsappSdk
423
389
  # @param recipient_number [Integer] Recipient' Phone number.
424
390
  # @param message_id [String] the id of the message to reaction.
425
391
  # @param emoji [String] unicode of the emoji to send.
426
- # @return [Api::Response] Response object.
392
+ # @return [MessageDataResponse] Response object.
427
393
  def send_reaction(sender_id:, recipient_number:, message_id:, emoji:)
428
394
  params = {
429
395
  messaging_product: "whatsapp",
@@ -442,10 +408,7 @@ module WhatsappSdk
442
408
  headers: DEFAULT_HEADERS
443
409
  )
444
410
 
445
- Api::Response.new(
446
- response: response,
447
- data_class_type: Api::Responses::MessageDataResponse
448
- )
411
+ Api::Responses::MessageDataResponse.build_from_response(response: response)
449
412
  end
450
413
 
451
414
  private
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "request"
4
- require_relative "response"
5
4
 
6
5
  module WhatsappSdk
7
6
  module Api
@@ -16,39 +15,37 @@ module WhatsappSdk
16
15
  #
17
16
  # @param business_id [Integer] Business Id.
18
17
  # @return [Api::Response] Response object.
19
- def registered_numbers(business_id)
18
+ def list(business_id)
20
19
  response = send_request(
21
20
  http_method: "get",
22
21
  endpoint: "#{business_id}/phone_numbers?fields=#{DEFAULT_FIELDS}"
23
22
  )
24
23
 
25
- Api::Response.new(
26
- response: response,
27
- data_class_type: Api::Responses::PhoneNumbersDataResponse
24
+ Api::Responses::PaginationRecords.new(
25
+ records: parse_phone_numbers(response['data']),
26
+ before: response['paging']['cursors']['before'],
27
+ after: response['paging']['cursors']['after']
28
28
  )
29
29
  end
30
30
 
31
- # Get the registered number id.
31
+ # Get the registered number.
32
32
  #
33
33
  # @param phone_number_id [Integer] The registered number we want to retrieve.
34
- # @return [Api::Response] Response object.
35
- def registered_number(phone_number_id)
34
+ # @return [Resource::PhoneNumber] A PhoneNumber object.
35
+ def get(phone_number_id)
36
36
  response = send_request(
37
37
  http_method: "get",
38
38
  endpoint: "#{phone_number_id}?fields=#{DEFAULT_FIELDS}"
39
39
  )
40
40
 
41
- Api::Response.new(
42
- response: response,
43
- data_class_type: Api::Responses::PhoneNumberDataResponse
44
- )
41
+ Resource::PhoneNumber.from_hash(response)
45
42
  end
46
43
 
47
44
  # Register a phone number.
48
45
  #
49
46
  # @param phone_number_id [Integer] The registered number we want to retrieve.
50
47
  # @param pin [Integer] Pin of 6 digits.
51
- # @return [Api::Response] Response object.
48
+ # @return [Boolean] Whether the registration was successful.
52
49
  def register_number(phone_number_id, pin)
53
50
  response = send_request(
54
51
  http_method: "post",
@@ -56,16 +53,13 @@ module WhatsappSdk
56
53
  params: { messaging_product: 'whatsapp', pin: pin }
57
54
  )
58
55
 
59
- Api::Response.new(
60
- response: response,
61
- data_class_type: Api::Responses::PhoneNumberDataResponse
62
- )
56
+ Api::Responses::SuccessResponse.success_response?(response: response)
63
57
  end
64
58
 
65
59
  # Deregister a phone number.
66
60
  #
67
61
  # @param phone_number_id [Integer] The registered number we want to retrieve.
68
- # @return [Api::Response] Response object.
62
+ # @return [Boolean] Whether the deregistration was successful.
69
63
  def deregister_number(phone_number_id)
70
64
  response = send_request(
71
65
  http_method: "post",
@@ -73,10 +67,26 @@ module WhatsappSdk
73
67
  params: {}
74
68
  )
75
69
 
76
- Api::Response.new(
77
- response: response,
78
- data_class_type: Api::Responses::PhoneNumberDataResponse
79
- )
70
+ Api::Responses::SuccessResponse.success_response?(response: response)
71
+ end
72
+
73
+ # deprecated methods
74
+ def registered_numbers(business_id)
75
+ warn "[DEPRECATION] `registered_numbers` is deprecated. Please use `list` instead."
76
+ list(business_id)
77
+ end
78
+
79
+ def registered_number(phone_number_id)
80
+ warn "[DEPRECATION] `registered_number` is deprecated. Please use `get` instead."
81
+ get(phone_number_id)
82
+ end
83
+
84
+ private
85
+
86
+ def parse_phone_numbers(phone_numbers_data)
87
+ phone_numbers_data.map do |phone_number|
88
+ Resource::PhoneNumber.from_hash(phone_number)
89
+ end
80
90
  end
81
91
  end
82
92
  end
@@ -1,6 +1,8 @@
1
1
  # typed: true
2
2
  # frozen_string_literal: true
3
3
 
4
+ require_relative "../../whatsapp_sdk"
5
+
4
6
  module WhatsappSdk
5
7
  module Api
6
8
  class Request
@@ -1,11 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "error_response"
4
-
5
3
  module WhatsappSdk
6
4
  module Api
7
5
  module Responses
8
- class GenericErrorResponse < ErrorResponse
6
+ class GenericErrorResponse
9
7
  attr_reader :code, :subcode, :message, :type, :data, :fbtrace_id
10
8
 
11
9
  def initialize(response:)
@@ -15,8 +13,10 @@ module WhatsappSdk
15
13
  @type = response["type"]
16
14
  @data = response["data"]
17
15
  @fbtrace_id = response["fbtrace_id"]
16
+ end
18
17
 
19
- super(response: response)
18
+ def self.response_error?(response:)
19
+ response["error"]
20
20
  end
21
21
 
22
22
  def self.build_from_response(response:)
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module WhatsappSdk
4
+ module Api
5
+ module Responses
6
+ class HttpResponseError < StandardError
7
+ attr_reader :http_status, :body, :error_info
8
+
9
+ def initialize(http_status:, body:)
10
+ super
11
+ @http_status = http_status
12
+ @body = body
13
+ @error_info = GenericErrorResponse.build_from_response(response: body)
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -1,11 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "generic_error_response"
4
-
5
3
  module WhatsappSdk
6
4
  module Api
7
5
  module Responses
8
- class MessageErrorResponse < GenericErrorResponse
6
+ class IdResponse
7
+ attr_accessor :id
8
+
9
+ def initialize(id)
10
+ @id = id
11
+ end
9
12
  end
10
13
  end
11
14
  end
@@ -1,37 +1,34 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "../request"
4
- require_relative "data_response"
5
3
  require_relative "../../resource/message"
6
4
  require_relative "../../resource/contact_response"
7
5
 
8
6
  module WhatsappSdk
9
7
  module Api
10
8
  module Responses
11
- class MessageDataResponse < DataResponse
12
- attr_reader :contacts, :messages
9
+ class MessageDataResponse
10
+ attr_accessor :contacts, :messages
13
11
 
14
- def initialize(response:)
15
- @contacts = response["contacts"]&.map { |contact_json| parse_contact(contact_json) }
16
- @messages = response["messages"]&.map { |contact_json| parse_message(contact_json) }
12
+ class << self
13
+ def build_from_response(response:)
14
+ return unless response["messages"]
17
15
 
18
- super(response)
19
- end
20
-
21
- def self.build_from_response(response:)
22
- return unless response["messages"]
16
+ data_response = new
17
+ data_response.contacts = response["contacts"]&.map { |contact_json| parse_contact(contact_json) }
18
+ data_response.messages = response["messages"]&.map { |contact_json| parse_message(contact_json) }
23
19
 
24
- new(response: response)
25
- end
20
+ data_response
21
+ end
26
22
 
27
- private
23
+ private
28
24
 
29
- def parse_message(message_json)
30
- Resource::Message.new(id: message_json["id"])
31
- end
25
+ def parse_message(message_json)
26
+ Resource::Message.new(id: message_json["id"])
27
+ end
32
28
 
33
- def parse_contact(contact_json)
34
- Resource::ContactResponse.new(input: contact_json["input"], wa_id: contact_json["wa_id"])
29
+ def parse_contact(contact_json)
30
+ Resource::ContactResponse.new(input: contact_json["input"], wa_id: contact_json["wa_id"])
31
+ end
35
32
  end
36
33
  end
37
34
  end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module WhatsappSdk
4
+ module Api
5
+ module Responses
6
+ class PaginationRecords
7
+ attr_reader :records, :before, :after
8
+
9
+ def initialize(records:, before:, after:)
10
+ @records = records
11
+ @before = before
12
+ @after = after
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -1,24 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "data_response"
4
-
5
3
  module WhatsappSdk
6
4
  module Api
7
5
  module Responses
8
- class SuccessResponse < DataResponse
9
- def initialize(response:)
10
- @success = response["success"]
11
- super(response)
12
- end
13
-
14
- def self.build_from_response(response:)
15
- return unless response["success"]
16
-
17
- new(response: response)
18
- end
19
-
20
- def success?
21
- @success
6
+ class SuccessResponse
7
+ def self.success_response?(response:)
8
+ response["success"] == true
22
9
  end
23
10
  end
24
11
  end