comms_sdk 1.0.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.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.standard.yml +3 -0
- data/LICENSE.txt +21 -0
- data/README.md +35 -0
- data/Rakefile +10 -0
- data/lib/comms_sdk/v1/README.md +167 -0
- data/lib/comms_sdk/v1/comms_sdk.rb +171 -0
- data/lib/comms_sdk/v1/models.rb +160 -0
- data/lib/comms_sdk/v1/utils.rb +96 -0
- data/lib/comms_sdk/version.rb +5 -0
- data/lib/comms_sdk.rb +13 -0
- data/sig/comms_sdk.rbs +4 -0
- metadata +53 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: d62a3711780407daaa0294a755a59775499665b3b07ef3a5af9edcd849f32fdb
|
|
4
|
+
data.tar.gz: 9dd7deca89b32b00f1a483e0dc5cb0738a3cf5a19406e9a73ea38111d3c7ed41
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 4dc71be2eb0b4e17b2a46e530d57a3291ea1cf056549172afefd9b542b961c350ee3cf2528b8507ddd381cb69ebfeef31d1e4c0694b34d701746ae02f3f8d477
|
|
7
|
+
data.tar.gz: 17cd00fbc6f6e3a3973402d6864c137839a5f975b315ce08852f99898d5431f2da1b89c95303c23457ebeb78427b0b08dd51f4116cf72ef009a49717d80d8fe7
|
data/.rspec
ADDED
data/.standard.yml
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 TODO: Write your name
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# CommsSdk
|
|
2
|
+
|
|
3
|
+
TODO: Delete this and the text below, and describe your gem
|
|
4
|
+
|
|
5
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/comms_sdk`. To experiment with that code, run `bin/console` for an interactive prompt.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
|
|
10
|
+
|
|
11
|
+
Install the gem and add to the application's Gemfile by executing:
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
TODO: Write usage instructions here
|
|
26
|
+
|
|
27
|
+
## Development
|
|
28
|
+
|
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
30
|
+
|
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
|
32
|
+
|
|
33
|
+
## License
|
|
34
|
+
|
|
35
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
# CommsSdk Ruby Implementation
|
|
2
|
+
|
|
3
|
+
A Ruby implementation of the Communications SDK that follows the same patterns as the Python implementation and matches the Kotlin reference implementation.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Consistent API**: Matches the standardized interface across all language implementations
|
|
8
|
+
- **Authentication**: Uses `userName` and `apiKey` authentication pattern
|
|
9
|
+
- **Dual Methods**: Provides both simple boolean return methods and full response query methods
|
|
10
|
+
- **Error Handling**: Comprehensive error handling with proper Ruby exceptions
|
|
11
|
+
- **Validation**: Phone number validation and credential validation
|
|
12
|
+
- **Logging**: Proper error logging to stderr
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
Add this line to your application's Gemfile:
|
|
17
|
+
|
|
18
|
+
```ruby
|
|
19
|
+
gem 'comms_sdk'
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
And then execute:
|
|
23
|
+
|
|
24
|
+
$ bundle install
|
|
25
|
+
|
|
26
|
+
Or install it yourself as:
|
|
27
|
+
|
|
28
|
+
$ gem install comms_sdk
|
|
29
|
+
|
|
30
|
+
## Usage
|
|
31
|
+
|
|
32
|
+
### Basic Authentication
|
|
33
|
+
|
|
34
|
+
```ruby
|
|
35
|
+
require 'comms_sdk'
|
|
36
|
+
|
|
37
|
+
# Authenticate with username and API key
|
|
38
|
+
sdk = CommsSdk.authenticate("your_username", "your_api_key")
|
|
39
|
+
|
|
40
|
+
# Or use the V1 module directly
|
|
41
|
+
sdk = CommsSdk::V1::CommsSDK.authenticate("your_username", "your_api_key")
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### Sending SMS
|
|
45
|
+
|
|
46
|
+
```ruby
|
|
47
|
+
# Send SMS to a single number
|
|
48
|
+
success = sdk.send_sms("256712345678", "Hello from Ruby!")
|
|
49
|
+
|
|
50
|
+
# Send SMS to multiple numbers
|
|
51
|
+
success = sdk.send_sms(["256712345678", "256787654321"], "Hello to all!")
|
|
52
|
+
|
|
53
|
+
# Send SMS with custom sender ID and priority
|
|
54
|
+
success = sdk.send_sms(
|
|
55
|
+
"256712345678",
|
|
56
|
+
"Hello!",
|
|
57
|
+
sender_id: "MyApp",
|
|
58
|
+
priority: CommsSdk::V1::MessagePriority::HIGH
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
# Get full API response
|
|
62
|
+
response = sdk.query_send_sms(
|
|
63
|
+
["256712345678"],
|
|
64
|
+
"Hello!",
|
|
65
|
+
"MyApp",
|
|
66
|
+
CommsSdk::V1::MessagePriority::HIGHEST
|
|
67
|
+
)
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### Checking Balance
|
|
71
|
+
|
|
72
|
+
```ruby
|
|
73
|
+
# Get balance as a float
|
|
74
|
+
balance = sdk.get_balance
|
|
75
|
+
puts "Balance: #{balance}"
|
|
76
|
+
|
|
77
|
+
# Get full balance response
|
|
78
|
+
response = sdk.query_balance
|
|
79
|
+
puts "Status: #{response.status}"
|
|
80
|
+
puts "Balance: #{response.balance}"
|
|
81
|
+
puts "Currency: #{response.currency}"
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
### Configuration
|
|
85
|
+
|
|
86
|
+
```ruby
|
|
87
|
+
# Use sandbox environment
|
|
88
|
+
CommsSdk::V1::CommsSDK.use_sandbox
|
|
89
|
+
|
|
90
|
+
# Use live server (default)
|
|
91
|
+
CommsSdk::V1::CommsSDK.use_live_server
|
|
92
|
+
|
|
93
|
+
# Set custom sender ID
|
|
94
|
+
sdk.with_sender_id("MyCustomSender")
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
## API Reference
|
|
98
|
+
|
|
99
|
+
### CommsSdk::V1::CommsSDK
|
|
100
|
+
|
|
101
|
+
#### Class Methods
|
|
102
|
+
|
|
103
|
+
- `authenticate(user_name, api_key)` - Authenticate and return SDK instance
|
|
104
|
+
- `use_sandbox` - Switch to sandbox environment
|
|
105
|
+
- `use_live_server` - Switch to live environment
|
|
106
|
+
|
|
107
|
+
#### Instance Methods
|
|
108
|
+
|
|
109
|
+
- `send_sms(numbers, message, sender_id: nil, priority: MessagePriority::HIGHEST)` - Send SMS, returns boolean
|
|
110
|
+
- `query_send_sms(numbers, message, sender_id, priority)` - Send SMS, returns full ApiResponse
|
|
111
|
+
- `get_balance()` - Get account balance as float
|
|
112
|
+
- `query_balance()` - Get full balance response as ApiResponse
|
|
113
|
+
- `with_sender_id(sender_id)` - Set sender ID, returns self for chaining
|
|
114
|
+
|
|
115
|
+
#### Properties
|
|
116
|
+
|
|
117
|
+
- `api_key` - The API key used for authentication
|
|
118
|
+
- `user_name` - The username used for authentication
|
|
119
|
+
- `sender_id` - Current sender ID
|
|
120
|
+
- `is_authenticated` - Authentication status
|
|
121
|
+
|
|
122
|
+
### Models
|
|
123
|
+
|
|
124
|
+
#### MessagePriority
|
|
125
|
+
- `HIGHEST` - Priority "0"
|
|
126
|
+
- `HIGH` - Priority "1"
|
|
127
|
+
- `MEDIUM` - Priority "2"
|
|
128
|
+
- `LOW` - Priority "3"
|
|
129
|
+
- `LOWEST` - Priority "4"
|
|
130
|
+
|
|
131
|
+
#### ApiResponse
|
|
132
|
+
- `status` - Response status ("OK" or "Failed")
|
|
133
|
+
- `message` - Response message
|
|
134
|
+
- `cost` - Message cost
|
|
135
|
+
- `currency` - Currency code
|
|
136
|
+
- `msg_follow_up_unique_code` - Unique tracking code
|
|
137
|
+
- `balance` - Account balance
|
|
138
|
+
|
|
139
|
+
## Error Handling
|
|
140
|
+
|
|
141
|
+
The SDK raises appropriate Ruby exceptions:
|
|
142
|
+
|
|
143
|
+
```ruby
|
|
144
|
+
begin
|
|
145
|
+
sdk = CommsSdk.authenticate("", "") # Empty credentials
|
|
146
|
+
rescue ArgumentError => e
|
|
147
|
+
puts "Authentication error: #{e.message}"
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
begin
|
|
151
|
+
sdk.send_sms([], "") # Empty numbers and message
|
|
152
|
+
rescue ArgumentError => e
|
|
153
|
+
puts "Validation error: #{e.message}"
|
|
154
|
+
end
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
## Thread Safety
|
|
158
|
+
|
|
159
|
+
The SDK is thread-safe for read operations. For write operations or shared state modifications, use appropriate synchronization mechanisms.
|
|
160
|
+
|
|
161
|
+
## Contributing
|
|
162
|
+
|
|
163
|
+
Bug reports and pull requests are welcome on GitHub.
|
|
164
|
+
|
|
165
|
+
## License
|
|
166
|
+
|
|
167
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'net/http'
|
|
4
|
+
require 'json'
|
|
5
|
+
require 'uri'
|
|
6
|
+
require_relative 'models'
|
|
7
|
+
require_relative 'utils'
|
|
8
|
+
|
|
9
|
+
module CommsSdk
|
|
10
|
+
module V1
|
|
11
|
+
class CommsSDK
|
|
12
|
+
API_URL = "http://176.58.101.43:8080/communications/api/v1/json/"
|
|
13
|
+
|
|
14
|
+
attr_reader :api_key, :user_name, :sender_id, :is_authenticated
|
|
15
|
+
|
|
16
|
+
def initialize
|
|
17
|
+
@api_key = nil
|
|
18
|
+
@user_name = nil
|
|
19
|
+
@sender_id = "EgoSMS"
|
|
20
|
+
@is_authenticated = false
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def self.authenticate(user_name, api_key)
|
|
24
|
+
sdk = new
|
|
25
|
+
sdk.instance_variable_set(:@user_name, user_name)
|
|
26
|
+
sdk.instance_variable_set(:@api_key, api_key)
|
|
27
|
+
Utils::Validator.validate_credentials(sdk)
|
|
28
|
+
sdk
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def self.use_sandbox
|
|
32
|
+
remove_const(:API_URL) if const_defined?(:API_URL)
|
|
33
|
+
const_set(:API_URL, "http://176.58.101.43:8080/communications/api/v1/json")
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def self.use_live_server
|
|
37
|
+
remove_const(:API_URL) if const_defined?(:API_URL)
|
|
38
|
+
const_set(:API_URL, "http://176.58.101.43:8080/communications/api/v1/json")
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def set_authenticated
|
|
42
|
+
@is_authenticated = true
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def with_sender_id(sender_id)
|
|
46
|
+
@sender_id = sender_id
|
|
47
|
+
self
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def send_sms(numbers, message, sender_id: nil, priority: MessagePriority::HIGHEST)
|
|
51
|
+
numbers = [numbers] if numbers.is_a?(String)
|
|
52
|
+
|
|
53
|
+
api_response = query_send_sms(numbers, message, sender_id || @sender_id, priority)
|
|
54
|
+
|
|
55
|
+
if api_response.nil?
|
|
56
|
+
puts "Failed to get a response from the server."
|
|
57
|
+
return false
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
case api_response.status
|
|
61
|
+
when ApiResponseCode::OK
|
|
62
|
+
puts "SMS sent successfully."
|
|
63
|
+
puts "MessageFollowUpUniqueCode: #{api_response.msg_follow_up_unique_code}" if api_response.msg_follow_up_unique_code
|
|
64
|
+
true
|
|
65
|
+
when ApiResponseCode::FAILED
|
|
66
|
+
puts "Failed: #{api_response.message}"
|
|
67
|
+
false
|
|
68
|
+
else
|
|
69
|
+
raise "Unexpected response status: #{api_response.status}"
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def query_send_sms(numbers, message, sender_id, priority)
|
|
74
|
+
return nil if sdk_not_authenticated?
|
|
75
|
+
|
|
76
|
+
raise ArgumentError, "Numbers list cannot be empty" if numbers.nil? || numbers.empty?
|
|
77
|
+
raise ArgumentError, "Message cannot be empty" if message.nil? || message.empty?
|
|
78
|
+
raise ArgumentError, "Message cannot be a single character" if message.length == 1
|
|
79
|
+
|
|
80
|
+
sender_id = @sender_id if sender_id.nil? || sender_id.strip.empty?
|
|
81
|
+
|
|
82
|
+
if sender_id.length > 11
|
|
83
|
+
puts "Warning: Sender ID length exceeds 11 characters. Some networks may truncate or reject messages."
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
numbers = Utils::NumberValidator.validate_numbers(numbers)
|
|
87
|
+
if numbers.empty?
|
|
88
|
+
$stderr.puts "No valid phone numbers provided. Please check inputs."
|
|
89
|
+
return nil
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
message_models = numbers.map do |num|
|
|
93
|
+
MessageModel.new(number: num, message: message, senderid: sender_id, priority: priority.value)
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
api_request = ApiRequest.new(
|
|
97
|
+
method: "SendSms",
|
|
98
|
+
userdata: UserData.new(@user_name, @api_key),
|
|
99
|
+
msgdata: message_models
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
begin
|
|
103
|
+
response = make_http_request(api_request)
|
|
104
|
+
ApiResponse.from_hash(JSON.parse(response.body))
|
|
105
|
+
rescue => e
|
|
106
|
+
$stderr.puts "Failed to send SMS: #{e.message}"
|
|
107
|
+
begin
|
|
108
|
+
$stderr.puts "Request: #{api_request.to_hash}"
|
|
109
|
+
rescue
|
|
110
|
+
# Ignore serialization errors
|
|
111
|
+
end
|
|
112
|
+
nil
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
def query_balance
|
|
117
|
+
return nil if sdk_not_authenticated?
|
|
118
|
+
|
|
119
|
+
api_request = ApiRequest.new(
|
|
120
|
+
method: "Balance",
|
|
121
|
+
userdata: UserData.new(@user_name, @api_key)
|
|
122
|
+
)
|
|
123
|
+
|
|
124
|
+
begin
|
|
125
|
+
response = make_http_request(api_request)
|
|
126
|
+
ApiResponse.from_hash(JSON.parse(response.body))
|
|
127
|
+
rescue => e
|
|
128
|
+
raise "Failed to get balance: #{e.message}"
|
|
129
|
+
end
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
def get_balance
|
|
133
|
+
response = query_balance
|
|
134
|
+
return nil if response.nil? || response.balance.nil?
|
|
135
|
+
|
|
136
|
+
begin
|
|
137
|
+
Float(response.balance)
|
|
138
|
+
rescue ArgumentError
|
|
139
|
+
nil
|
|
140
|
+
end
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
def to_s
|
|
144
|
+
"SDK(#{@user_name} => #{@api_key})"
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
private
|
|
148
|
+
|
|
149
|
+
def sdk_not_authenticated?
|
|
150
|
+
unless @is_authenticated
|
|
151
|
+
$stderr.puts "SDK is not authenticated. Please authenticate before performing actions."
|
|
152
|
+
$stderr.puts "Attempting to re-authenticate with provided credentials..."
|
|
153
|
+
return !Utils::Validator.validate_credentials(self)
|
|
154
|
+
end
|
|
155
|
+
false
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
def make_http_request(api_request)
|
|
159
|
+
uri = URI(API_URL)
|
|
160
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
|
161
|
+
http.use_ssl = uri.scheme == 'https'
|
|
162
|
+
|
|
163
|
+
request = Net::HTTP::Post.new(uri)
|
|
164
|
+
request['Content-Type'] = 'application/json'
|
|
165
|
+
request.body = api_request.to_json
|
|
166
|
+
|
|
167
|
+
http.request(request)
|
|
168
|
+
end
|
|
169
|
+
end
|
|
170
|
+
end
|
|
171
|
+
end
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'json'
|
|
4
|
+
|
|
5
|
+
module CommsSdk
|
|
6
|
+
module V1
|
|
7
|
+
class ApiResponseCode
|
|
8
|
+
OK = "OK"
|
|
9
|
+
FAILED = "Failed"
|
|
10
|
+
|
|
11
|
+
def self.from_json(json_string)
|
|
12
|
+
case json_string.downcase
|
|
13
|
+
when "ok"
|
|
14
|
+
OK
|
|
15
|
+
when "failed"
|
|
16
|
+
FAILED
|
|
17
|
+
else
|
|
18
|
+
raise ArgumentError, "Unknown value: #{json_string}"
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
class MessagePriority
|
|
24
|
+
attr_reader :value
|
|
25
|
+
|
|
26
|
+
def initialize(value)
|
|
27
|
+
@value = value
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def self.from_value(text)
|
|
31
|
+
case text
|
|
32
|
+
when "0"
|
|
33
|
+
HIGHEST
|
|
34
|
+
when "1"
|
|
35
|
+
HIGH
|
|
36
|
+
when "2"
|
|
37
|
+
MEDIUM
|
|
38
|
+
when "3"
|
|
39
|
+
LOW
|
|
40
|
+
when "4"
|
|
41
|
+
LOWEST
|
|
42
|
+
else
|
|
43
|
+
raise ArgumentError, "Unknown priority value: #{text}"
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
HIGHEST = new("0")
|
|
48
|
+
HIGH = new("1")
|
|
49
|
+
MEDIUM = new("2")
|
|
50
|
+
LOW = new("3")
|
|
51
|
+
LOWEST = new("4")
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
class UserData
|
|
55
|
+
attr_reader :username, :apikey
|
|
56
|
+
|
|
57
|
+
def initialize(username, apikey)
|
|
58
|
+
@username = username
|
|
59
|
+
@apikey = apikey
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def to_hash
|
|
63
|
+
{
|
|
64
|
+
"username" => @username,
|
|
65
|
+
"password" => @apikey # Maps to "password" in JSON
|
|
66
|
+
}
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def to_json(*args)
|
|
70
|
+
to_hash.to_json(*args)
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
class MessageModel
|
|
75
|
+
attr_reader :number, :message, :senderid, :priority
|
|
76
|
+
|
|
77
|
+
def initialize(number:, message:, senderid:, priority:)
|
|
78
|
+
@number = number
|
|
79
|
+
@message = message
|
|
80
|
+
@senderid = senderid
|
|
81
|
+
@priority = priority
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def to_hash
|
|
85
|
+
{
|
|
86
|
+
"number" => @number,
|
|
87
|
+
"message" => @message,
|
|
88
|
+
"senderid" => @senderid,
|
|
89
|
+
"priority" => @priority
|
|
90
|
+
}
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def to_json(*args)
|
|
94
|
+
to_hash.to_json(*args)
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
class ApiRequest
|
|
99
|
+
attr_reader :method, :userdata, :msgdata
|
|
100
|
+
|
|
101
|
+
def initialize(method:, userdata:, msgdata: nil)
|
|
102
|
+
@method = method
|
|
103
|
+
@userdata = userdata
|
|
104
|
+
@msgdata = msgdata
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def to_hash
|
|
108
|
+
hash = {
|
|
109
|
+
"method" => @method,
|
|
110
|
+
"userdata" => @userdata.to_hash
|
|
111
|
+
}
|
|
112
|
+
hash["msgdata"] = @msgdata.map(&:to_hash) if @msgdata
|
|
113
|
+
hash
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
def to_json(*args)
|
|
117
|
+
to_hash.to_json(*args)
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
class ApiResponse
|
|
122
|
+
attr_reader :status, :message, :cost, :currency, :msg_follow_up_unique_code, :balance
|
|
123
|
+
|
|
124
|
+
def initialize(status:, message: nil, cost: nil, currency: nil, msg_follow_up_unique_code: nil, balance: nil)
|
|
125
|
+
@status = status
|
|
126
|
+
@message = message
|
|
127
|
+
@cost = cost
|
|
128
|
+
@currency = currency
|
|
129
|
+
@msg_follow_up_unique_code = msg_follow_up_unique_code
|
|
130
|
+
@balance = balance
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
def self.from_hash(hash)
|
|
134
|
+
new(
|
|
135
|
+
status: hash["Status"],
|
|
136
|
+
message: hash["Message"],
|
|
137
|
+
cost: hash["Cost"],
|
|
138
|
+
currency: hash["Currency"],
|
|
139
|
+
msg_follow_up_unique_code: hash["MsgFollowUpUniqueCode"],
|
|
140
|
+
balance: hash["Balance"]
|
|
141
|
+
)
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
def to_hash
|
|
145
|
+
{
|
|
146
|
+
"Status" => @status,
|
|
147
|
+
"Message" => @message,
|
|
148
|
+
"Cost" => @cost,
|
|
149
|
+
"Currency" => @currency,
|
|
150
|
+
"MsgFollowUpUniqueCode" => @msg_follow_up_unique_code,
|
|
151
|
+
"Balance" => @balance
|
|
152
|
+
}.compact
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
def to_json(*args)
|
|
156
|
+
to_hash.to_json(*args)
|
|
157
|
+
end
|
|
158
|
+
end
|
|
159
|
+
end
|
|
160
|
+
end
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'net/http'
|
|
4
|
+
require 'json'
|
|
5
|
+
require 'uri'
|
|
6
|
+
require 'set'
|
|
7
|
+
|
|
8
|
+
module CommsSdk
|
|
9
|
+
module V1
|
|
10
|
+
module Utils
|
|
11
|
+
class NumberValidator
|
|
12
|
+
REGEX = /^\+?(0|\d{3})\d{9}$/
|
|
13
|
+
|
|
14
|
+
def self.validate_numbers(numbers)
|
|
15
|
+
return [] if numbers.nil? || numbers.empty?
|
|
16
|
+
|
|
17
|
+
cleansed = Set.new
|
|
18
|
+
numbers.each do |number|
|
|
19
|
+
next if number.nil? || number.strip.empty?
|
|
20
|
+
|
|
21
|
+
cleaned_number = number.strip.gsub(/[-\s]/, '')
|
|
22
|
+
if cleaned_number.match?(REGEX)
|
|
23
|
+
if cleaned_number.start_with?('0')
|
|
24
|
+
cleaned_number = '256' + cleaned_number[1..]
|
|
25
|
+
elsif cleaned_number.start_with?('+')
|
|
26
|
+
cleaned_number = cleaned_number[1..]
|
|
27
|
+
end
|
|
28
|
+
cleansed.add(cleaned_number)
|
|
29
|
+
else
|
|
30
|
+
puts "Number (#{number}) is not valid!"
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
cleansed.to_a
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
class Validator
|
|
39
|
+
def self.validate_credentials(sdk)
|
|
40
|
+
if sdk.api_key.nil? || sdk.api_key.empty? || sdk.user_name.nil? || sdk.user_name.empty?
|
|
41
|
+
raise ArgumentError, "Either API Key or Username must be provided"
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
unless valid_credential?(sdk)
|
|
45
|
+
puts <<~ASCII
|
|
46
|
+
_
|
|
47
|
+
/\\ _|_ |_ _ ._ _|_ o _ _. _|_ o _ ._ |_ _. o | _ _| | |
|
|
48
|
+
/--\\ |_| |_ | | (/_ | | |_ | (_ (_| |_ | (_) | | | (_| | | (/_ (_| o o
|
|
49
|
+
|
|
50
|
+
ASCII
|
|
51
|
+
puts
|
|
52
|
+
raise "Credentials validation failed"
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
puts "Credentials validated successfully."
|
|
56
|
+
puts "Validated using basic auth"
|
|
57
|
+
sdk.set_authenticated
|
|
58
|
+
true
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
private
|
|
62
|
+
|
|
63
|
+
def self.valid_credential?(sdk)
|
|
64
|
+
api_request = ApiRequest.new(
|
|
65
|
+
method: "Balance",
|
|
66
|
+
userdata: UserData.new(sdk.user_name, sdk.api_key)
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
begin
|
|
70
|
+
uri = URI(CommsSdk::V1::CommsSDK::API_URL)
|
|
71
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
|
72
|
+
http.use_ssl = uri.scheme == 'https'
|
|
73
|
+
|
|
74
|
+
request = Net::HTTP::Post.new(uri)
|
|
75
|
+
request['Content-Type'] = 'application/json'
|
|
76
|
+
request.body = api_request.to_json
|
|
77
|
+
|
|
78
|
+
response = http.request(request)
|
|
79
|
+
api_response = ApiResponse.from_hash(JSON.parse(response.body))
|
|
80
|
+
|
|
81
|
+
if api_response.status == ApiResponseCode::OK
|
|
82
|
+
puts "Credentials validated successfully."
|
|
83
|
+
true
|
|
84
|
+
else
|
|
85
|
+
puts "Error validating credentials: #{api_response.message}"
|
|
86
|
+
false
|
|
87
|
+
end
|
|
88
|
+
rescue => e
|
|
89
|
+
puts "Error validating credentials: #{e.message}"
|
|
90
|
+
false
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
end
|
data/lib/comms_sdk.rb
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "comms_sdk/version"
|
|
4
|
+
require_relative "comms_sdk/v1/comms_sdk"
|
|
5
|
+
|
|
6
|
+
module CommsSdk
|
|
7
|
+
class Error < StandardError; end
|
|
8
|
+
|
|
9
|
+
# Convenience method to access the V1 SDK
|
|
10
|
+
def self.authenticate(user_name, api_key)
|
|
11
|
+
V1::CommsSDK.authenticate(user_name, api_key)
|
|
12
|
+
end
|
|
13
|
+
end
|
data/sig/comms_sdk.rbs
ADDED
metadata
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: comms_sdk
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Daniel
|
|
8
|
+
bindir: exe
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies: []
|
|
12
|
+
description: Pahappa Comms platform SDK.
|
|
13
|
+
email:
|
|
14
|
+
- daniel@ug.pahappa.com
|
|
15
|
+
executables: []
|
|
16
|
+
extensions: []
|
|
17
|
+
extra_rdoc_files: []
|
|
18
|
+
files:
|
|
19
|
+
- ".rspec"
|
|
20
|
+
- ".standard.yml"
|
|
21
|
+
- LICENSE.txt
|
|
22
|
+
- README.md
|
|
23
|
+
- Rakefile
|
|
24
|
+
- lib/comms_sdk.rb
|
|
25
|
+
- lib/comms_sdk/v1/README.md
|
|
26
|
+
- lib/comms_sdk/v1/comms_sdk.rb
|
|
27
|
+
- lib/comms_sdk/v1/models.rb
|
|
28
|
+
- lib/comms_sdk/v1/utils.rb
|
|
29
|
+
- lib/comms_sdk/version.rb
|
|
30
|
+
- sig/comms_sdk.rbs
|
|
31
|
+
homepage: https://www.pahappa.com
|
|
32
|
+
licenses:
|
|
33
|
+
- MIT
|
|
34
|
+
metadata:
|
|
35
|
+
homepage_uri: https://www.pahappa.com
|
|
36
|
+
rdoc_options: []
|
|
37
|
+
require_paths:
|
|
38
|
+
- lib
|
|
39
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
40
|
+
requirements:
|
|
41
|
+
- - ">="
|
|
42
|
+
- !ruby/object:Gem::Version
|
|
43
|
+
version: 3.1.0
|
|
44
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
45
|
+
requirements:
|
|
46
|
+
- - ">="
|
|
47
|
+
- !ruby/object:Gem::Version
|
|
48
|
+
version: '0'
|
|
49
|
+
requirements: []
|
|
50
|
+
rubygems_version: 3.6.9
|
|
51
|
+
specification_version: 4
|
|
52
|
+
summary: Pahappa Comms platform SDK.
|
|
53
|
+
test_files: []
|