whatsapp_sdk 0.9.2 → 0.11.0

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 (36) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +11 -1
  3. data/Gemfile +0 -6
  4. data/Gemfile.lock +16 -16
  5. data/README.md +19 -0
  6. data/example.rb +67 -1
  7. data/lib/whatsapp_sdk/api/api_configuration.rb +1 -1
  8. data/lib/whatsapp_sdk/api/business_profile.rb +8 -8
  9. data/lib/whatsapp_sdk/api/medias.rb +18 -18
  10. data/lib/whatsapp_sdk/api/messages.rb +67 -71
  11. data/lib/whatsapp_sdk/api/phone_numbers.rb +16 -16
  12. data/lib/whatsapp_sdk/api/response.rb +4 -4
  13. data/lib/whatsapp_sdk/api/responses/generic_error_response.rb +49 -0
  14. data/lib/whatsapp_sdk/api/responses/message_data_response.rb +8 -8
  15. data/lib/whatsapp_sdk/api/responses/message_error_response.rb +2 -38
  16. data/lib/whatsapp_sdk/api/responses/message_template_namespace_data_response.rb +37 -0
  17. data/lib/whatsapp_sdk/api/responses/template_data_response.rb +51 -0
  18. data/lib/whatsapp_sdk/api/responses/templates_data_response.rb +39 -0
  19. data/lib/whatsapp_sdk/api/templates.rb +201 -0
  20. data/lib/whatsapp_sdk/configuration.rb +2 -2
  21. data/lib/whatsapp_sdk/resource/component.rb +2 -2
  22. data/lib/whatsapp_sdk/resource/errors.rb +68 -0
  23. data/lib/whatsapp_sdk/resource/interactive_action.rb +5 -5
  24. data/lib/whatsapp_sdk/resource/interactive_action_reply_button.rb +2 -2
  25. data/lib/whatsapp_sdk/resource/interactive_action_section.rb +2 -2
  26. data/lib/whatsapp_sdk/resource/interactive_action_section_row.rb +5 -5
  27. data/lib/whatsapp_sdk/resource/interactive_body.rb +1 -1
  28. data/lib/whatsapp_sdk/resource/interactive_footer.rb +1 -1
  29. data/lib/whatsapp_sdk/resource/interactive_header.rb +1 -1
  30. data/lib/whatsapp_sdk/resource/languages.rb +86 -0
  31. data/lib/whatsapp_sdk/resource/parameter_object.rb +2 -2
  32. data/lib/whatsapp_sdk/resource/template.rb +64 -0
  33. data/lib/whatsapp_sdk/version.rb +1 -1
  34. data/whatsapp_sdk.gemspec +8 -8
  35. metadata +22 -15
  36. data/lib/whatsapp_sdk/resource/error.rb +0 -39
@@ -11,31 +11,18 @@ module WhatsappSdk
11
11
 
12
12
  DEFAULT_HEADERS = T.let({ 'Content-Type' => 'application/json' }.freeze, Hash)
13
13
 
14
- class MissingArgumentError < StandardError
15
- extend T::Sig
16
-
17
- sig { returns(String) }
18
- attr_reader :message
19
-
20
- sig { params(message: String).void }
21
- def initialize(message)
22
- @message = message
23
- super(message)
24
- end
25
- end
26
-
27
14
  # Send a text message.
28
15
  #
29
16
  # @param sender_id [Integer] Sender' phone number.
30
17
  # @param recipient_number [Integer] Recipient' Phone number.
31
18
  # @param message [String] Text to send.
32
19
  # @param message_id [String] The id of the message to reply to.
33
- # @return [WhatsappSdk::Api::Response] Response object.
20
+ # @return [Api::Response] Response object.
34
21
  sig do
35
22
  params(
36
23
  sender_id: Integer, recipient_number: Integer, message: String,
37
24
  message_id: T.nilable(String)
38
- ).returns(WhatsappSdk::Api::Response)
25
+ ).returns(Api::Response)
39
26
  end
40
27
  def send_text(sender_id:, recipient_number:, message:, message_id: nil)
41
28
  params = {
@@ -53,9 +40,9 @@ module WhatsappSdk
53
40
  headers: DEFAULT_HEADERS
54
41
  )
55
42
 
56
- WhatsappSdk::Api::Response.new(
43
+ Api::Response.new(
57
44
  response: response,
58
- data_class_type: WhatsappSdk::Api::Responses::MessageDataResponse
45
+ data_class_type: Api::Responses::MessageDataResponse
59
46
  )
60
47
  end
61
48
 
@@ -68,13 +55,13 @@ module WhatsappSdk
68
55
  # @param name [String] Location name.
69
56
  # @param address [String] Location address.
70
57
  # @param message_id [String] The id of the message to reply to.
71
- # @return [WhatsappSdk::Api::Response] Response object.
58
+ # @return [Api::Response] Response object.
72
59
  sig do
73
60
  params(
74
61
  sender_id: Integer, recipient_number: Integer,
75
62
  longitude: Float, latitude: Float, name: String, address: String,
76
63
  message_id: T.nilable(String)
77
- ).returns(WhatsappSdk::Api::Response)
64
+ ).returns(Api::Response)
78
65
  end
79
66
  def send_location(
80
67
  sender_id:, recipient_number:, longitude:, latitude:, name:, address:, message_id: nil
@@ -99,9 +86,9 @@ module WhatsappSdk
99
86
  headers: DEFAULT_HEADERS
100
87
  )
101
88
 
102
- WhatsappSdk::Api::Response.new(
89
+ Api::Response.new(
103
90
  response: response,
104
- data_class_type: WhatsappSdk::Api::Responses::MessageDataResponse
91
+ data_class_type: Api::Responses::MessageDataResponse
105
92
  )
106
93
  end
107
94
 
@@ -113,18 +100,18 @@ module WhatsappSdk
113
100
  # @param link [String] Image link.
114
101
  # @param caption [String] Image caption.
115
102
  # @param message_id [String] The id of the message to reply to.
116
- # @return [WhatsappSdk::Api::Response] Response object.
103
+ # @return [Api::Response] Response object.
117
104
  sig do
118
105
  params(
119
106
  sender_id: Integer, recipient_number: Integer, image_id: T.nilable(String),
120
107
  link: T.nilable(String), caption: T.nilable(String),
121
108
  message_id: T.nilable(String)
122
- ).returns(WhatsappSdk::Api::Response)
109
+ ).returns(Api::Response)
123
110
  end
124
111
  def send_image(
125
112
  sender_id:, recipient_number:, image_id: nil, link: nil, caption: "", message_id: nil
126
113
  )
127
- raise MissingArgumentError, "image_id or link is required" if !image_id && !link
114
+ raise Resource::Errors::MissingArgumentError, "image_id or link is required" if !image_id && !link
128
115
 
129
116
  params = {
130
117
  messaging_product: "whatsapp",
@@ -145,9 +132,9 @@ module WhatsappSdk
145
132
  headers: DEFAULT_HEADERS
146
133
  )
147
134
 
148
- WhatsappSdk::Api::Response.new(
135
+ Api::Response.new(
149
136
  response: response,
150
- data_class_type: WhatsappSdk::Api::Responses::MessageDataResponse
137
+ data_class_type: Api::Responses::MessageDataResponse
151
138
  )
152
139
  end
153
140
 
@@ -158,15 +145,15 @@ module WhatsappSdk
158
145
  # @param audio_id [String] Audio ID.
159
146
  # @param link [String] Audio link.
160
147
  # @param message_id [String] The id of the message to reply to.
161
- # @return [WhatsappSdk::Api::Response] Response object.
148
+ # @return [Api::Response] Response object.
162
149
  sig do
163
150
  params(
164
151
  sender_id: Integer, recipient_number: Integer, audio_id: T.nilable(String),
165
152
  link: T.nilable(String), message_id: T.nilable(String)
166
- ).returns(WhatsappSdk::Api::Response)
153
+ ).returns(Api::Response)
167
154
  end
168
155
  def send_audio(sender_id:, recipient_number:, audio_id: nil, link: nil, message_id: nil)
169
- raise MissingArgumentError, "audio_id or link is required" if !audio_id && !link
156
+ raise Resource::Errors::MissingArgumentError, "audio_id or link is required" if !audio_id && !link
170
157
 
171
158
  params = {
172
159
  messaging_product: "whatsapp",
@@ -183,9 +170,9 @@ module WhatsappSdk
183
170
  headers: DEFAULT_HEADERS
184
171
  )
185
172
 
186
- WhatsappSdk::Api::Response.new(
173
+ Api::Response.new(
187
174
  response: response,
188
- data_class_type: WhatsappSdk::Api::Responses::MessageDataResponse
175
+ data_class_type: Api::Responses::MessageDataResponse
189
176
  )
190
177
  end
191
178
 
@@ -197,18 +184,18 @@ module WhatsappSdk
197
184
  # @param link [String] Image link.
198
185
  # @param caption [String] Image caption.
199
186
  # @param message_id [String] The id of the message to reply to.
200
- # @return [WhatsappSdk::Api::Response] Response object.
187
+ # @return [Api::Response] Response object.
201
188
  sig do
202
189
  params(
203
190
  sender_id: Integer, recipient_number: Integer,
204
191
  video_id: T.nilable(String), link: T.nilable(String), caption: String,
205
192
  message_id: T.nilable(String)
206
- ).returns(WhatsappSdk::Api::Response)
193
+ ).returns(Api::Response)
207
194
  end
208
195
  def send_video(
209
196
  sender_id:, recipient_number:, video_id: nil, link: nil, caption: "", message_id: nil
210
197
  )
211
- raise MissingArgumentError, "video_id or link is required" if !video_id && !link
198
+ raise Resource::Errors::MissingArgumentError, "video_id or link is required" if !video_id && !link
212
199
 
213
200
  params = {
214
201
  messaging_product: "whatsapp",
@@ -229,9 +216,9 @@ module WhatsappSdk
229
216
  headers: DEFAULT_HEADERS
230
217
  )
231
218
 
232
- WhatsappSdk::Api::Response.new(
219
+ Api::Response.new(
233
220
  response: response,
234
- data_class_type: WhatsappSdk::Api::Responses::MessageDataResponse
221
+ data_class_type: Api::Responses::MessageDataResponse
235
222
  )
236
223
  end
237
224
 
@@ -243,18 +230,21 @@ module WhatsappSdk
243
230
  # @param link [String] Image link.
244
231
  # @param caption [String] Image caption.
245
232
  # @param message_id [String] The id of the message to reply to.
246
- # @return [WhatsappSdk::Api::Response] Response object.
233
+ # @return [Api::Response] Response object.
247
234
  sig do
248
235
  params(
249
236
  sender_id: Integer, recipient_number: Integer,
250
237
  document_id: T.nilable(String), link: T.nilable(String), caption: String,
251
238
  message_id: T.nilable(String)
252
- ).returns(WhatsappSdk::Api::Response)
239
+ ).returns(Api::Response)
253
240
  end
254
241
  def send_document(
255
242
  sender_id:, recipient_number:, document_id: nil, link: nil, caption: "", message_id: nil
256
243
  )
257
- raise MissingArgumentError, "document or link is required" if !document_id && !link
244
+ if !document_id && !link
245
+ raise Resource::Errors::MissingArgumentError,
246
+ "document or link is required"
247
+ end
258
248
 
259
249
  params = {
260
250
  messaging_product: "whatsapp",
@@ -275,9 +265,9 @@ module WhatsappSdk
275
265
  headers: DEFAULT_HEADERS
276
266
  )
277
267
 
278
- WhatsappSdk::Api::Response.new(
268
+ Api::Response.new(
279
269
  response: response,
280
- data_class_type: WhatsappSdk::Api::Responses::MessageDataResponse
270
+ data_class_type: Api::Responses::MessageDataResponse
281
271
  )
282
272
  end
283
273
 
@@ -288,15 +278,15 @@ module WhatsappSdk
288
278
  # @param sticker_id [String] The sticker ID.
289
279
  # @param link [String] Image link.
290
280
  # @param message_id [String] The id of the message to reply to.
291
- # @return [WhatsappSdk::Api::Response] Response object.
281
+ # @return [Api::Response] Response object.
292
282
  sig do
293
283
  params(
294
284
  sender_id: Integer, recipient_number: Integer, sticker_id: T.nilable(String),
295
285
  link: T.nilable(String), message_id: T.nilable(String)
296
- ).returns(WhatsappSdk::Api::Response)
286
+ ).returns(Api::Response)
297
287
  end
298
288
  def send_sticker(sender_id:, recipient_number:, sticker_id: nil, link: nil, message_id: nil)
299
- raise MissingArgumentError, "sticker or link is required" if !sticker_id && !link
289
+ raise Resource::Errors::MissingArgumentError, "sticker or link is required" if !sticker_id && !link
300
290
 
301
291
  params = {
302
292
  messaging_product: "whatsapp",
@@ -313,9 +303,9 @@ module WhatsappSdk
313
303
  headers: DEFAULT_HEADERS
314
304
  )
315
305
 
316
- WhatsappSdk::Api::Response.new(
306
+ Api::Response.new(
317
307
  response: response,
318
- data_class_type: WhatsappSdk::Api::Responses::MessageDataResponse
308
+ data_class_type: Api::Responses::MessageDataResponse
319
309
  )
320
310
  end
321
311
 
@@ -327,13 +317,13 @@ module WhatsappSdk
327
317
  # @param contacts [Array<Contact>] Contacts.
328
318
  # @param contacts_json [Json] Contacts.
329
319
  # @param message_id [String] The id of the message to reply to.
330
- # @return [WhatsappSdk::Api::Response] Response object.
320
+ # @return [Api::Response] Response object.
331
321
  sig do
332
322
  params(
333
323
  sender_id: Integer, recipient_number: Integer,
334
- contacts: T.nilable(T::Array[WhatsappSdk::Resource::Contact]),
324
+ contacts: T.nilable(T::Array[Resource::Contact]),
335
325
  contacts_json: T::Hash[T.untyped, T.untyped], message_id: T.nilable(String)
336
- ).returns(WhatsappSdk::Api::Response)
326
+ ).returns(Api::Response)
337
327
  end
338
328
  def send_contacts(
339
329
  sender_id:, recipient_number:, contacts: nil, contacts_json: {}, message_id: nil
@@ -353,9 +343,9 @@ module WhatsappSdk
353
343
  headers: DEFAULT_HEADERS
354
344
  )
355
345
 
356
- WhatsappSdk::Api::Response.new(
346
+ Api::Response.new(
357
347
  response: response,
358
- data_class_type: WhatsappSdk::Api::Responses::MessageDataResponse
348
+ data_class_type: Api::Responses::MessageDataResponse
359
349
  )
360
350
  end
361
351
 
@@ -373,18 +363,21 @@ module WhatsappSdk
373
363
  # @param interactive_json [Json] The interactive object as a Json.
374
364
  # If you pass interactive_json, you can't pass interactive.
375
365
  # @param message_id [String] The id of the message to reply to.
376
- # @return [WhatsappSdk::Api::Response] Response object.
366
+ # @return [Api::Response] Response object.
377
367
  sig do
378
368
  params(
379
369
  sender_id: Integer, recipient_number: Integer,
380
- interactive: T.nilable(WhatsappSdk::Resource::Interactive),
370
+ interactive: T.nilable(Resource::Interactive),
381
371
  interactive_json: T.nilable(T::Hash[T.untyped, T.untyped]), message_id: T.nilable(String)
382
- ).returns(WhatsappSdk::Api::Response)
372
+ ).returns(Api::Response)
383
373
  end
384
374
  def send_interactive_message(
385
375
  sender_id:, recipient_number:, interactive: nil, interactive_json: nil, message_id: nil
386
376
  )
387
- raise MissingArgumentError, "interactive or interactive_json is required" if !interactive && !interactive_json
377
+ if !interactive && !interactive_json
378
+ raise Resource::Errors::MissingArgumentError,
379
+ "interactive or interactive_json is required"
380
+ end
388
381
 
389
382
  params = {
390
383
  messaging_product: "whatsapp",
@@ -406,9 +399,9 @@ module WhatsappSdk
406
399
  headers: DEFAULT_HEADERS
407
400
  )
408
401
 
409
- WhatsappSdk::Api::Response.new(
402
+ Api::Response.new(
410
403
  response: response,
411
- data_class_type: WhatsappSdk::Api::Responses::MessageDataResponse
404
+ data_class_type: Api::Responses::MessageDataResponse
412
405
  )
413
406
  end
414
407
 
@@ -419,8 +412,8 @@ module WhatsappSdk
419
412
  #
420
413
  # @param sender_id [Integer] Sender' phone number.
421
414
  # @param message_id [Integer] Message ID.
422
- # @return [WhatsappSdk::Api::Response] Response object.
423
- sig { params(sender_id: Integer, message_id: String).returns(WhatsappSdk::Api::Response) }
415
+ # @return [Api::Response] Response object.
416
+ sig { params(sender_id: Integer, message_id: String).returns(Api::Response) }
424
417
  def read_message(sender_id:, message_id:)
425
418
  params = {
426
419
  messaging_product: "whatsapp",
@@ -434,9 +427,9 @@ module WhatsappSdk
434
427
  headers: DEFAULT_HEADERS
435
428
  )
436
429
 
437
- WhatsappSdk::Api::Response.new(
430
+ Api::Response.new(
438
431
  response: response,
439
- data_class_type: WhatsappSdk::Api::Responses::ReadMessageDataResponse
432
+ data_class_type: Api::Responses::ReadMessageDataResponse
440
433
  )
441
434
  end
442
435
 
@@ -448,18 +441,21 @@ module WhatsappSdk
448
441
  # @param language [String] template language.
449
442
  # @param components [Component] Component.
450
443
  # @param components_json [Json] The component as a Json. If you pass components_json, you can't pass components.
451
- # @return [WhatsappSdk::Api::Response] Response object.
444
+ # @return [Api::Response] Response object.
452
445
  sig do
453
446
  params(
454
447
  sender_id: Integer, recipient_number: Integer, name: String, language: String,
455
- components: T.nilable(T::Array[WhatsappSdk::Resource::Component]),
448
+ components: T.nilable(T::Array[Resource::Component]),
456
449
  components_json: T.nilable(T::Array[T::Hash[T.untyped, T.untyped]])
457
- ).returns(WhatsappSdk::Api::Response)
450
+ ).returns(Api::Response)
458
451
  end
459
452
  def send_template(
460
453
  sender_id:, recipient_number:, name:, language:, components: nil, components_json: nil
461
454
  )
462
- raise MissingArgumentError, "components or components_json is required" if !components && !components_json
455
+ if !components && !components_json
456
+ raise Resource::Errors::MissingArgumentError,
457
+ "components or components_json is required"
458
+ end
463
459
 
464
460
  params = {
465
461
  messaging_product: "whatsapp",
@@ -484,9 +480,9 @@ module WhatsappSdk
484
480
  headers: DEFAULT_HEADERS
485
481
  )
486
482
 
487
- WhatsappSdk::Api::Response.new(
483
+ Api::Response.new(
488
484
  response: response,
489
- data_class_type: WhatsappSdk::Api::Responses::MessageDataResponse
485
+ data_class_type: Api::Responses::MessageDataResponse
490
486
  )
491
487
  end
492
488
 
@@ -496,12 +492,12 @@ module WhatsappSdk
496
492
  # @param recipient_number [Integer] Recipient' Phone number.
497
493
  # @param message_id [String] the id of the message to reaction.
498
494
  # @param emoji [String] unicode of the emoji to send.
499
- # @return [WhatsappSdk::Api::Response] Response object.
495
+ # @return [Api::Response] Response object.
500
496
  sig do
501
497
  params(
502
498
  sender_id: Integer, recipient_number: Integer, message_id: String,
503
499
  emoji: T.any(String, Integer)
504
- ).returns(WhatsappSdk::Api::Response)
500
+ ).returns(Api::Response)
505
501
  end
506
502
  def send_reaction(sender_id:, recipient_number:, message_id:, emoji:)
507
503
  params = {
@@ -521,9 +517,9 @@ module WhatsappSdk
521
517
  headers: DEFAULT_HEADERS
522
518
  )
523
519
 
524
- WhatsappSdk::Api::Response.new(
520
+ Api::Response.new(
525
521
  response: response,
526
- data_class_type: WhatsappSdk::Api::Responses::MessageDataResponse
522
+ data_class_type: Api::Responses::MessageDataResponse
527
523
  )
528
524
  end
529
525
 
@@ -16,34 +16,34 @@ module WhatsappSdk
16
16
  # Get list of registered numbers.
17
17
  #
18
18
  # @param business_id [Integer] Business Id.
19
- # @return [WhatsappSdk::Api::Response] Response object.
20
- sig { params(business_id: Integer).returns(WhatsappSdk::Api::Response) }
19
+ # @return [Api::Response] Response object.
20
+ sig { params(business_id: Integer).returns(Api::Response) }
21
21
  def registered_numbers(business_id)
22
22
  response = send_request(
23
23
  http_method: "get",
24
24
  endpoint: "#{business_id}/phone_numbers?fields=#{DEFAULT_FIELDS}"
25
25
  )
26
26
 
27
- WhatsappSdk::Api::Response.new(
27
+ Api::Response.new(
28
28
  response: response,
29
- data_class_type: WhatsappSdk::Api::Responses::PhoneNumbersDataResponse
29
+ data_class_type: Api::Responses::PhoneNumbersDataResponse
30
30
  )
31
31
  end
32
32
 
33
33
  # Get the registered number id.
34
34
  #
35
35
  # @param phone_number_id [Integer] The registered number we want to retrieve.
36
- # @return [WhatsappSdk::Api::Response] Response object.
37
- sig { params(phone_number_id: Integer).returns(WhatsappSdk::Api::Response) }
36
+ # @return [Api::Response] Response object.
37
+ sig { params(phone_number_id: Integer).returns(Api::Response) }
38
38
  def registered_number(phone_number_id)
39
39
  response = send_request(
40
40
  http_method: "get",
41
41
  endpoint: "#{phone_number_id}?fields=#{DEFAULT_FIELDS}"
42
42
  )
43
43
 
44
- WhatsappSdk::Api::Response.new(
44
+ Api::Response.new(
45
45
  response: response,
46
- data_class_type: WhatsappSdk::Api::Responses::PhoneNumberDataResponse
46
+ data_class_type: Api::Responses::PhoneNumberDataResponse
47
47
  )
48
48
  end
49
49
 
@@ -51,12 +51,12 @@ module WhatsappSdk
51
51
  #
52
52
  # @param phone_number_id [Integer] The registered number we want to retrieve.
53
53
  # @param pin [Integer] Pin of 6 digits.
54
- # @return [WhatsappSdk::Api::Response] Response object.
54
+ # @return [Api::Response] Response object.
55
55
  sig do
56
56
  params(
57
57
  phone_number_id: Integer,
58
58
  pin: Integer
59
- ).returns(WhatsappSdk::Api::Response)
59
+ ).returns(Api::Response)
60
60
  end
61
61
  def register_number(phone_number_id, pin)
62
62
  response = send_request(
@@ -65,20 +65,20 @@ module WhatsappSdk
65
65
  params: { messaging_product: 'whatsapp', pin: pin }
66
66
  )
67
67
 
68
- WhatsappSdk::Api::Response.new(
68
+ Api::Response.new(
69
69
  response: response,
70
- data_class_type: WhatsappSdk::Api::Responses::PhoneNumberDataResponse
70
+ data_class_type: Api::Responses::PhoneNumberDataResponse
71
71
  )
72
72
  end
73
73
 
74
74
  # Deregister a phone number.
75
75
  #
76
76
  # @param phone_number_id [Integer] The registered number we want to retrieve.
77
- # @return [WhatsappSdk::Api::Response] Response object.
77
+ # @return [Api::Response] Response object.
78
78
  sig do
79
79
  params(
80
80
  phone_number_id: Integer
81
- ).returns(WhatsappSdk::Api::Response)
81
+ ).returns(Api::Response)
82
82
  end
83
83
  def deregister_number(phone_number_id)
84
84
  response = send_request(
@@ -87,9 +87,9 @@ module WhatsappSdk
87
87
  params: {}
88
88
  )
89
89
 
90
- WhatsappSdk::Api::Response.new(
90
+ Api::Response.new(
91
91
  response: response,
92
- data_class_type: WhatsappSdk::Api::Responses::PhoneNumberDataResponse
92
+ data_class_type: Api::Responses::PhoneNumberDataResponse
93
93
  )
94
94
  end
95
95
  end
@@ -13,10 +13,10 @@ module WhatsappSdk
13
13
  class Response
14
14
  extend T::Sig
15
15
 
16
- sig { returns(T.nilable(WhatsappSdk::Api::Responses::ErrorResponse)) }
16
+ sig { returns(T.nilable(Api::Responses::ErrorResponse)) }
17
17
  attr_accessor :error
18
18
 
19
- sig { returns(T.nilable(WhatsappSdk::Api::Responses::DataResponse)) }
19
+ sig { returns(T.nilable(Api::Responses::DataResponse)) }
20
20
  attr_accessor :data
21
21
 
22
22
  sig { returns(T::Hash[T.untyped, T.untyped]) }
@@ -25,8 +25,8 @@ module WhatsappSdk
25
25
  sig do
26
26
  params(
27
27
  response: T::Hash[T.untyped, T.untyped],
28
- data_class_type: T.class_of(WhatsappSdk::Api::Responses::DataResponse),
29
- error_class_type: T.class_of(WhatsappSdk::Api::Responses::ErrorResponse)
28
+ data_class_type: T.class_of(Api::Responses::DataResponse),
29
+ error_class_type: T.class_of(Api::Responses::ErrorResponse)
30
30
  ).void
31
31
  end
32
32
  def initialize(response:, data_class_type:, error_class_type: Responses::MessageErrorResponse)
@@ -0,0 +1,49 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ require_relative "error_response"
5
+
6
+ module WhatsappSdk
7
+ module Api
8
+ module Responses
9
+ class GenericErrorResponse < ErrorResponse
10
+ sig { returns(Integer) }
11
+ attr_reader :code
12
+
13
+ sig { returns(T.nilable(Integer)) }
14
+ attr_reader :subcode
15
+
16
+ sig { returns(T.nilable(String)) }
17
+ attr_reader :message
18
+
19
+ sig { returns(T.nilable(String)) }
20
+ attr_reader :type
21
+
22
+ sig { returns(T.nilable(String)) }
23
+ attr_reader :data
24
+
25
+ sig { returns(T.nilable(String)) }
26
+ attr_reader :fbtrace_id
27
+
28
+ sig { params(response: T::Hash[T.untyped, T.untyped]).void }
29
+ def initialize(response:)
30
+ @code = T.let(response["code"], Integer)
31
+ @subcode = T.let(response["error_subcode"], T.nilable(Integer))
32
+ @message = T.let(response["message"], T.nilable(String))
33
+ @type = T.let(response["type"], T.nilable(String))
34
+ @data = T.let(response["data"], T.nilable(String))
35
+ @fbtrace_id = T.let(response["fbtrace_id"], T.nilable(String))
36
+ super(response: response)
37
+ end
38
+
39
+ sig { override.params(response: T::Hash[T.untyped, T.untyped]).returns(T.nilable(GenericErrorResponse)) }
40
+ def self.build_from_response(response:)
41
+ error_response = response["error"]
42
+ return unless error_response
43
+
44
+ new(response: error_response)
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -12,21 +12,21 @@ module WhatsappSdk
12
12
  class MessageDataResponse < DataResponse
13
13
  extend T::Sig
14
14
 
15
- sig { returns(T::Array[::WhatsappSdk::Resource::ContactResponse]) }
15
+ sig { returns(T::Array[Resource::ContactResponse]) }
16
16
  attr_reader :contacts
17
17
 
18
- sig { returns(T::Array[::WhatsappSdk::Resource::Message]) }
18
+ sig { returns(T::Array[Resource::Message]) }
19
19
  attr_reader :messages
20
20
 
21
21
  sig { params(response: T::Hash[T.untyped, T.untyped]).void }
22
22
  def initialize(response:)
23
23
  @contacts = T.let(
24
24
  response["contacts"]&.map { |contact_json| parse_contact(contact_json) },
25
- T::Array[::WhatsappSdk::Resource::ContactResponse]
25
+ T::Array[Resource::ContactResponse]
26
26
  )
27
27
  @messages = T.let(
28
28
  response["messages"]&.map { |contact_json| parse_message(contact_json) },
29
- T::Array[::WhatsappSdk::Resource::Message]
29
+ T::Array[Resource::Message]
30
30
  )
31
31
  super(response)
32
32
  end
@@ -40,14 +40,14 @@ module WhatsappSdk
40
40
 
41
41
  private
42
42
 
43
- sig { params(message_json: T::Hash[T.untyped, T.untyped]).returns(::WhatsappSdk::Resource::Message) }
43
+ sig { params(message_json: T::Hash[T.untyped, T.untyped]).returns(Resource::Message) }
44
44
  def parse_message(message_json)
45
- ::WhatsappSdk::Resource::Message.new(id: message_json["id"])
45
+ Resource::Message.new(id: message_json["id"])
46
46
  end
47
47
 
48
- sig { params(contact_json: T::Hash[T.untyped, T.untyped]).returns(::WhatsappSdk::Resource::ContactResponse) }
48
+ sig { params(contact_json: T::Hash[T.untyped, T.untyped]).returns(Resource::ContactResponse) }
49
49
  def parse_contact(contact_json)
50
- ::WhatsappSdk::Resource::ContactResponse.new(input: contact_json["input"], wa_id: contact_json["wa_id"])
50
+ Resource::ContactResponse.new(input: contact_json["input"], wa_id: contact_json["wa_id"])
51
51
  end
52
52
  end
53
53
  end
@@ -1,48 +1,12 @@
1
1
  # typed: strict
2
2
  # frozen_string_literal: true
3
3
 
4
- require_relative "error_response"
4
+ require_relative "generic_error_response"
5
5
 
6
6
  module WhatsappSdk
7
7
  module Api
8
8
  module Responses
9
- class MessageErrorResponse < ErrorResponse
10
- sig { returns(Integer) }
11
- attr_reader :code
12
-
13
- sig { returns(T.nilable(Integer)) }
14
- attr_reader :subcode
15
-
16
- sig { returns(T.nilable(String)) }
17
- attr_reader :message
18
-
19
- sig { returns(T.nilable(String)) }
20
- attr_reader :type
21
-
22
- sig { returns(T.nilable(String)) }
23
- attr_reader :data
24
-
25
- sig { returns(T.nilable(String)) }
26
- attr_reader :fbtrace_id
27
-
28
- sig { params(response: T::Hash[T.untyped, T.untyped]).void }
29
- def initialize(response:)
30
- @code = T.let(response["code"], Integer)
31
- @subcode = T.let(response["error_subcode"], T.nilable(Integer))
32
- @message = T.let(response["message"], T.nilable(String))
33
- @type = T.let(response["type"], T.nilable(String))
34
- @data = T.let(response["data"], T.nilable(String))
35
- @fbtrace_id = T.let(response["fbtrace_id"], T.nilable(String))
36
- super(response: response)
37
- end
38
-
39
- sig { override.params(response: T::Hash[T.untyped, T.untyped]).returns(T.nilable(MessageErrorResponse)) }
40
- def self.build_from_response(response:)
41
- error_response = response["error"]
42
- return unless error_response
43
-
44
- new(response: error_response)
45
- end
9
+ class MessageErrorResponse < GenericErrorResponse
46
10
  end
47
11
  end
48
12
  end