whatsapp_sdk 0.0.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,85 @@
1
+ # frozen_string_literal: true
2
+
3
+ module WhatsappSdk
4
+ module Resource
5
+ class Component
6
+ class InvalidField < StandardError
7
+ attr_reader :field, :message
8
+
9
+ def initialize(field, message)
10
+ @field = field
11
+ @message = message
12
+ super(message)
13
+ end
14
+ end
15
+
16
+ module Type
17
+ HEADER = 'header'
18
+ BODY = 'body'
19
+ BUTTON = 'button'
20
+ end
21
+
22
+ module Subtype
23
+ QUICK_REPLY = "quick_reply"
24
+ URL = "url"
25
+ end
26
+
27
+ # Returns the Component type.
28
+ #
29
+ # @returns type [String]. Supported Options are header, body and button.
30
+ attr_accessor :type
31
+
32
+ # Returns the parameters of the component. For button type, it's required.
33
+ #
34
+ # @returns parameter [Array<ButtonParameter, ParameterObject>] .
35
+ attr_accessor :parameters
36
+
37
+ # Returns the Type of button to create. Required when type=button. Not used for the other types.
38
+ # Supported Options
39
+ # quick_reply: Refers to a previously created quick reply button
40
+ # that allows for the customer to return a predefined message.
41
+ # url: Refers to a previously created button that allows the customer to visit the URL generated by
42
+ # appending the text parameter to the predefined prefix URL in the template.
43
+ #
44
+ # @returns subtype [String]. Valid options are quick_reply and url.
45
+ attr_accessor :sub_type
46
+
47
+ # Required when type=button. Not used for the other types.
48
+ # Position index of the button. You can have up to 3 buttons using index values of 0 to 2.
49
+ #
50
+ # @returns index [Integer].
51
+ attr_accessor :index
52
+
53
+ def add_parameter(parameter)
54
+ @parameters << parameter
55
+ end
56
+
57
+ def initialize(type:, parameters: [], sub_type: nil, index: nil)
58
+ @parameters = parameters
59
+ @type = type
60
+ @sub_type = sub_type
61
+ @index = index.nil? && type == Type::BUTTON ? 0 : index
62
+ validate_fields
63
+ end
64
+
65
+ def to_json(*_args)
66
+ json = {
67
+ type: type,
68
+ parameters: parameters.map(&:to_json)
69
+ }
70
+ json[:sub_type] = sub_type if sub_type
71
+ json[:index] = index if index
72
+ json
73
+ end
74
+
75
+ private
76
+
77
+ def validate_fields
78
+ return if type == Type::BUTTON
79
+ raise InvalidField.new(:sub_type, 'sub_type is not required when type is not button') if sub_type
80
+
81
+ raise InvalidField.new(:index, 'index is not required when type is not button') if index
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module WhatsappSdk
4
+ module Resource
5
+ class Currency
6
+ # Returns default text if localization fails.
7
+ #
8
+ # @returns fallback_value [String].
9
+ attr_accessor :fallback_value
10
+
11
+ # Currency code as defined in ISO 4217.
12
+ #
13
+ # @returns code [String].
14
+ attr_accessor :code
15
+
16
+ # Amount multiplied by 1000.
17
+ #
18
+ # @returns code [Float].
19
+ attr_accessor :amount
20
+
21
+ def initialize(fallback_value:, code:, amount:)
22
+ @fallback_value = fallback_value
23
+ @code = code
24
+ @amount = amount
25
+ end
26
+
27
+ def to_json(*_args)
28
+ {
29
+ fallback_value: fallback_value,
30
+ code: code,
31
+ amount_1000: amount
32
+ }
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module WhatsappSdk
4
+ module Resource
5
+ class DateTime
6
+ # Returns default text if localization fails.
7
+ #
8
+ # @returns fallback_value [String].
9
+ attr_accessor :fallback_value
10
+
11
+ def initialize(fallback_value:)
12
+ @fallback_value = fallback_value
13
+ end
14
+
15
+ def to_json(*_args)
16
+ {
17
+ fallback_value: fallback_value
18
+ }
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,86 @@
1
+ # frozen_string_literal: true
2
+
3
+ module WhatsappSdk
4
+ module Resource
5
+ class Media
6
+ class InvalidMedia < StandardError
7
+ attr_reader :field, :message
8
+
9
+ def initialize(field, message)
10
+ @field = field
11
+ @message = message
12
+ super(message)
13
+ end
14
+ end
15
+
16
+ # Returns media id.
17
+ #
18
+ # @returns id [String].
19
+ attr_accessor :id
20
+
21
+ module Type
22
+ AUDIO = 'audio'
23
+ DOCUMENT = 'document'
24
+ IMAGE = 'image'
25
+ VIDEO = 'video'
26
+ STICKER = 'sticker'
27
+
28
+ VALID_TYPES = [AUDIO, DOCUMENT, IMAGE, VIDEO, STICKER].freeze
29
+ end
30
+
31
+ # @returns type [String]. Valid options ar audio, document, image, video and sticker.
32
+ attr_accessor :type
33
+
34
+ # The protocol and URL of the media to be sent. Use only with HTTP/HTTPS URLs.
35
+ # Do not use this field when the message type is set to text.
36
+ #
37
+ # @returns link [String].
38
+ attr_accessor :link
39
+
40
+ # Describes the specified document or image media.
41
+ #
42
+ # @returns caption [String].
43
+ attr_accessor :caption
44
+
45
+ # Describes the filename for the specific document. Use only with document media.
46
+ #
47
+ # @returns filename [String].
48
+ attr_accessor :filename
49
+
50
+ def initialize(type:, id: nil, link: nil, caption: nil, filename: nil)
51
+ @type = type
52
+ @id = id
53
+ @link = link
54
+ @caption = caption
55
+ @filename = filename
56
+ validate_media
57
+ end
58
+
59
+ def to_json(*_args)
60
+ json = {}
61
+ json[:id] = id unless id.nil?
62
+ json[:link] = link unless link.nil?
63
+ json[:caption] = caption unless caption.nil?
64
+ json[:filename] = filename unless filename.nil?
65
+ json
66
+ end
67
+
68
+ private
69
+
70
+ def validate_media
71
+ unless Type::VALID_TYPES.include?(type)
72
+ raise InvalidMedia.new(:type, "invalid type. type should be audio, document, image, video or sticker")
73
+ end
74
+ if filename && (type != Type::DOCUMENT)
75
+ raise InvalidMedia.new(:filename, "filename can only be used with document")
76
+ end
77
+
78
+ if caption && !(type == Type::DOCUMENT || type == Type::IMAGE)
79
+ raise InvalidMedia.new(:caption, "caption can only be used with document or image")
80
+ end
81
+
82
+ true
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,134 @@
1
+ # frozen_string_literal: true
2
+
3
+ module WhatsappSdk
4
+ module Resource
5
+ class ParameterObject
6
+ class InvalidType < StandardError
7
+ attr_accessor :message
8
+
9
+ def initialize(type)
10
+ @message = "invalid type #{type}. type should be text, currency, date_time, image, document or video"
11
+ super
12
+ end
13
+ end
14
+
15
+ class MissingValue < StandardError
16
+ attr_reader :field, :message
17
+
18
+ def initialize(field, message)
19
+ @field = field
20
+ @message = message
21
+ super(message)
22
+ end
23
+ end
24
+
25
+ # Returns the parameter type.
26
+ #
27
+ # @returns type [String] Valid options are text, currency, date_time, image, document, video.
28
+ attr_accessor :type
29
+
30
+ module Type
31
+ TEXT = "text"
32
+ CURRENCY = "currency"
33
+ DATE_TIME = "date_time"
34
+ IMAGE = "image"
35
+ DOCUMENT = "document"
36
+ VIDEO = "video"
37
+
38
+ VALID_TYPES = [TEXT, CURRENCY, DATE_TIME, IMAGE, DOCUMENT, VIDEO].freeze
39
+ end
40
+
41
+ # Returns Text string if the parameter object type is text.
42
+ # For the header component, the character limit is 60 characters.
43
+ # For the body component, the character limit is 1024 characters.
44
+ #
45
+ # @returns text [String]
46
+ attr_accessor :text
47
+
48
+ # Returns Currency if the parameter object type is currency.
49
+ #
50
+ # @returns currency [Currency]
51
+ attr_accessor :currency
52
+
53
+ # Returns date_time if the parameter object type is date_time.
54
+ #
55
+ # @returns date_time [DateTime]
56
+ attr_accessor :date_time
57
+
58
+ # Returns image if the parameter object type is image.
59
+ #
60
+ # @returns image [Media]
61
+ attr_accessor :image
62
+
63
+ # Returns document if the parameter object type is document.
64
+ #
65
+ # @returns document [Media]
66
+ attr_accessor :document
67
+
68
+ # Returns video if the parameter object type is video.
69
+ #
70
+ # @returns video [Media]
71
+ attr_accessor :video
72
+
73
+ def initialize(type:, text: nil, currency: nil, date_time: nil, image: nil, document: nil, video: nil)
74
+ @type = type
75
+ @text = text
76
+ @currency = currency
77
+ @date_time = date_time
78
+ @image = image
79
+ @document = document
80
+ @video = video
81
+ validate
82
+ end
83
+
84
+ def to_json(*_args)
85
+ json = { type: type }
86
+ json[type.to_sym] = case type
87
+ when "text"
88
+ text
89
+ when "currency"
90
+ currency.to_json
91
+ when "date_time"
92
+ date_time.to_json
93
+ when "image"
94
+ image.to_json
95
+ when "document"
96
+ document.to_json
97
+ when "video"
98
+ video.to_json
99
+ else
100
+ raise "Invalid type: #{type}"
101
+ end
102
+
103
+ json
104
+ end
105
+
106
+ private
107
+
108
+ def validate
109
+ validate_attributes
110
+ validate_type
111
+ end
112
+
113
+ def validate_type
114
+ return if Type::VALID_TYPES.include?(type)
115
+
116
+ raise InvalidType, type
117
+ end
118
+
119
+ def validate_attributes
120
+ [
121
+ [:text, text],
122
+ [:currency, currency],
123
+ [:date_time, date_time],
124
+ [:image, image],
125
+ [:document, document],
126
+ [:video, video]
127
+ ].each do |type_sym, value|
128
+ next unless type == type_sym
129
+ raise MissingValue.new(type, "#{type} is required when the type is #{type}") if value.nil?
130
+ end
131
+ end
132
+ end
133
+ end
134
+ end
data/lib/whatsapp_sdk.rb CHANGED
@@ -3,22 +3,33 @@
3
3
  # APIs
4
4
  require_relative "whatsapp_sdk/api/phone_numbers"
5
5
  require_relative "whatsapp_sdk/api/messages"
6
+ require_relative "whatsapp_sdk/api/medias"
6
7
  require_relative "whatsapp_sdk/api/client"
7
8
 
8
9
  # APIs responses
9
10
  require_relative "whatsapp_sdk/api/responses/message_data_response"
10
11
  require_relative "whatsapp_sdk/api/responses/phone_number_data_response"
11
12
  require_relative "whatsapp_sdk/api/responses/phone_numbers_data_response"
12
- require_relative "whatsapp_sdk/api/responses/error_response"
13
+ require_relative "whatsapp_sdk/api/responses/message_error_response"
13
14
  require_relative "whatsapp_sdk/api/responses/data_response"
15
+ require_relative "whatsapp_sdk/api/responses/read_message_data_response"
16
+ require_relative "whatsapp_sdk/api/responses/media_data_response"
17
+ require_relative "whatsapp_sdk/api/responses/success_response"
18
+ require_relative "whatsapp_sdk/api/responses/error_response"
14
19
 
15
20
  # Resources
16
21
  require_relative "whatsapp_sdk/resource/address"
22
+ require_relative "whatsapp_sdk/resource/button_parameter"
23
+ require_relative "whatsapp_sdk/resource/component"
17
24
  require_relative "whatsapp_sdk/resource/contact_response"
18
25
  require_relative "whatsapp_sdk/resource/contact"
26
+ require_relative "whatsapp_sdk/resource/currency"
27
+ require_relative "whatsapp_sdk/resource/date_time"
19
28
  require_relative "whatsapp_sdk/resource/email"
29
+ require_relative "whatsapp_sdk/resource/media"
20
30
  require_relative "whatsapp_sdk/resource/message"
21
31
  require_relative "whatsapp_sdk/resource/name"
22
32
  require_relative "whatsapp_sdk/resource/org"
33
+ require_relative "whatsapp_sdk/resource/parameter_object"
23
34
  require_relative "whatsapp_sdk/resource/phone_number"
24
35
  require_relative "whatsapp_sdk/resource/url"
data/tmp/whatsapp.png ADDED
Binary file
data/whatsapp_sdk.gemspec CHANGED
@@ -24,7 +24,7 @@ Gem::Specification.new do |spec|
24
24
  spec.metadata["changelog_uri"] = "https://github.com/ignacio-chiazzo/whatsapp_sdk/blob/main/CHANGELOG.md"
25
25
  else
26
26
  raise "RubyGems 2.0 or newer is required to protect against " \
27
- "public gem pushes."
27
+ "public gem pushes."
28
28
  end
29
29
 
30
30
  # Specify which files should be added to the gem when it is released.
@@ -41,5 +41,7 @@ Gem::Specification.new do |spec|
41
41
  spec.add_development_dependency "rake", "~> 10.0"
42
42
 
43
43
  spec.add_dependency("faraday", "~> 2.3.0")
44
+ spec.add_dependency("faraday-multipart", "~> 1.0.4")
44
45
  spec.add_dependency("oj", "~> 3.13.13")
46
+ spec.metadata['rubygems_mfa_required'] = 'true'
45
47
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: whatsapp_sdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ignacio-chiazzo
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-05-29 00:00:00.000000000 Z
11
+ date: 2022-06-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: 2.3.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: faraday-multipart
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 1.0.4
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 1.0.4
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: oj
71
85
  requirement: !ruby/object:Gem::Requirement
@@ -102,28 +116,41 @@ files:
102
116
  - Rakefile
103
117
  - bin/console
104
118
  - bin/setup
119
+ - example.rb
105
120
  - lib/version.rb
106
121
  - lib/whatsapp_sdk.rb
107
122
  - lib/whatsapp_sdk/api/client.rb
123
+ - lib/whatsapp_sdk/api/medias.rb
108
124
  - lib/whatsapp_sdk/api/messages.rb
109
125
  - lib/whatsapp_sdk/api/phone_numbers.rb
110
126
  - lib/whatsapp_sdk/api/request.rb
111
127
  - lib/whatsapp_sdk/api/response.rb
112
128
  - lib/whatsapp_sdk/api/responses/data_response.rb
113
129
  - lib/whatsapp_sdk/api/responses/error_response.rb
130
+ - lib/whatsapp_sdk/api/responses/media_data_response.rb
114
131
  - lib/whatsapp_sdk/api/responses/message_data_response.rb
132
+ - lib/whatsapp_sdk/api/responses/message_error_response.rb
115
133
  - lib/whatsapp_sdk/api/responses/phone_number_data_response.rb
116
134
  - lib/whatsapp_sdk/api/responses/phone_numbers_data_response.rb
135
+ - lib/whatsapp_sdk/api/responses/read_message_data_response.rb
136
+ - lib/whatsapp_sdk/api/responses/success_response.rb
117
137
  - lib/whatsapp_sdk/error.rb
118
138
  - lib/whatsapp_sdk/resource/address.rb
139
+ - lib/whatsapp_sdk/resource/button_parameter.rb
140
+ - lib/whatsapp_sdk/resource/component.rb
119
141
  - lib/whatsapp_sdk/resource/contact.rb
120
142
  - lib/whatsapp_sdk/resource/contact_response.rb
143
+ - lib/whatsapp_sdk/resource/currency.rb
144
+ - lib/whatsapp_sdk/resource/date_time.rb
121
145
  - lib/whatsapp_sdk/resource/email.rb
146
+ - lib/whatsapp_sdk/resource/media.rb
122
147
  - lib/whatsapp_sdk/resource/message.rb
123
148
  - lib/whatsapp_sdk/resource/name.rb
124
149
  - lib/whatsapp_sdk/resource/org.rb
150
+ - lib/whatsapp_sdk/resource/parameter_object.rb
125
151
  - lib/whatsapp_sdk/resource/phone_number.rb
126
152
  - lib/whatsapp_sdk/resource/url.rb
153
+ - tmp/whatsapp.png
127
154
  - whatsapp_sdk.gemspec
128
155
  homepage: https://github.com/ignacio-chiazzo/whatsapp_sdk
129
156
  licenses:
@@ -132,6 +159,7 @@ metadata:
132
159
  homepage_uri: https://github.com/ignacio-chiazzo/whatsapp_sdk
133
160
  source_code_uri: https://github.com/ignacio-chiazzo/whatsapp_sdk
134
161
  changelog_uri: https://github.com/ignacio-chiazzo/whatsapp_sdk/blob/main/CHANGELOG.md
162
+ rubygems_mfa_required: 'true'
135
163
  post_install_message:
136
164
  rdoc_options: []
137
165
  require_paths: