whatsapp_sdk 0.3.2 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.rubocop.yml +14 -0
- data/CHANGELOG.md +2 -0
- data/Gemfile +5 -0
- data/Gemfile.lock +44 -0
- data/README.md +102 -28
- data/Rakefile +1 -0
- data/bin/tapioca +30 -0
- data/example.rb +31 -36
- data/lib/version.rb +2 -1
- data/lib/whatsapp_sdk/api/client.rb +14 -3
- data/lib/whatsapp_sdk/api/medias.rb +12 -3
- data/lib/whatsapp_sdk/api/messages.rb +72 -14
- data/lib/whatsapp_sdk/api/phone_numbers.rb +3 -0
- data/lib/whatsapp_sdk/api/request.rb +2 -2
- data/lib/whatsapp_sdk/api/response.rb +17 -1
- data/lib/whatsapp_sdk/api/responses/data_response.rb +10 -3
- data/lib/whatsapp_sdk/api/responses/error_response.rb +8 -1
- data/lib/whatsapp_sdk/api/responses/media_data_response.rb +26 -7
- data/lib/whatsapp_sdk/api/responses/message_data_response.rb +20 -3
- data/lib/whatsapp_sdk/api/responses/message_error_response.rb +26 -7
- data/lib/whatsapp_sdk/api/responses/phone_number_data_response.rb +18 -5
- data/lib/whatsapp_sdk/api/responses/phone_numbers_data_response.rb +9 -1
- data/lib/whatsapp_sdk/api/responses/read_message_data_response.rb +10 -3
- data/lib/whatsapp_sdk/api/responses/success_response.rb +5 -1
- data/lib/whatsapp_sdk/configuration.rb +7 -3
- data/lib/whatsapp_sdk/error.rb +1 -0
- data/lib/whatsapp_sdk/resource/address.rb +30 -6
- data/lib/whatsapp_sdk/resource/address_type.rb +15 -0
- data/lib/whatsapp_sdk/resource/button_parameter.rb +19 -23
- data/lib/whatsapp_sdk/resource/component.rb +45 -13
- data/lib/whatsapp_sdk/resource/contact.rb +30 -1
- data/lib/whatsapp_sdk/resource/contact_response.rb +9 -1
- data/lib/whatsapp_sdk/resource/currency.rb +9 -1
- data/lib/whatsapp_sdk/resource/date_time.rb +7 -1
- data/lib/whatsapp_sdk/resource/email.rb +9 -5
- data/lib/whatsapp_sdk/resource/media.rb +44 -15
- data/lib/whatsapp_sdk/resource/message.rb +5 -0
- data/lib/whatsapp_sdk/resource/name.rb +28 -1
- data/lib/whatsapp_sdk/resource/org.rb +13 -1
- data/lib/whatsapp_sdk/resource/parameter_object.rb +82 -39
- data/lib/whatsapp_sdk/resource/phone_number.rb +12 -5
- data/lib/whatsapp_sdk/resource/url.rb +9 -5
- data/lib/whatsapp_sdk.rb +7 -0
- data/sorbet/config +6 -0
- data/sorbet/rbi/annotations/faraday.rbi +17 -0
- data/sorbet/rbi/annotations/mocha.rbi +34 -0
- data/sorbet/rbi/annotations/rainbow.rbi +269 -0
- data/sorbet/rbi/gems/faraday-multipart@1.0.4.rbi +270 -0
- data/sorbet/rbi/gems/faraday-net_http@2.0.3.rbi +182 -0
- data/sorbet/rbi/gems/faraday@2.3.0.rbi +2494 -0
- data/sorbet/rbi/gems/method_source@1.0.0.rbi +272 -0
- data/sorbet/rbi/gems/minitest@5.16.1.rbi +1459 -0
- data/sorbet/rbi/gems/mocha@1.14.0.rbi +60 -0
- data/sorbet/rbi/gems/multipart-post@2.2.3.rbi +239 -0
- data/sorbet/rbi/gems/netrc@0.11.0.rbi +150 -0
- data/sorbet/rbi/gems/oj@3.13.14.rbi +589 -0
- data/sorbet/rbi/gems/zeitwerk@2.6.0.rbi +867 -0
- data/sorbet/rbi/todo.rbi +8 -0
- data/sorbet/shims/request.rbi +10 -0
- data/sorbet/tapioca/config.yml +13 -0
- data/sorbet/tapioca/require.rb +5 -0
- data/whatsapp_sdk.gemspec +4 -1
- metadata +50 -2
@@ -1,4 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
# typed: strict
|
2
3
|
|
3
4
|
require_relative "data_response"
|
4
5
|
|
@@ -6,17 +7,20 @@ module WhatsappSdk
|
|
6
7
|
module Api
|
7
8
|
module Responses
|
8
9
|
class SuccessResponse < DataResponse
|
10
|
+
sig { params(response: T::Hash[T.untyped, T.untyped]).void }
|
9
11
|
def initialize(response:)
|
10
|
-
@success = response["success"]
|
12
|
+
@success = T.let(response["success"], T::Boolean)
|
11
13
|
super(response)
|
12
14
|
end
|
13
15
|
|
16
|
+
sig { override.params(response: T::Hash[T.untyped, T.untyped]).returns(T.nilable(SuccessResponse)) }
|
14
17
|
def self.build_from_response(response:)
|
15
18
|
return unless response["success"]
|
16
19
|
|
17
20
|
new(response: response)
|
18
21
|
end
|
19
22
|
|
23
|
+
sig { returns(T::Boolean) }
|
20
24
|
def success?
|
21
25
|
@success
|
22
26
|
end
|
@@ -1,4 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
# typed: strict
|
2
3
|
|
3
4
|
module WhatsappSdk
|
4
5
|
# This module allows client instantiating the client as a singleton like the following example:
|
@@ -8,15 +9,18 @@ module WhatsappSdk
|
|
8
9
|
#
|
9
10
|
# The gem have access to the client through WhatsappSdk.configuration.client
|
10
11
|
class Configuration
|
12
|
+
extend T::Sig
|
13
|
+
|
14
|
+
sig { returns(String) }
|
11
15
|
attr_accessor :access_token
|
12
16
|
|
13
|
-
|
17
|
+
sig { params(access_token: String).void }
|
18
|
+
def initialize(access_token = "")
|
14
19
|
@access_token = access_token
|
15
20
|
end
|
16
21
|
|
22
|
+
sig { returns(WhatsappSdk::Api::Client) }
|
17
23
|
def client
|
18
|
-
return unless access_token
|
19
|
-
|
20
24
|
WhatsappSdk::Api::Client.new(access_token)
|
21
25
|
end
|
22
26
|
end
|
data/lib/whatsapp_sdk/error.rb
CHANGED
@@ -1,16 +1,39 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
# typed: strict
|
2
3
|
|
3
4
|
module WhatsappSdk
|
4
5
|
module Resource
|
5
6
|
class Address
|
6
|
-
|
7
|
+
extend T::Sig
|
7
8
|
|
8
|
-
|
9
|
-
|
10
|
-
work: "WORK"
|
11
|
-
}.freeze
|
9
|
+
sig { returns(String) }
|
10
|
+
attr_accessor :street
|
12
11
|
|
13
|
-
|
12
|
+
sig { returns(String) }
|
13
|
+
attr_accessor :city
|
14
|
+
|
15
|
+
sig { returns(String) }
|
16
|
+
attr_accessor :state
|
17
|
+
|
18
|
+
sig { returns(String) }
|
19
|
+
attr_accessor :zip
|
20
|
+
|
21
|
+
sig { returns(String) }
|
22
|
+
attr_accessor :country
|
23
|
+
|
24
|
+
sig { returns(String) }
|
25
|
+
attr_accessor :country_code
|
26
|
+
|
27
|
+
sig { returns(AddressType) }
|
28
|
+
attr_accessor :type
|
29
|
+
|
30
|
+
sig do
|
31
|
+
params(
|
32
|
+
street: String, city: String, state: String, zip: String,
|
33
|
+
country: String, country_code: String, type: AddressType
|
34
|
+
).void
|
35
|
+
end
|
36
|
+
def initialize(street:, city:, state:, zip:, country:, country_code:, type: AddressType::Home)
|
14
37
|
@street = street
|
15
38
|
@city = city
|
16
39
|
@state = state
|
@@ -20,6 +43,7 @@ module WhatsappSdk
|
|
20
43
|
@type = type
|
21
44
|
end
|
22
45
|
|
46
|
+
sig { returns(T::Hash[T.untyped, T.untyped]) }
|
23
47
|
def to_h
|
24
48
|
{
|
25
49
|
street: @street,
|
@@ -1,27 +1,24 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
# typed: strict
|
2
3
|
|
3
4
|
module WhatsappSdk
|
4
5
|
module Resource
|
5
6
|
class ButtonParameter
|
6
|
-
|
7
|
-
attr_accessor :message
|
8
|
-
|
9
|
-
def initialize(type)
|
10
|
-
@message = "invalid type #{type}. type should be text or payload"
|
11
|
-
super
|
12
|
-
end
|
13
|
-
end
|
7
|
+
extend T::Sig
|
14
8
|
|
15
9
|
# Returns the button parameter type.
|
16
10
|
#
|
17
11
|
# @returns type [String] Valid options are payload and text.
|
12
|
+
sig { returns(Type) }
|
18
13
|
attr_accessor :type
|
19
14
|
|
20
|
-
|
21
|
-
|
22
|
-
PAYLOAD = "payload"
|
15
|
+
class Type < T::Enum
|
16
|
+
extend T::Sig
|
23
17
|
|
24
|
-
|
18
|
+
enums do
|
19
|
+
Text = new("text")
|
20
|
+
Payload = new("payload")
|
21
|
+
end
|
25
22
|
end
|
26
23
|
|
27
24
|
# Required for quick_reply buttons.
|
@@ -29,37 +26,36 @@ module WhatsappSdk
|
|
29
26
|
# in addition to the display text on the button.
|
30
27
|
#
|
31
28
|
# @returns payload [String]
|
29
|
+
sig { returns(T.nilable(String)) }
|
32
30
|
attr_accessor :payload
|
33
31
|
|
34
32
|
# Required for URL buttons.
|
35
33
|
# Developer-provided suffix that is appended to the predefined prefix URL in the template.
|
36
34
|
#
|
37
35
|
# @returns text [String]
|
36
|
+
sig { returns(T.nilable(String)) }
|
38
37
|
attr_accessor :text
|
39
38
|
|
39
|
+
sig do
|
40
|
+
params(
|
41
|
+
type: Type, payload: T.nilable(String), text: T.nilable(String)
|
42
|
+
).void
|
43
|
+
end
|
40
44
|
def initialize(type:, payload: nil, text: nil)
|
41
45
|
@type = type
|
42
46
|
@payload = payload
|
43
47
|
@text = text
|
44
|
-
validate
|
45
48
|
end
|
46
49
|
|
47
|
-
|
50
|
+
sig { returns(T::Hash[T.untyped, T.untyped]) }
|
51
|
+
def to_json
|
48
52
|
json = {
|
49
|
-
type: type
|
53
|
+
type: type.serialize
|
50
54
|
}
|
51
55
|
json[:payload] = payload if payload
|
52
56
|
json[:text] = text if text
|
53
57
|
json
|
54
58
|
end
|
55
|
-
|
56
|
-
private
|
57
|
-
|
58
|
-
def validate
|
59
|
-
return if Type::VALID_TYPES.include?(type)
|
60
|
-
|
61
|
-
raise InvalidType, type
|
62
|
-
end
|
63
59
|
end
|
64
60
|
end
|
65
61
|
end
|
@@ -1,11 +1,21 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
# typed: strict
|
2
3
|
|
3
4
|
module WhatsappSdk
|
4
5
|
module Resource
|
5
6
|
class Component
|
7
|
+
extend T::Sig
|
8
|
+
|
6
9
|
class InvalidField < StandardError
|
7
|
-
|
10
|
+
extend T::Sig
|
11
|
+
|
12
|
+
sig { returns(Symbol) }
|
13
|
+
attr_reader :field
|
14
|
+
|
15
|
+
sig { returns(String) }
|
16
|
+
attr_reader :message
|
8
17
|
|
18
|
+
sig { params(field: Symbol, message: String).void }
|
9
19
|
def initialize(field, message)
|
10
20
|
@field = field
|
11
21
|
@message = message
|
@@ -13,25 +23,35 @@ module WhatsappSdk
|
|
13
23
|
end
|
14
24
|
end
|
15
25
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
26
|
+
class Type < T::Enum
|
27
|
+
extend T::Sig
|
28
|
+
|
29
|
+
enums do
|
30
|
+
Header = new("header")
|
31
|
+
Body = new("body")
|
32
|
+
Button = new("button")
|
33
|
+
end
|
20
34
|
end
|
21
35
|
|
22
|
-
|
23
|
-
|
24
|
-
|
36
|
+
class Subtype < T::Enum
|
37
|
+
extend T::Sig
|
38
|
+
|
39
|
+
enums do
|
40
|
+
QuickReply = new("quick_reply")
|
41
|
+
Url = new("url")
|
42
|
+
end
|
25
43
|
end
|
26
44
|
|
27
45
|
# Returns the Component type.
|
28
46
|
#
|
29
47
|
# @returns type [String]. Supported Options are header, body and button.
|
48
|
+
sig { returns(Type) }
|
30
49
|
attr_accessor :type
|
31
50
|
|
32
51
|
# Returns the parameters of the component. For button type, it's required.
|
33
52
|
#
|
34
53
|
# @returns parameter [Array<ButtonParameter, ParameterObject>] .
|
54
|
+
sig { returns(T::Array[T.any(ButtonParameter, ParameterObject)]) }
|
35
55
|
attr_accessor :parameters
|
36
56
|
|
37
57
|
# Returns the Type of button to create. Required when type=button. Not used for the other types.
|
@@ -42,40 +62,52 @@ module WhatsappSdk
|
|
42
62
|
# appending the text parameter to the predefined prefix URL in the template.
|
43
63
|
#
|
44
64
|
# @returns subtype [String]. Valid options are quick_reply and url.
|
65
|
+
sig { returns(T.nilable(WhatsappSdk::Resource::Component::Subtype)) }
|
45
66
|
attr_accessor :sub_type
|
46
67
|
|
47
68
|
# Required when type=button. Not used for the other types.
|
48
69
|
# Position index of the button. You can have up to 3 buttons using index values of 0 to 2.
|
49
70
|
#
|
50
71
|
# @returns index [Integer].
|
72
|
+
sig { returns(T.nilable(Integer)) }
|
51
73
|
attr_accessor :index
|
52
74
|
|
75
|
+
sig { params(parameter: T.any(ButtonParameter, ParameterObject)).void }
|
53
76
|
def add_parameter(parameter)
|
54
77
|
@parameters << parameter
|
55
78
|
end
|
56
79
|
|
80
|
+
sig do
|
81
|
+
params(
|
82
|
+
type: Type, parameters: T::Array[T.any(ButtonParameter, ParameterObject)],
|
83
|
+
sub_type: T.nilable(WhatsappSdk::Resource::Component::Subtype), index: T.nilable(Integer)
|
84
|
+
).void
|
85
|
+
end
|
57
86
|
def initialize(type:, parameters: [], sub_type: nil, index: nil)
|
58
87
|
@parameters = parameters
|
59
88
|
@type = type
|
60
89
|
@sub_type = sub_type
|
61
|
-
@index = index.nil? && type == Type::
|
90
|
+
@index = index.nil? && type == Type::Button ? 0 : index
|
62
91
|
validate_fields
|
63
92
|
end
|
64
93
|
|
65
|
-
|
94
|
+
sig { returns(T::Hash[T.untyped, T.untyped]) }
|
95
|
+
def to_json
|
66
96
|
json = {
|
67
|
-
type: type,
|
97
|
+
type: type.serialize,
|
68
98
|
parameters: parameters.map(&:to_json)
|
69
99
|
}
|
70
|
-
json[:sub_type] = sub_type if sub_type
|
100
|
+
json[:sub_type] = sub_type&.serialize if sub_type
|
71
101
|
json[:index] = index if index
|
72
102
|
json
|
73
103
|
end
|
74
104
|
|
75
105
|
private
|
76
106
|
|
107
|
+
sig { void }
|
77
108
|
def validate_fields
|
78
|
-
return if type == Type::
|
109
|
+
return if type == Type::Button
|
110
|
+
|
79
111
|
raise InvalidField.new(:sub_type, 'sub_type is not required when type is not button') if sub_type
|
80
112
|
|
81
113
|
raise InvalidField.new(:index, 'index is not required when type is not button') if index
|
@@ -1,10 +1,38 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
# typed: strict
|
2
3
|
|
3
4
|
module WhatsappSdk
|
4
5
|
module Resource
|
5
6
|
class Contact
|
6
|
-
|
7
|
+
extend T::Sig
|
7
8
|
|
9
|
+
sig { returns(T::Array[Address]) }
|
10
|
+
attr_accessor :addresses
|
11
|
+
|
12
|
+
sig { returns(String) }
|
13
|
+
attr_accessor :birthday
|
14
|
+
|
15
|
+
sig { returns(T::Array[Email]) }
|
16
|
+
attr_accessor :emails
|
17
|
+
|
18
|
+
sig { returns(Name) }
|
19
|
+
attr_accessor :name
|
20
|
+
|
21
|
+
sig { returns(Org) }
|
22
|
+
attr_accessor :org
|
23
|
+
|
24
|
+
sig { returns(T::Array[PhoneNumber]) }
|
25
|
+
attr_accessor :phones
|
26
|
+
|
27
|
+
sig { returns(T::Array[Url]) }
|
28
|
+
attr_accessor :urls
|
29
|
+
|
30
|
+
sig do
|
31
|
+
params(
|
32
|
+
addresses: T::Array[Address], birthday: String, emails: T::Array[Email],
|
33
|
+
name: Name, org: Org, phones: T::Array[PhoneNumber], urls: T::Array[Url]
|
34
|
+
).void
|
35
|
+
end
|
8
36
|
def initialize(addresses:, birthday:, emails:, name:, org:, phones:, urls:)
|
9
37
|
@addresses = addresses
|
10
38
|
@birthday = birthday
|
@@ -15,6 +43,7 @@ module WhatsappSdk
|
|
15
43
|
@urls = urls
|
16
44
|
end
|
17
45
|
|
46
|
+
sig { returns(T::Hash[T.untyped, T.untyped]) }
|
18
47
|
def to_h
|
19
48
|
{
|
20
49
|
addresses: addresses.map(&:to_h),
|
@@ -1,10 +1,18 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
# typed: strict
|
2
3
|
|
3
4
|
module WhatsappSdk
|
4
5
|
module Resource
|
5
6
|
class ContactResponse
|
6
|
-
|
7
|
+
extend T::Sig
|
7
8
|
|
9
|
+
sig { returns(String) }
|
10
|
+
attr_accessor :wa_id
|
11
|
+
|
12
|
+
sig { returns(String) }
|
13
|
+
attr_accessor :input
|
14
|
+
|
15
|
+
sig { params(input: String, wa_id: String).void }
|
8
16
|
def initialize(input:, wa_id:)
|
9
17
|
@input = input
|
10
18
|
@wa_id = wa_id
|
@@ -1,30 +1,38 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
# typed: strict
|
2
3
|
|
3
4
|
module WhatsappSdk
|
4
5
|
module Resource
|
5
6
|
class Currency
|
7
|
+
extend T::Sig
|
8
|
+
|
6
9
|
# Returns default text if localization fails.
|
7
10
|
#
|
8
11
|
# @returns fallback_value [String].
|
12
|
+
sig { returns(String) }
|
9
13
|
attr_accessor :fallback_value
|
10
14
|
|
11
15
|
# Currency code as defined in ISO 4217.
|
12
16
|
#
|
13
17
|
# @returns code [String].
|
18
|
+
sig { returns(String) }
|
14
19
|
attr_accessor :code
|
15
20
|
|
16
21
|
# Amount multiplied by 1000.
|
17
22
|
#
|
18
23
|
# @returns code [Float].
|
24
|
+
sig { returns(T.any(Float, Integer)) }
|
19
25
|
attr_accessor :amount
|
20
26
|
|
27
|
+
sig { params(fallback_value: String, code: String, amount: T.any(Float, Integer)).void }
|
21
28
|
def initialize(fallback_value:, code:, amount:)
|
22
29
|
@fallback_value = fallback_value
|
23
30
|
@code = code
|
24
31
|
@amount = amount
|
25
32
|
end
|
26
33
|
|
27
|
-
|
34
|
+
sig { returns(T::Hash[T.untyped, T.untyped]) }
|
35
|
+
def to_json
|
28
36
|
{
|
29
37
|
fallback_value: fallback_value,
|
30
38
|
code: code,
|
@@ -1,18 +1,24 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
# typed: strict
|
2
3
|
|
3
4
|
module WhatsappSdk
|
4
5
|
module Resource
|
5
6
|
class DateTime
|
7
|
+
extend T::Sig
|
8
|
+
|
6
9
|
# Returns default text if localization fails.
|
7
10
|
#
|
8
11
|
# @returns fallback_value [String].
|
12
|
+
sig { returns(String) }
|
9
13
|
attr_accessor :fallback_value
|
10
14
|
|
15
|
+
sig { params(fallback_value: String).void }
|
11
16
|
def initialize(fallback_value:)
|
12
17
|
@fallback_value = fallback_value
|
13
18
|
end
|
14
19
|
|
15
|
-
|
20
|
+
sig { returns(T::Hash[T.untyped, T.untyped]) }
|
21
|
+
def to_json
|
16
22
|
{
|
17
23
|
fallback_value: fallback_value
|
18
24
|
}
|
@@ -1,20 +1,24 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
# typed: strict
|
2
3
|
|
3
4
|
module WhatsappSdk
|
4
5
|
module Resource
|
5
6
|
class Email
|
6
|
-
|
7
|
+
extend T::Sig
|
7
8
|
|
8
|
-
|
9
|
-
|
10
|
-
work: "WORK"
|
11
|
-
}.freeze
|
9
|
+
sig { returns(String) }
|
10
|
+
attr_accessor :email
|
12
11
|
|
12
|
+
sig { returns(AddressType) }
|
13
|
+
attr_accessor :type
|
14
|
+
|
15
|
+
sig { params(email: String, type: AddressType).void }
|
13
16
|
def initialize(email:, type:)
|
14
17
|
@email = email
|
15
18
|
@type = type
|
16
19
|
end
|
17
20
|
|
21
|
+
sig { returns(T::Hash[T.untyped, T.untyped]) }
|
18
22
|
def to_h
|
19
23
|
{
|
20
24
|
email: @email,
|
@@ -1,11 +1,21 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
# typed: strict
|
2
3
|
|
3
4
|
module WhatsappSdk
|
4
5
|
module Resource
|
5
6
|
class Media
|
7
|
+
extend T::Sig
|
8
|
+
|
6
9
|
class InvalidMedia < StandardError
|
7
|
-
|
10
|
+
extend T::Sig
|
11
|
+
|
12
|
+
sig { returns(Symbol) }
|
13
|
+
attr_reader :field
|
14
|
+
|
15
|
+
sig { returns(String) }
|
16
|
+
attr_reader :message
|
8
17
|
|
18
|
+
sig { params(field: Symbol, message: String).void }
|
9
19
|
def initialize(field, message)
|
10
20
|
@field = field
|
11
21
|
@message = message
|
@@ -16,39 +26,52 @@ module WhatsappSdk
|
|
16
26
|
# Returns media id.
|
17
27
|
#
|
18
28
|
# @returns id [String].
|
29
|
+
sig { returns(T.nilable(String)) }
|
19
30
|
attr_accessor :id
|
20
31
|
|
21
|
-
|
22
|
-
|
23
|
-
DOCUMENT = 'document'
|
24
|
-
IMAGE = 'image'
|
25
|
-
VIDEO = 'video'
|
26
|
-
STICKER = 'sticker'
|
32
|
+
class Type < T::Enum
|
33
|
+
extend T::Sig
|
27
34
|
|
28
|
-
|
35
|
+
enums do
|
36
|
+
Audio = new('audio')
|
37
|
+
Document = new('document')
|
38
|
+
Image = new('image')
|
39
|
+
Video = new('video')
|
40
|
+
Sticker = new('sticker')
|
41
|
+
end
|
29
42
|
end
|
30
43
|
|
31
44
|
# @returns type [String]. Valid options ar audio, document, image, video and sticker.
|
45
|
+
sig { returns(Type) }
|
32
46
|
attr_accessor :type
|
33
47
|
|
34
48
|
# The protocol and URL of the media to be sent. Use only with HTTP/HTTPS URLs.
|
35
49
|
# Do not use this field when the message type is set to text.
|
36
50
|
#
|
37
51
|
# @returns link [String].
|
52
|
+
sig { returns(T.nilable(String)) }
|
38
53
|
attr_accessor :link
|
39
54
|
|
40
55
|
# Describes the specified document or image media.
|
41
56
|
#
|
42
57
|
# @returns caption [String].
|
58
|
+
sig { returns(T.nilable(String)) }
|
43
59
|
attr_accessor :caption
|
44
60
|
|
45
61
|
# Describes the filename for the specific document. Use only with document media.
|
46
62
|
#
|
47
63
|
# @returns filename [String].
|
64
|
+
sig { returns(T.nilable(String)) }
|
48
65
|
attr_accessor :filename
|
49
66
|
|
67
|
+
sig do
|
68
|
+
params(
|
69
|
+
type: T.any(Type, String), id: T.nilable(String), link: T.nilable(String),
|
70
|
+
caption: T.nilable(String), filename: T.nilable(String)
|
71
|
+
).void
|
72
|
+
end
|
50
73
|
def initialize(type:, id: nil, link: nil, caption: nil, filename: nil)
|
51
|
-
@type = type
|
74
|
+
@type = T.let(deserialize_type(type), Type)
|
52
75
|
@id = id
|
53
76
|
@link = link
|
54
77
|
@caption = caption
|
@@ -56,7 +79,8 @@ module WhatsappSdk
|
|
56
79
|
validate_media
|
57
80
|
end
|
58
81
|
|
59
|
-
|
82
|
+
sig { returns(T::Hash[T.untyped, T.untyped]) }
|
83
|
+
def to_json
|
60
84
|
json = {}
|
61
85
|
json[:id] = id unless id.nil?
|
62
86
|
json[:link] = link unless link.nil?
|
@@ -67,15 +91,20 @@ module WhatsappSdk
|
|
67
91
|
|
68
92
|
private
|
69
93
|
|
94
|
+
sig { params(type: T.any(String, Type)).returns(Type) }
|
95
|
+
def deserialize_type(type)
|
96
|
+
return type if type.is_a?(Type)
|
97
|
+
|
98
|
+
Type.deserialize(type)
|
99
|
+
end
|
100
|
+
|
101
|
+
sig { returns(T::Boolean) }
|
70
102
|
def validate_media
|
71
|
-
|
72
|
-
raise InvalidMedia.new(:type, "invalid type. type should be audio, document, image, video or sticker")
|
73
|
-
end
|
74
|
-
if filename && (type != Type::DOCUMENT)
|
103
|
+
if filename && (type != Type::Document)
|
75
104
|
raise InvalidMedia.new(:filename, "filename can only be used with document")
|
76
105
|
end
|
77
106
|
|
78
|
-
if caption && !(type == Type::
|
107
|
+
if caption && !(type == Type::Document || type == Type::Image)
|
79
108
|
raise InvalidMedia.new(:caption, "caption can only be used with document or image")
|
80
109
|
end
|
81
110
|
|
@@ -1,10 +1,36 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
# typed: strict
|
2
3
|
|
3
4
|
module WhatsappSdk
|
4
5
|
module Resource
|
5
6
|
class Name
|
6
|
-
|
7
|
+
extend T::Sig
|
7
8
|
|
9
|
+
sig { returns(T.nilable(String)) }
|
10
|
+
attr_accessor :formatted_name
|
11
|
+
|
12
|
+
sig { returns(T.nilable(String)) }
|
13
|
+
attr_accessor :first_name
|
14
|
+
|
15
|
+
sig { returns(T.nilable(String)) }
|
16
|
+
attr_accessor :last_name
|
17
|
+
|
18
|
+
sig { returns(T.nilable(String)) }
|
19
|
+
attr_accessor :middle_name
|
20
|
+
|
21
|
+
sig { returns(T.nilable(String)) }
|
22
|
+
attr_accessor :suffix
|
23
|
+
|
24
|
+
sig { returns(T.nilable(String)) }
|
25
|
+
attr_accessor :prefix
|
26
|
+
|
27
|
+
sig do
|
28
|
+
params(
|
29
|
+
formatted_name: T.nilable(String), first_name: T.nilable(String),
|
30
|
+
last_name: T.nilable(String), middle_name: T.nilable(String),
|
31
|
+
suffix: T.nilable(String), prefix: T.nilable(String)
|
32
|
+
).void
|
33
|
+
end
|
8
34
|
def initialize(
|
9
35
|
formatted_name: nil, first_name: nil,
|
10
36
|
last_name: nil, middle_name: nil, suffix: nil, prefix: nil
|
@@ -17,6 +43,7 @@ module WhatsappSdk
|
|
17
43
|
@prefix = prefix
|
18
44
|
end
|
19
45
|
|
46
|
+
sig { returns(T::Hash[T.untyped, T.untyped]) }
|
20
47
|
def to_h
|
21
48
|
{
|
22
49
|
formatted_name: @formatted_name,
|