dialog_lk_esms 0.1.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/.rubocop.yml +13 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/Gemfile +13 -0
- data/Gemfile.lock +92 -0
- data/LICENSE.txt +21 -0
- data/README.md +316 -0
- data/Rakefile +16 -0
- data/lib/dialog_lk_esms/client.rb +132 -0
- data/lib/dialog_lk_esms/errors.rb +12 -0
- data/lib/dialog_lk_esms/types.rb +26 -0
- data/lib/dialog_lk_esms/version.rb +5 -0
- data/lib/dialog_lk_esms.rb +21 -0
- data/sig/dialog_lk_esms.rbs +4 -0
- metadata +103 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: a5c4ae045d14a69cd081c66c3ecc9c13f4e317424032c33624e3187b292d5f05
|
4
|
+
data.tar.gz: e8297cf521d5c66015d1a68245606eb3e693a59746b93d0268d94c24a0bb8667
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7c36293f322111c7e69d9bd08a51db414db833d9152e5a7aafdc1363a430b3f61ea1697d984945a759ec0d3efc76705439bdebad66ed23a308a2df376092fddd
|
7
|
+
data.tar.gz: c214e0ac7968b0ebe2e9687ea266b3e3714f7fe8eff54c3e04d4b43db91c5a7ee29e2c4fe77c3b212604043384549f37152dd73ebf30c0c7de551065e3a6b0f2
|
data/.rubocop.yml
ADDED
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at lahiru.lahirumadusanka@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
dialog_lk_esms (0.1.0)
|
5
|
+
dry-configurable (~> 1.0)
|
6
|
+
dry-monads (~> 1.0)
|
7
|
+
dry-types (~> 1.0)
|
8
|
+
|
9
|
+
GEM
|
10
|
+
remote: https://rubygems.org/
|
11
|
+
specs:
|
12
|
+
addressable (2.8.7)
|
13
|
+
public_suffix (>= 2.0.2, < 7.0)
|
14
|
+
ast (2.4.3)
|
15
|
+
bigdecimal (3.3.1)
|
16
|
+
concurrent-ruby (1.3.5)
|
17
|
+
crack (1.0.0)
|
18
|
+
bigdecimal
|
19
|
+
rexml
|
20
|
+
dry-configurable (1.0.1)
|
21
|
+
dry-core (~> 1.0, < 2)
|
22
|
+
zeitwerk (~> 2.6)
|
23
|
+
dry-core (1.0.0)
|
24
|
+
concurrent-ruby (~> 1.0)
|
25
|
+
zeitwerk (~> 2.6)
|
26
|
+
dry-inflector (1.1.0)
|
27
|
+
dry-logic (1.5.0)
|
28
|
+
concurrent-ruby (~> 1.0)
|
29
|
+
dry-core (~> 1.0, < 2)
|
30
|
+
zeitwerk (~> 2.6)
|
31
|
+
dry-monads (1.6.0)
|
32
|
+
concurrent-ruby (~> 1.0)
|
33
|
+
dry-core (~> 1.0, < 2)
|
34
|
+
zeitwerk (~> 2.6)
|
35
|
+
dry-types (1.7.1)
|
36
|
+
concurrent-ruby (~> 1.0)
|
37
|
+
dry-core (~> 1.0)
|
38
|
+
dry-inflector (~> 1.0)
|
39
|
+
dry-logic (~> 1.4)
|
40
|
+
zeitwerk (~> 2.6)
|
41
|
+
hashdiff (1.2.1)
|
42
|
+
json (2.15.1)
|
43
|
+
language_server-protocol (3.17.0.5)
|
44
|
+
lint_roller (1.1.0)
|
45
|
+
minitest (5.26.0)
|
46
|
+
parallel (1.27.0)
|
47
|
+
parser (3.3.9.0)
|
48
|
+
ast (~> 2.4.1)
|
49
|
+
racc
|
50
|
+
prism (1.6.0)
|
51
|
+
public_suffix (5.1.1)
|
52
|
+
racc (1.8.1)
|
53
|
+
rainbow (3.1.1)
|
54
|
+
rake (13.3.0)
|
55
|
+
regexp_parser (2.11.3)
|
56
|
+
rexml (3.4.4)
|
57
|
+
rubocop (1.81.1)
|
58
|
+
json (~> 2.3)
|
59
|
+
language_server-protocol (~> 3.17.0.2)
|
60
|
+
lint_roller (~> 1.1.0)
|
61
|
+
parallel (~> 1.10)
|
62
|
+
parser (>= 3.3.0.2)
|
63
|
+
rainbow (>= 2.2.2, < 4.0)
|
64
|
+
regexp_parser (>= 2.9.3, < 3.0)
|
65
|
+
rubocop-ast (>= 1.47.1, < 2.0)
|
66
|
+
ruby-progressbar (~> 1.7)
|
67
|
+
unicode-display_width (>= 2.4.0, < 4.0)
|
68
|
+
rubocop-ast (1.47.1)
|
69
|
+
parser (>= 3.3.7.2)
|
70
|
+
prism (~> 1.4)
|
71
|
+
ruby-progressbar (1.13.0)
|
72
|
+
unicode-display_width (3.2.0)
|
73
|
+
unicode-emoji (~> 4.1)
|
74
|
+
unicode-emoji (4.1.0)
|
75
|
+
webmock (3.25.1)
|
76
|
+
addressable (>= 2.8.0)
|
77
|
+
crack (>= 0.3.2)
|
78
|
+
hashdiff (>= 0.4.0, < 2.0.0)
|
79
|
+
zeitwerk (2.6.18)
|
80
|
+
|
81
|
+
PLATFORMS
|
82
|
+
arm64-darwin-21
|
83
|
+
|
84
|
+
DEPENDENCIES
|
85
|
+
dialog_lk_esms!
|
86
|
+
minitest (~> 5.0)
|
87
|
+
rake (~> 13.0)
|
88
|
+
rubocop (~> 1.21)
|
89
|
+
webmock (~> 3.0)
|
90
|
+
|
91
|
+
BUNDLED WITH
|
92
|
+
2.3.26
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2025 Lahiru Himesh Madusanka
|
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,316 @@
|
|
1
|
+
# DialogLkEsms
|
2
|
+
|
3
|
+
A Ruby gem for sending SMS messages via Dialog eSMS API in Sri Lanka. Built with dry-rb ecosystem for robust, functional programming patterns.
|
4
|
+
|
5
|
+
## Features
|
6
|
+
|
7
|
+
- 🚀 **Simple API** - Easy to use client for sending SMS messages
|
8
|
+
- 💰 **Balance Checking** - Check your account balance
|
9
|
+
- 🔒 **Type Safety** - Built with dry-types for data validation
|
10
|
+
- ⚡ **Functional Programming** - Uses dry-monads for reliable error handling
|
11
|
+
- 🛡️ **Robust Error Handling** - Comprehensive error types and handling
|
12
|
+
- 🔧 **Configurable** - Flexible configuration options
|
13
|
+
|
14
|
+
## Installation
|
15
|
+
|
16
|
+
Add this line to your application's Gemfile:
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
gem 'dialog_lk_esms'
|
20
|
+
```
|
21
|
+
|
22
|
+
And then execute:
|
23
|
+
|
24
|
+
```bash
|
25
|
+
$ bundle install
|
26
|
+
```
|
27
|
+
|
28
|
+
Or install it directly:
|
29
|
+
|
30
|
+
```bash
|
31
|
+
$ gem install dialog_lk_esms
|
32
|
+
```
|
33
|
+
|
34
|
+
## Quick Start
|
35
|
+
|
36
|
+
### Basic Usage
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
require 'dialog_lk_esms'
|
40
|
+
|
41
|
+
# Create a client
|
42
|
+
client = DialogLkEsms.client(api_key: 'your_api_key_here')
|
43
|
+
|
44
|
+
# Send a message
|
45
|
+
result = client.send_message(
|
46
|
+
number_list: '0771234567',
|
47
|
+
message: 'Hello from Dialog eSMS!',
|
48
|
+
source_address: 'YOUR_SENDER_ID'
|
49
|
+
)
|
50
|
+
|
51
|
+
if result.success?
|
52
|
+
puts "Message sent successfully!"
|
53
|
+
puts "Response: #{result.value!.message}"
|
54
|
+
else
|
55
|
+
puts "Failed to send message: #{result.failure.message}"
|
56
|
+
end
|
57
|
+
```
|
58
|
+
|
59
|
+
### Check Account Balance
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
# Check your account balance
|
63
|
+
balance_result = client.check_balance
|
64
|
+
|
65
|
+
if balance_result.success?
|
66
|
+
balance = balance_result.value!.payload[:balance]
|
67
|
+
puts "Your balance: #{balance}"
|
68
|
+
else
|
69
|
+
puts "Failed to check balance: #{balance_result.failure.message}"
|
70
|
+
end
|
71
|
+
```
|
72
|
+
|
73
|
+
## Configuration
|
74
|
+
|
75
|
+
### Environment Variables
|
76
|
+
|
77
|
+
Set your API key as an environment variable:
|
78
|
+
|
79
|
+
```bash
|
80
|
+
export DIALOG_LK_ESMS_API_KEY="your_api_key_here"
|
81
|
+
```
|
82
|
+
|
83
|
+
Then use the configured client:
|
84
|
+
|
85
|
+
```ruby
|
86
|
+
# Uses API key from environment
|
87
|
+
client = DialogLkEsms.client(api_key: ENV['DIALOG_LK_ESMS_API_KEY'])
|
88
|
+
```
|
89
|
+
|
90
|
+
### Global Configuration
|
91
|
+
|
92
|
+
```ruby
|
93
|
+
DialogLkEsms.configure do |config|
|
94
|
+
config.api_key = 'your_api_key_here'
|
95
|
+
config.base_url = 'https://e-sms.dialog.lk/api/v1' # Default
|
96
|
+
end
|
97
|
+
```
|
98
|
+
|
99
|
+
## API Reference
|
100
|
+
|
101
|
+
### Client Methods
|
102
|
+
|
103
|
+
#### `send_message`
|
104
|
+
|
105
|
+
Send SMS messages to one or more recipients.
|
106
|
+
|
107
|
+
```ruby
|
108
|
+
result = client.send_message(
|
109
|
+
number_list: ['0771234567', '0777654321'], # Array or single string
|
110
|
+
message: 'Your message here',
|
111
|
+
source_address: 'SENDER_ID', # Your registered sender ID
|
112
|
+
push_notification_url: 'https://your-callback-url.com' # Optional
|
113
|
+
)
|
114
|
+
```
|
115
|
+
|
116
|
+
**Parameters:**
|
117
|
+
- `number_list` (String|Array) - Phone numbers to send to
|
118
|
+
- `message` (String) - SMS message content
|
119
|
+
- `source_address` (String) - Your registered sender ID
|
120
|
+
- `push_notification_url` (String, optional) - Callback URL for delivery reports
|
121
|
+
|
122
|
+
**Returns:** `Result::Success(SendResult)` or `Result::Failure(SendResult)`
|
123
|
+
|
124
|
+
#### `check_balance`
|
125
|
+
|
126
|
+
Check your account balance.
|
127
|
+
|
128
|
+
```ruby
|
129
|
+
result = client.check_balance
|
130
|
+
```
|
131
|
+
|
132
|
+
**Returns:** `Result::Success(BalanceResult)` or `Result::Failure(BalanceResult)`
|
133
|
+
|
134
|
+
### Result Objects
|
135
|
+
|
136
|
+
#### SendResult
|
137
|
+
|
138
|
+
```ruby
|
139
|
+
result = client.send_message(...)
|
140
|
+
|
141
|
+
if result.success?
|
142
|
+
send_result = result.value!
|
143
|
+
puts send_result.code # Status code (e.g., "1" for success)
|
144
|
+
puts send_result.ok # Boolean success indicator
|
145
|
+
puts send_result.message # Human-readable message
|
146
|
+
puts send_result.raw # Raw API response
|
147
|
+
end
|
148
|
+
```
|
149
|
+
|
150
|
+
#### BalanceResult
|
151
|
+
|
152
|
+
```ruby
|
153
|
+
result = client.check_balance
|
154
|
+
|
155
|
+
if result.success?
|
156
|
+
balance_result = result.value!
|
157
|
+
puts balance_result.code # Status code
|
158
|
+
puts balance_result.ok # Boolean success indicator
|
159
|
+
puts balance_result.message # Human-readable message
|
160
|
+
puts balance_result.payload[:balance] # Balance amount (BigDecimal)
|
161
|
+
end
|
162
|
+
```
|
163
|
+
|
164
|
+
## Error Handling
|
165
|
+
|
166
|
+
The gem uses dry-monads for functional error handling. All methods return `Result` objects that can be either `Success` or `Failure`.
|
167
|
+
|
168
|
+
### Error Types
|
169
|
+
|
170
|
+
```ruby
|
171
|
+
# Configuration errors
|
172
|
+
DialogLkEsms::Errors::ConfigurationError
|
173
|
+
|
174
|
+
# Network/transport errors
|
175
|
+
DialogLkEsms::Errors::TransportError
|
176
|
+
|
177
|
+
# Parsing errors
|
178
|
+
DialogLkEsms::Errors::ParseError
|
179
|
+
```
|
180
|
+
|
181
|
+
### Example Error Handling
|
182
|
+
|
183
|
+
```ruby
|
184
|
+
result = client.send_message(
|
185
|
+
number_list: '0771234567',
|
186
|
+
message: 'Hello',
|
187
|
+
source_address: 'SENDER'
|
188
|
+
)
|
189
|
+
|
190
|
+
case result
|
191
|
+
when Success
|
192
|
+
puts "Message sent: #{result.value!.message}"
|
193
|
+
when Failure
|
194
|
+
case result.failure
|
195
|
+
when DialogLkEsms::Errors::ConfigurationError
|
196
|
+
puts "Configuration error: #{result.failure.message}"
|
197
|
+
when DialogLkEsms::Errors::TransportError
|
198
|
+
puts "Network error: #{result.failure.message}"
|
199
|
+
else
|
200
|
+
puts "API error: #{result.failure.message}"
|
201
|
+
end
|
202
|
+
end
|
203
|
+
```
|
204
|
+
|
205
|
+
## Status Codes
|
206
|
+
|
207
|
+
The API returns various status codes. Here are the common ones:
|
208
|
+
|
209
|
+
| Code | Message | Description |
|
210
|
+
|------|---------|-------------|
|
211
|
+
| `1` | Success | Message sent successfully |
|
212
|
+
| `2001` | Error occurred during campaign creation | General API error |
|
213
|
+
| `2002` | Bad request | Invalid request parameters |
|
214
|
+
| `2003` | Empty number list | No phone numbers provided |
|
215
|
+
| `2004` | Empty message body | No message content |
|
216
|
+
| `2005` | Invalid number list format | Malformed phone numbers |
|
217
|
+
| `2006` | Not eligible to send messages via GET requests | API access issue |
|
218
|
+
| `2007` | Invalid key | Invalid API key |
|
219
|
+
| `2008` | Insufficient balance or package quota | Not enough credits |
|
220
|
+
| `2009` | No valid numbers after mask-block removal | All numbers blocked |
|
221
|
+
| `2010` | Not eligible to consume packaging | Package access issue |
|
222
|
+
| `2011` | Transactional error | Transaction failed |
|
223
|
+
|
224
|
+
## Advanced Usage
|
225
|
+
|
226
|
+
### Multiple Recipients
|
227
|
+
|
228
|
+
```ruby
|
229
|
+
# Send to multiple numbers
|
230
|
+
result = client.send_message(
|
231
|
+
number_list: ['0771234567', '0777654321', '0779876543'],
|
232
|
+
message: 'Bulk message to multiple recipients',
|
233
|
+
source_address: 'COMPANY'
|
234
|
+
)
|
235
|
+
```
|
236
|
+
|
237
|
+
### With Callback URL
|
238
|
+
|
239
|
+
```ruby
|
240
|
+
result = client.send_message(
|
241
|
+
number_list: '0771234567',
|
242
|
+
message: 'Message with delivery callback',
|
243
|
+
source_address: 'SENDER',
|
244
|
+
push_notification_url: 'https://your-app.com/sms-callback'
|
245
|
+
)
|
246
|
+
```
|
247
|
+
|
248
|
+
### Custom Base URL
|
249
|
+
|
250
|
+
```ruby
|
251
|
+
# Use custom API endpoint
|
252
|
+
client = DialogLkEsms.client(
|
253
|
+
api_key: 'your_key',
|
254
|
+
base_url: 'https://custom-api.dialog.lk/api/v1'
|
255
|
+
)
|
256
|
+
```
|
257
|
+
|
258
|
+
## Type Validation
|
259
|
+
|
260
|
+
The gem includes built-in type validation using dry-types:
|
261
|
+
|
262
|
+
```ruby
|
263
|
+
# Phone number validation
|
264
|
+
DialogLkEsms::Types::PhoneList.call(['0771234567']) # Success
|
265
|
+
DialogLkEsms::Types::PhoneList.call(['123']) # Failure - too short
|
266
|
+
|
267
|
+
# Message validation
|
268
|
+
DialogLkEsms::Types::MessageText.call('Hello') # Success
|
269
|
+
DialogLkEsms::Types::MessageText.call('') # Failure - empty
|
270
|
+
|
271
|
+
# Source address validation
|
272
|
+
DialogLkEsms::Types::SourceAddr.call('SENDER') # Success
|
273
|
+
DialogLkEsms::Types::SourceAddr.call('') # Failure - empty
|
274
|
+
```
|
275
|
+
|
276
|
+
## Testing
|
277
|
+
|
278
|
+
The gem includes comprehensive test coverage. Run tests with:
|
279
|
+
|
280
|
+
```bash
|
281
|
+
bundle exec ruby -Itest test/test_*.rb
|
282
|
+
```
|
283
|
+
|
284
|
+
## Requirements
|
285
|
+
|
286
|
+
- Ruby >= 2.6.0
|
287
|
+
- Valid Dialog eSMS API key
|
288
|
+
- Registered sender ID with Dialog
|
289
|
+
|
290
|
+
## License
|
291
|
+
|
292
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
293
|
+
|
294
|
+
## Development
|
295
|
+
|
296
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run the tests:
|
297
|
+
|
298
|
+
```bash
|
299
|
+
bundle exec ruby -Itest test/test_*.rb
|
300
|
+
```
|
301
|
+
|
302
|
+
You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
303
|
+
|
304
|
+
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).
|
305
|
+
|
306
|
+
## Contributing
|
307
|
+
|
308
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/sdglhm/dialog_lk_esms. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/sdglhm/dialog_lk_esms/blob/main/CODE_OF_CONDUCT.md).
|
309
|
+
|
310
|
+
## License
|
311
|
+
|
312
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
313
|
+
|
314
|
+
## Code of Conduct
|
315
|
+
|
316
|
+
Everyone interacting in the DialogLkEsms project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/sdglhm/dialog_lk_esms/blob/main/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "bundler/gem_tasks"
|
4
|
+
require "rake/testtask"
|
5
|
+
|
6
|
+
Rake::TestTask.new(:test) do |t|
|
7
|
+
t.libs << "test"
|
8
|
+
t.libs << "lib"
|
9
|
+
t.test_files = FileList["test/**/test_*.rb"]
|
10
|
+
end
|
11
|
+
|
12
|
+
require "rubocop/rake_task"
|
13
|
+
|
14
|
+
RuboCop::RakeTask.new
|
15
|
+
|
16
|
+
task default: %i[test rubocop]
|
@@ -0,0 +1,132 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "uri"
|
4
|
+
require "net/http"
|
5
|
+
require "cgi"
|
6
|
+
require "bigdecimal"
|
7
|
+
require "dry-monads"
|
8
|
+
require "dialog_lk_esms/types"
|
9
|
+
require "dialog_lk_esms/errors"
|
10
|
+
|
11
|
+
module DialogLkEsms
|
12
|
+
class Client
|
13
|
+
include Dry::Monads[:result]
|
14
|
+
include Dry::Monads[:do]
|
15
|
+
|
16
|
+
STATUS_MESSAGES = {
|
17
|
+
"1" => "Success",
|
18
|
+
"2001" => "Error occurred during campaign creation",
|
19
|
+
"2002" => "Bad request",
|
20
|
+
"2003" => "Empty number list",
|
21
|
+
"2004" => "Empty message body",
|
22
|
+
"2005" => "Invalid number list format",
|
23
|
+
"2006" => "Not eligible to send messages via GET requests",
|
24
|
+
"2007" => "Invalid key (esmsqk parameter is invalid)",
|
25
|
+
"2008" => "Insufficient balance or package quota",
|
26
|
+
"2009" => "No valid numbers after mask-block removal",
|
27
|
+
"2010" => "Not eligible to consume packaging",
|
28
|
+
"2011" => "Transactional error"
|
29
|
+
}
|
30
|
+
|
31
|
+
SendResult = Struct.new(:code, :ok, :message, :raw, :payload, keyword_init: true)
|
32
|
+
BalanceResult = Struct.new(:code, :ok, :message, :raw, :payload, keyword_init: true)
|
33
|
+
|
34
|
+
def initialize(api_key:, base_url: "https://e-sms.dialog.lk/api/v1")
|
35
|
+
@api_key = api_key
|
36
|
+
@base_url = base_url.chomp("/")
|
37
|
+
raise DialogLkEsms::Errors::ConfigurationError, "api_key is required" if @api_key.nil? || @api_key.empty?
|
38
|
+
end
|
39
|
+
|
40
|
+
def send_message(number_list:, message:, source_address:, push_notification_url: nil)
|
41
|
+
numbers = Array(number_list).map(&:to_s).join(",")
|
42
|
+
query = {
|
43
|
+
esmsqk: @api_key,
|
44
|
+
list: numbers,
|
45
|
+
source_address: source_address.to_s,
|
46
|
+
message: message.to_s
|
47
|
+
}
|
48
|
+
# Mirrors PHP sendMessage
|
49
|
+
|
50
|
+
query[:push_notification_url] = push_notification_url.to_s if push_notification_url
|
51
|
+
|
52
|
+
|
53
|
+
endpoint = "/message-via-url/create/url-campaign"
|
54
|
+
raw = yield http_get(endpoint, query)
|
55
|
+
|
56
|
+
|
57
|
+
code = raw.to_s.strip
|
58
|
+
msg = STATUS_MESSAGES.fetch(code, "Unknown response: #{code}")
|
59
|
+
ok = (code == "1")
|
60
|
+
|
61
|
+
|
62
|
+
result = SendResult.new(code: code, ok: ok, message: msg, raw: raw)
|
63
|
+
ok ? Success(result) : Failure(result)
|
64
|
+
end
|
65
|
+
|
66
|
+
|
67
|
+
# Mirrors PHP checkBalance
|
68
|
+
# Returns Result::Success(BalanceResult) with payload: { balance: BigDecimal }
|
69
|
+
# or Failure(BalanceResult)
|
70
|
+
def check_balance
|
71
|
+
endpoint = "/message-via-url/check/balance"
|
72
|
+
query = { esmsqk: @api_key }
|
73
|
+
raw = yield http_get(endpoint, query)
|
74
|
+
|
75
|
+
|
76
|
+
status, balance_str = raw.to_s.split("|", 2)
|
77
|
+
status = status&.strip.to_s
|
78
|
+
|
79
|
+
|
80
|
+
if status.empty?
|
81
|
+
result = BalanceResult.new(code: "parse_error", ok: false, message: "Unknown response or error", raw: raw)
|
82
|
+
return Failure(result)
|
83
|
+
end
|
84
|
+
|
85
|
+
|
86
|
+
if status == "1"
|
87
|
+
bd = coerce_decimal(balance_str)
|
88
|
+
payload = { balance: bd }
|
89
|
+
result = BalanceResult.new(code: status, ok: true, message: "Success", raw: raw, payload: payload)
|
90
|
+
Success(result)
|
91
|
+
else
|
92
|
+
msg = STATUS_MESSAGES.fetch(status, "Unknown response or error")
|
93
|
+
result = BalanceResult.new(code: status, ok: false, message: msg, raw: raw)
|
94
|
+
Failure(result)
|
95
|
+
end
|
96
|
+
rescue => e
|
97
|
+
Failure(BalanceResult.new(code: "exception", ok: false, message: e.message, raw: nil))
|
98
|
+
end
|
99
|
+
|
100
|
+
|
101
|
+
private
|
102
|
+
|
103
|
+
|
104
|
+
def http_get(path, params)
|
105
|
+
uri = URI.join(@base_url + "/", path.sub(%r{^/}, ""))
|
106
|
+
uri.query = URI.encode_www_form(params)
|
107
|
+
|
108
|
+
|
109
|
+
res = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https") do |http|
|
110
|
+
req = Net::HTTP::Get.new(uri)
|
111
|
+
http.request(req)
|
112
|
+
end
|
113
|
+
|
114
|
+
|
115
|
+
if res.is_a?(Net::HTTPSuccess)
|
116
|
+
Success(res.body.to_s)
|
117
|
+
else
|
118
|
+
Failure(DialogLkEsms::Errors::TransportError.new("HTTP #{res.code}: #{res.body}"))
|
119
|
+
end
|
120
|
+
rescue SocketError, Timeout::Error, Errno::ECONNREFUSED => e
|
121
|
+
Failure(DialogLkEsms::Errors::TransportError.new(e.message))
|
122
|
+
end
|
123
|
+
|
124
|
+
|
125
|
+
def coerce_decimal(str)
|
126
|
+
s = (str || "0").to_s.strip
|
127
|
+
BigDecimal(s)
|
128
|
+
rescue ArgumentError
|
129
|
+
BigDecimal("0")
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module DialogLkEsms
|
4
|
+
module Errors
|
5
|
+
class Error < StandardError; end
|
6
|
+
|
7
|
+
class ConfigurationError < Error; end
|
8
|
+
class TransportError < Error; end
|
9
|
+
class ParseError < Error; end
|
10
|
+
class Error < StandardError; end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require "dry-types"
|
2
|
+
|
3
|
+
module DialogLkEsms
|
4
|
+
module Types
|
5
|
+
include Dry.Types()
|
6
|
+
|
7
|
+
PhoneList = Types::Array.of(Types::String.constrained(format: /\A\+?\d{6,15}\z/)).meta(omittable: false)
|
8
|
+
MessageText = Types::String.constrained(min_size: 1)
|
9
|
+
SourceAddr = Types::String.constrained(min_size: 1)
|
10
|
+
|
11
|
+
|
12
|
+
StatusCode = Types::String
|
13
|
+
|
14
|
+
|
15
|
+
Balance = Types::Decimal | Types::Float | Types::Coercible::Decimal | Types::Coercible::Float
|
16
|
+
|
17
|
+
|
18
|
+
ResponseStruct = Types::Hash.schema(
|
19
|
+
code: StatusCode,
|
20
|
+
ok: Types::Bool,
|
21
|
+
message: Types::String,
|
22
|
+
raw: Types::String.optional,
|
23
|
+
payload: Types::Hash.optional
|
24
|
+
)
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "dialog_lk_esms/version"
|
4
|
+
require "dry-configurable"
|
5
|
+
require "dry-types"
|
6
|
+
require "dry-monads"
|
7
|
+
require_relative "dialog_lk_esms/client"
|
8
|
+
require_relative "dialog_lk_esms/errors"
|
9
|
+
require_relative "dialog_lk_esms/types"
|
10
|
+
|
11
|
+
module DialogLkEsms
|
12
|
+
extend Dry::Configurable
|
13
|
+
|
14
|
+
setting :api_key, default: -> { ENV["DIALOG_LK_ESMS_API_KEY"] }, reader: true
|
15
|
+
setting :base_url, default: "https://e-sms.dialog.lk/api/v1", reader: true
|
16
|
+
|
17
|
+
def self.client(api_key:, base_url: "https://e-sms.dialog.lk/api/v1")
|
18
|
+
Client.new api_key: api_key, base_url: base_url
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dialog_lk_esms
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Lahiru Himesh Madusanka
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2025-10-20 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: dry-configurable
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: dry-types
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: dry-monads
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.0'
|
55
|
+
description: Dry‑rb compatible client for Dialog eSMS (URL API)
|
56
|
+
email:
|
57
|
+
- lahiru.lahirumadusanka@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".rubocop.yml"
|
63
|
+
- CHANGELOG.md
|
64
|
+
- CODE_OF_CONDUCT.md
|
65
|
+
- Gemfile
|
66
|
+
- Gemfile.lock
|
67
|
+
- LICENSE.txt
|
68
|
+
- README.md
|
69
|
+
- Rakefile
|
70
|
+
- lib/dialog_lk_esms.rb
|
71
|
+
- lib/dialog_lk_esms/client.rb
|
72
|
+
- lib/dialog_lk_esms/errors.rb
|
73
|
+
- lib/dialog_lk_esms/types.rb
|
74
|
+
- lib/dialog_lk_esms/version.rb
|
75
|
+
- sig/dialog_lk_esms.rbs
|
76
|
+
homepage: https://github.com/sdglhm/dialog_lk_esms
|
77
|
+
licenses:
|
78
|
+
- MIT
|
79
|
+
metadata:
|
80
|
+
allowed_push_host: https://rubygems.org
|
81
|
+
homepage_uri: https://github.com/sdglhm/dialog_lk_esms
|
82
|
+
source_code_uri: https://github.com/sdglhm/dialog_lk_esms
|
83
|
+
changelog_uri: https://github.com/sdglhm/dialog_lk_esms/blob/main/CHANGELOG.md
|
84
|
+
post_install_message:
|
85
|
+
rdoc_options: []
|
86
|
+
require_paths:
|
87
|
+
- lib
|
88
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: 2.6.0
|
93
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
requirements: []
|
99
|
+
rubygems_version: 3.1.6
|
100
|
+
signing_key:
|
101
|
+
specification_version: 4
|
102
|
+
summary: Dry‑rb compatible client for Dialog eSMS (URL API)
|
103
|
+
test_files: []
|