direct7 0.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 6175cc93029466c8a65427a26da9cc33161a5a50b36aa0325f09f200f6a585f8
4
+ data.tar.gz: 4bb97133bc32676cab935fe1daedc20b49f59f8dc4237305dce348f86020c6f2
5
+ SHA512:
6
+ metadata.gz: bedd96dd8d8f0b3de2047fde528b74c552bad84cde221336f311733a8f7cf61897d9b790ab98765019bb5403d17c4fa61a879c3dffad54f42c24dfc7911cdf92
7
+ data.tar.gz: 6724d2b2fa62a1cfa7ab19c5f7cf734d1953986d74541e5ff805da49bda7b9fc74810b4d06b3856783416e28c93610b6ef31191aa48da860287d9fe14ee81841
@@ -0,0 +1,132 @@
1
+ require 'json'
2
+ require 'net/http'
3
+ require 'uri'
4
+ require 'http'
5
+ require 'net/http/persistent'
6
+ require_relative '../direct7/version'
7
+ require_relative '../direct7/sms'
8
+ require_relative '../direct7/verify'
9
+ require_relative '../direct7/slack'
10
+ require_relative '../direct7/viber'
11
+ require_relative '../direct7/number_lookup'
12
+ require_relative '../direct7/whatsapp'
13
+
14
+ module Direct7
15
+ class Client
16
+ attr_accessor :timeout, :session, :adapter
17
+
18
+ def initialize(api_token = nil, timeout = 30, pool_connections = 10, pool_maxsize = 10, max_retries = 3)
19
+ @api_token = api_token
20
+ @host = 'https://api.d7networks.com'
21
+ user_agent = "direct7-ruby-sdk/#{Direct7::VERSION} ruby/#{RUBY_VERSION}"
22
+ @headers = {
23
+ 'User-Agent' => user_agent,
24
+ 'Accept' => 'application/json'
25
+ }
26
+
27
+ def sms
28
+ @sms
29
+ end
30
+ def verify
31
+ @verify
32
+ end
33
+ def slack
34
+ @slack
35
+ end
36
+ def viber
37
+ @viber
38
+ end
39
+ def number_lookup
40
+ @number_lookup
41
+ end
42
+ def whatsapp
43
+ @whatsapp
44
+ end
45
+ @sms = Direct7::SMS.new(self)
46
+ @verify = Direct7::VERIFY.new(self)
47
+ @slack = Direct7::SLACK.new(self)
48
+ @viber = Direct7::VIBER.new(self)
49
+ @number_lookup = Direct7::NUMBER_LOOKUP.new(self)
50
+ @whatsapp = Direct7::WHATSAPP.new(self)
51
+ # ... initialize other services ...
52
+
53
+ @session = HTTP.persistent(@host)
54
+ @session.headers('User-Agent' => user_agent, 'Accept' => 'application/json')
55
+ @session.timeout(write: timeout, connect: timeout)
56
+ # @session.connect('pool_size'=> pool_connections, 'pool_max' => pool_maxsize)
57
+
58
+ end
59
+
60
+ def host(value = nil)
61
+ if value.nil?
62
+ @host
63
+ else
64
+ @host = value
65
+ end
66
+ end
67
+
68
+ def process_response(host, response)
69
+ # puts "Response headers #{response}"
70
+ case response.code.to_i
71
+ when 401
72
+ raise AuthenticationError, 'Invalid API token'
73
+ when 200..299
74
+ begin
75
+ result = JSON.parse(response.body)
76
+ # puts "Successful process response: #{result}"
77
+ result
78
+ rescue JSON::ParserError
79
+ nil
80
+ end
81
+ when 400..499
82
+ puts "Client error: #{response.code} #{response.body.inspect}"
83
+ case response.code.to_i
84
+ when 400
85
+ raise BadRequest, response.body.inspect
86
+ when 404
87
+ raise NotFoundError, response.body.inspect
88
+ when 402
89
+ raise InsufficientCreditError, response.body.inspect
90
+ when 422
91
+ raise ValidationError, response.body.inspect
92
+ else
93
+ raise ClientError, "#{response.code} response from #{host}"
94
+ end
95
+ when 500..599
96
+ puts "Server error: #{response.code} #{response.body.inspect}"
97
+ raise ServerError, "#{response.code} response from #{host}"
98
+ else
99
+ nil
100
+ end
101
+ end
102
+
103
+ def get(host, path, params = nil)
104
+ request_url = "#{host}#{path}"
105
+ request_headers = @headers.merge('Authorization' => create_bearer_token_string)
106
+ # puts "GET request sent to #{request_url} with headers #{request_headers} and params #{params}"
107
+ response = @session.get(URI(request_url), headers: request_headers)
108
+ process_response(host, response)
109
+ end
110
+
111
+ def post(host, path, body_is_json, params)
112
+ request_url = "#{host}#{path}"
113
+ request_headers = @headers.merge('Authorization' => create_bearer_token_string)
114
+ request_headers['Content-Type'] = body_is_json ? 'application/json' : 'application/x-www-form-urlencoded'
115
+ # puts "POST request sent to #{request_url} with headers #{request_headers} and params #{params}"
116
+ params_json = JSON.generate(params)
117
+ response = if body_is_json
118
+ @session.post(URI(request_url), headers: request_headers, json: params)
119
+ else
120
+ @session.post(URI(request_url), data: params, headers: request_headers)
121
+ end
122
+ # puts response
123
+ process_response(host, response)
124
+ end
125
+
126
+ private
127
+
128
+ def create_bearer_token_string
129
+ "Bearer #{@api_token}"
130
+ end
131
+ end
132
+ end
@@ -0,0 +1,25 @@
1
+ module Direct7
2
+ class AuthenticationError < StandardError
3
+ def initialize(message)
4
+ super(message)
5
+ end
6
+ end
7
+
8
+ class ClientError < StandardError
9
+ end
10
+
11
+ class ValidationError < StandardError
12
+ end
13
+
14
+ class InsufficientCreditError < StandardError
15
+ end
16
+
17
+ class NotFoundError < StandardError
18
+ end
19
+
20
+ class ServerError < StandardError
21
+ end
22
+
23
+ class BadRequest < StandardError
24
+ end
25
+ end
@@ -0,0 +1,27 @@
1
+ require 'json'
2
+ require 'net/http'
3
+ require 'uri'
4
+ require 'logger'
5
+
6
+ module Direct7
7
+ class NUMBER_LOOKUP
8
+ def initialize(client)
9
+ @client = client
10
+ @log = Logger.new(STDOUT) # You can customize the log destination as needed
11
+ end
12
+
13
+ def search_number_details(recipient)
14
+ """
15
+ Search number details.
16
+ :param params: dict - The search request parameters.
17
+ :return:
18
+ """
19
+ params = {
20
+ 'recipient' => recipient
21
+ }
22
+ response = @client.post(@client.host, '/hlr/v1/lookup',true, params= params)
23
+ @log.info('Search request is success.')
24
+ response
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,38 @@
1
+ require 'json'
2
+ require 'net/http'
3
+ require 'uri'
4
+ require 'logger'
5
+
6
+ module Direct7
7
+ class SLACK
8
+ def initialize(client)
9
+ @client = client
10
+ @log = Logger.new(STDOUT) # You can customize the log destination as needed
11
+ end
12
+
13
+ def send_slack_message(content, work_space_name, channel_name, report_url = nil)
14
+ message = {
15
+ 'channel' => 'slack',
16
+ 'content' => content,
17
+ 'work_space_name' => work_space_name,
18
+ 'channel_name' => channel_name
19
+ }
20
+ message_globals = {
21
+ 'report_url' => report_url
22
+ }
23
+ response = @client.post(@client.host, '/messages/v1/send', true, params= {
24
+ 'messages' => [message],
25
+ 'message_globals' => message_globals
26
+ })
27
+
28
+ @log.info('Message sent successfully.')
29
+ response
30
+ end
31
+
32
+ def get_status(request_id)
33
+ response = @client.get(@client.host, "/report/v1/message-log/#{request_id}")
34
+ @log.info('Message status retrieved successfully.')
35
+ response
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,37 @@
1
+ require 'json'
2
+ require 'net/http'
3
+ require 'uri'
4
+
5
+ module Direct7
6
+ class SMS
7
+ def initialize(client)
8
+ @client = client
9
+ end
10
+
11
+ def send_message(recipients, content, originator, report_url = nil, unicode = false)
12
+ message = {
13
+ 'channel' => 'sms',
14
+ 'content' => content,
15
+ 'msg_type' => 'text',
16
+ 'data_coding' => unicode ? 'unicode' : 'text',
17
+ 'recipients' => recipients
18
+ };
19
+ message_globals = {
20
+ 'originator' => originator,
21
+ 'report_url' => report_url,
22
+ };
23
+ response = @client.post(@client.host, '/messages/v1/send', true, params= {
24
+ 'messages' => [message],
25
+ 'message_globals' => message_globals
26
+ })
27
+ # puts "Message sent successfully."
28
+ response
29
+ end
30
+
31
+ def get_status(request_id)
32
+ response = @client.get(@client.host, "/report/v1/message-log/#{request_id}")
33
+ puts'Message status retrieved successfully.'
34
+ response
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,66 @@
1
+ require 'json'
2
+ require 'net/http'
3
+ require 'uri'
4
+ require 'logger'
5
+
6
+ module Direct7
7
+ class VERIFY
8
+ def initialize(client)
9
+ @client = client
10
+ @log = Logger.new(STDOUT) # You can customize the log destination as needed
11
+ end
12
+
13
+ def send_otp(originator, recipient, content= nil, data_coding= nil, expiry= nil, template_id= nil)
14
+ if template_id.nil?
15
+ params = {
16
+ 'originator' => originator,
17
+ 'recipient' => recipient,
18
+ 'content' => content,
19
+ 'expiry' => expiry,
20
+ 'data_coding' => data_coding
21
+ }
22
+ else
23
+ params = {
24
+ 'originator' => originator,
25
+ 'recipient' => recipient,
26
+ 'template_id' => template_id
27
+ }
28
+ end
29
+ response = @client.post(@client.host, '/verify/v1/otp/send-otp', true, params= params)
30
+ @log.info('OTP Message sent successfully.')
31
+ response
32
+ end
33
+
34
+ def resend_otp(otp_id)
35
+ params = {
36
+ 'otp_id' => otp_id
37
+ }
38
+ response = @client.post(
39
+ @client.host,
40
+ '/verify/v1/otp/resend-otp', true,
41
+ params= params
42
+ )
43
+ @log.info('OTP Message Re-sent successfully.')
44
+ response
45
+ end
46
+
47
+ def verify_otp(otp_id, otp_code)
48
+ params = {
49
+ 'otp_id' => otp_id,
50
+ 'otp_code' => otp_code
51
+ }
52
+ response = @client.post(@client.host, '/verify/v1/otp/verify-otp', true, params= params)
53
+ @log.info('OTP Message verified successfully.')
54
+ response
55
+ end
56
+
57
+ def get_status(otp_id)
58
+ response = @client.get(
59
+ @client.host,
60
+ "/verify/v1/report/#{otp_id}"
61
+ )
62
+ @log.info('OTP Message status retrieved successfully.')
63
+ response
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,3 @@
1
+ module Direct7
2
+ VERSION = '0.0.1'.freeze
3
+ end
@@ -0,0 +1,39 @@
1
+ require 'json'
2
+ require 'net/http'
3
+ require 'uri'
4
+ require 'logger'
5
+
6
+ module Direct7
7
+ class VIBER
8
+ def initialize(client)
9
+ @client = client
10
+ @log = Logger.new(STDOUT) # You can customize the log destination as needed
11
+ end
12
+
13
+ def send_viber_message(recipients, content, label, originator, call_back_url = nil)
14
+ message = {
15
+ 'channel' => 'viber',
16
+ 'recipients' => recipients,
17
+ 'content' => content,
18
+ 'label' => label
19
+ }
20
+ message_globals = {
21
+ 'originator' => originator,
22
+ 'call_back_url' => call_back_url
23
+ }
24
+
25
+ response = @client.post(@client.host, '/viber/v1/send', true, params= { 'messages' => [message], 'message_globals' => message_globals })
26
+ @log.info('Message sent successfully.')
27
+ response
28
+ end
29
+
30
+ def get_status(request_id)
31
+ response = @client.get(
32
+ @client.host,
33
+ "/report/v1/viber-log/#{request_id}"
34
+ )
35
+ @log.info('Message status retrieved successfully.')
36
+ response
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,105 @@
1
+ require 'json'
2
+ require 'net/http'
3
+ require 'uri'
4
+ require 'logger'
5
+
6
+ module Direct7
7
+ class WHATSAPP
8
+ def initialize(client)
9
+ @client = client
10
+ @log = Logger.new(STDOUT) # You can customize the log destination as needed
11
+ end
12
+
13
+ def send_whatsapp_freeform_message(originator, recipient, message_type, message_text= nil, first_name= nil,
14
+ last_name= nil, display_name= nil, phone= nil,
15
+ email= nil, url= nil, latitude= nil, longitude= nil,
16
+ location_name= nil, location_address= nil,
17
+ attachment_type= nil, attachment_url= nil,
18
+ attachment_caption= nil)
19
+ message = {
20
+ 'originator' => originator,
21
+ 'recipients' => [{'recipient' => recipient}],
22
+ 'content' => {
23
+ 'message_type' => message_type
24
+ }
25
+ }
26
+
27
+ case message_type
28
+ when 'CONTACTS'
29
+ message['content']['contact'] = {
30
+ 'first_name' => first_name,
31
+ 'last_name' => last_name,
32
+ 'display_name' => display_name,
33
+ 'phone' => phone,
34
+ 'email' => email,
35
+ 'url' => url
36
+ }
37
+ when 'LOCATION'
38
+ message['content']['location'] = {
39
+ 'latitude' => latitude,
40
+ 'longitude' => longitude,
41
+ 'name' => location_name,
42
+ 'address' => location_address
43
+ }
44
+ when 'ATTACHMENT'
45
+ message['content']['attachment'] = {
46
+ 'attachment_type' => attachment_type,
47
+ 'attachment_url' => attachment_url,
48
+ 'attachment_caption' => attachment_caption
49
+ }
50
+ when 'TEXT'
51
+ message['content']['message_text'] = message_text
52
+ end
53
+ puts message
54
+ response = @client.post(@client.host, '/whatsapp/v1/send', true, params= { 'messages' => [message] })
55
+ @log.info('Message sent successfully.')
56
+ response
57
+ end
58
+
59
+ def send_whatsapp_templated_message(originator, recipient, template_id,
60
+ body_parameter_values, media_type= nil, media_url= nil,
61
+ latitude= nil, longitude= nil, location_name= nil,
62
+ location_address= nil)
63
+ message = {
64
+ 'originator' => originator,
65
+ 'recipients' => [{'recipient' => recipient}],
66
+ 'content' => {
67
+ 'message_type' => 'TEMPLATE',
68
+ 'template' => {
69
+ 'template_id' => template_id,
70
+ 'body_parameter_values' => body_parameter_values
71
+ }
72
+ }
73
+ }
74
+
75
+ if media_type
76
+ if media_type == 'location'
77
+ message['content']['template']['media'] = {
78
+ 'media_type' => 'location',
79
+ 'location' => {
80
+ 'latitude' => latitude,
81
+ 'longitude' => longitude,
82
+ 'name' => location_name,
83
+ 'address' => location_address
84
+ }
85
+ }
86
+ else
87
+ message['content']['template']['media'] = { 'media_type' => media_type, 'media_url' => media_url }
88
+ end
89
+ end
90
+
91
+ response = @client.post(@client.host, '/whatsapp/v1/send', true, params= { 'messages' => [message] })
92
+ @log.info('Message sent successfully.')
93
+ response
94
+ end
95
+
96
+ def get_status(request_id)
97
+ response = @client.get(
98
+ @client.host,
99
+ "/whatsapp/v1/report/#{request_id}"
100
+ )
101
+ @log.info('Message status retrieved successfully.')
102
+ response
103
+ end
104
+ end
105
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: direct7
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Direct7 Networks
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-11-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: |-
42
+ This Ruby SDK provides a convenient and easy-to-use interface to the Direct7 REST API. The SDK allows you to perform
43
+ all the operations that are available through the REST API.
44
+ email:
45
+ - support@d7networks.com
46
+ executables: []
47
+ extensions: []
48
+ extra_rdoc_files: []
49
+ files:
50
+ - lib/direct7/client.rb
51
+ - lib/direct7/errors/errors.rb
52
+ - lib/direct7/number_lookup.rb
53
+ - lib/direct7/slack.rb
54
+ - lib/direct7/sms.rb
55
+ - lib/direct7/verify.rb
56
+ - lib/direct7/version.rb
57
+ - lib/direct7/viber.rb
58
+ - lib/direct7/whatsapp.rb
59
+ homepage: https://github.com/d7networks/direct7-ruby-sdk.git
60
+ licenses:
61
+ - MIT
62
+ metadata: {}
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubygems_version: 3.3.5
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: Ruby SDK for Direct7 Platform REST API
82
+ test_files: []