megalabs_sms 0.1.0 → 0.2.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 +4 -4
- data/CHANGELOG.md +35 -0
- data/LICENSE +21 -0
- data/lib/megalabs_sms/version.rb +1 -1
- data/lib/megalabs_sms.rb +23 -14
- data/spec/megalabs_sms/client_spec.rb +26 -6
- metadata +12 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d82e07153abbae70e6bd8cb9a967290b1ec0dc6f29f43a1d3e1f4335b7690883
|
4
|
+
data.tar.gz: f4532bc91e3b879133cb33c9929ceafecd3e42a325146429fc299b620cb72bc5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6bab059a7777d377914b460e34249a04011a98abe3096cafe8a385a5097ad34e0807542e67b3dd1c80b6fc9ad14179f84a26ffeb3dfedbffcc9dcecb70e23782
|
7
|
+
data.tar.gz: 9e23e8a94f9018fef98f1046fc63f1f3f6ca6657adc9daa0c4323b7c32674516799dc8aebce6856254e271fc8d064709c25c79b03482d5842350bc953aace426
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
## [0.2.0] - 2025-03-14
|
4
|
+
|
5
|
+
### Изменено
|
6
|
+
|
7
|
+
- Изменён тип возвращаемого значения метода `send_sms` с String на Boolean для более чёткого определения успешности вызова API
|
8
|
+
- Изменена документация для методов `send_sms`, `handle_stub_response`, `send_request`, `perform_http_request` и `process_http_response`
|
9
|
+
- Обновлены тесты для работы с булевым возвращаемым значением вместо строкового
|
10
|
+
|
11
|
+
## [0.1.1] - 2024-03-13
|
12
|
+
|
13
|
+
### Добавлено
|
14
|
+
|
15
|
+
- Добавлена поддержка GitHub Pages для документации
|
16
|
+
- Добавлена автоматическая генерация документации через GitHub Actions
|
17
|
+
- Добавлены метаданные для RubyGems (documentation_uri, changelog_uri, bug_tracker_uri)
|
18
|
+
|
19
|
+
### Изменено
|
20
|
+
|
21
|
+
- Улучшена структура gemspec
|
22
|
+
- Улучшена документация
|
23
|
+
|
24
|
+
## [0.1.0] - 2025-03-13
|
25
|
+
|
26
|
+
### Инициализация
|
27
|
+
|
28
|
+
- Создан базовый функционал для отправки SMS через API Megalabs
|
29
|
+
- Реализован класс `MegalabsSms::Client` для работы с API
|
30
|
+
- Добавлена базовая аутентификация через API-ключи
|
31
|
+
- Добавлена возможность отправки SMS с указанием отправителя, получателя и текста сообщения
|
32
|
+
- Реализована поддержка тестовых заглушек (stubs) для разработки
|
33
|
+
- Добавлена настраиваемая задержка между запросами
|
34
|
+
- Написаны тесты с использованием RSpec и WebMock
|
35
|
+
- Добавлена базовая документация методов
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2025 Vitalii Dementev
|
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 all
|
13
|
+
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 THE
|
21
|
+
SOFTWARE.
|
data/lib/megalabs_sms/version.rb
CHANGED
data/lib/megalabs_sms.rb
CHANGED
@@ -54,7 +54,7 @@ module MegalabsSms
|
|
54
54
|
# @param to [String] номер телефона получателя
|
55
55
|
# @param message [String] текст сообщения
|
56
56
|
#
|
57
|
-
# @return [
|
57
|
+
# @return [Boolean] true если SMS отправлено успешно, false в случае ошибки
|
58
58
|
#
|
59
59
|
def send_sms(from, to, message)
|
60
60
|
return handle_stub_response if stub_enabled?
|
@@ -77,12 +77,16 @@ module MegalabsSms
|
|
77
77
|
#
|
78
78
|
# Обрабатывает эмуляцию ответов
|
79
79
|
#
|
80
|
-
# @return [
|
80
|
+
# @return [Boolean] true если эмулируется успех, false если эмулируется ошибка
|
81
81
|
#
|
82
82
|
def handle_stub_response
|
83
|
-
|
84
|
-
|
85
|
-
|
83
|
+
if @error_stub
|
84
|
+
log_message('Stubbed error: SMS not sent')
|
85
|
+
false
|
86
|
+
elsif @success_stub
|
87
|
+
log_message('Stubbed success: SMS would be sent')
|
88
|
+
true
|
89
|
+
end
|
86
90
|
end
|
87
91
|
|
88
92
|
#
|
@@ -125,13 +129,13 @@ module MegalabsSms
|
|
125
129
|
#
|
126
130
|
# @param request [Net::HTTP::Post] объект HTTP-запроса
|
127
131
|
#
|
128
|
-
# @return [
|
132
|
+
# @return [Boolean] true если запрос выполнен успешно, false в случае ошибки
|
129
133
|
#
|
130
134
|
def send_request(request)
|
131
135
|
uri = URI('https://a2p-api.megalabs.ru/sms/v1/sms')
|
132
|
-
|
136
|
+
success = perform_http_request(uri, request)
|
133
137
|
sleep(@sleep_time) if @sleep_time.positive?
|
134
|
-
|
138
|
+
success
|
135
139
|
end
|
136
140
|
|
137
141
|
#
|
@@ -140,7 +144,7 @@ module MegalabsSms
|
|
140
144
|
# @param uri [URI] URI-адрес для запроса
|
141
145
|
# @param request [Net::HTTP::Post] объект HTTP-запроса
|
142
146
|
#
|
143
|
-
# @return [
|
147
|
+
# @return [Boolean] true если запрос выполнен успешно, false в случае ошибки
|
144
148
|
#
|
145
149
|
def perform_http_request(uri, request)
|
146
150
|
response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == 'https') do |http|
|
@@ -150,10 +154,11 @@ module MegalabsSms
|
|
150
154
|
if response.is_a?(Net::HTTPSuccess)
|
151
155
|
process_http_response(response.body)
|
152
156
|
else
|
153
|
-
log_message("Failed to send: #{response}")
|
157
|
+
log_message("Failed to send: #{response}") && false
|
154
158
|
end
|
155
159
|
rescue StandardError => e
|
156
160
|
log_message("Exception occurred: #{e.message}")
|
161
|
+
false
|
157
162
|
end
|
158
163
|
|
159
164
|
#
|
@@ -162,17 +167,21 @@ module MegalabsSms
|
|
162
167
|
#
|
163
168
|
# @param raw_body [String] оригинальный ответ от HTTP-запроса
|
164
169
|
#
|
165
|
-
# @return [
|
170
|
+
# @return [Boolean] true если сообщение отправлено успешно, false в случае ошибки
|
166
171
|
#
|
167
172
|
def process_http_response(raw_body)
|
168
173
|
body = raw_body.dup.force_encoding('UTF-8')
|
169
174
|
parsed = JSON.parse(body)
|
170
175
|
result = parsed.dig('result', 'status')
|
171
|
-
|
172
|
-
|
173
|
-
|
176
|
+
success = result&.fetch('code', nil)&.zero? && result&.fetch('description', '')&.downcase == 'ok'
|
177
|
+
if success
|
178
|
+
log_message("Successfully sent: #{body}") && true
|
179
|
+
else
|
180
|
+
log_message("Failed to send: #{body}") && false
|
181
|
+
end
|
174
182
|
rescue JSON::ParserError => e
|
175
183
|
log_message("Failed to parse JSON: #{e.message}")
|
184
|
+
false
|
176
185
|
end
|
177
186
|
end
|
178
187
|
end
|
@@ -27,9 +27,11 @@ RSpec.describe MegalabsSms::Client do
|
|
27
27
|
let(:success_stub) { true }
|
28
28
|
let(:error_stub) { false }
|
29
29
|
|
30
|
-
it 'returns
|
30
|
+
it 'returns true indicating success' do
|
31
|
+
allow(client).to receive(:log_message).and_return(true)
|
31
32
|
result = client.send_sms(from, to, message)
|
32
|
-
expect(result).to eq(
|
33
|
+
expect(result).to eq(true)
|
34
|
+
expect(client).to have_received(:log_message).with('Stubbed success: SMS would be sent')
|
33
35
|
end
|
34
36
|
end
|
35
37
|
|
@@ -37,9 +39,11 @@ RSpec.describe MegalabsSms::Client do
|
|
37
39
|
let(:success_stub) { false }
|
38
40
|
let(:error_stub) { true }
|
39
41
|
|
40
|
-
it 'returns
|
42
|
+
it 'returns false indicating failure' do
|
43
|
+
allow(client).to receive(:log_message).and_return(false)
|
41
44
|
result = client.send_sms(from, to, message)
|
42
|
-
expect(result).to eq(
|
45
|
+
expect(result).to eq(false)
|
46
|
+
expect(client).to have_received(:log_message).with('Stubbed error: SMS not sent')
|
43
47
|
end
|
44
48
|
end
|
45
49
|
end
|
@@ -51,12 +55,14 @@ RSpec.describe MegalabsSms::Client do
|
|
51
55
|
'{"result":{"msg_id":"id","status":{"code":0,"description":"ok"}}}'
|
52
56
|
end
|
53
57
|
|
54
|
-
it 'makes an HTTP request' do
|
58
|
+
it 'makes an HTTP request and returns true on success' do
|
55
59
|
stub_request(:post, 'https://a2p-api.megalabs.ru/sms/v1/sms')
|
56
60
|
.to_return(status: 200, body: valid_response, headers: { 'Content-Type': 'application/json' })
|
57
61
|
|
62
|
+
allow(client).to receive(:log_message).and_return(true)
|
63
|
+
|
58
64
|
result = client.send_sms(from, to, message)
|
59
|
-
expect(result).to eq(
|
65
|
+
expect(result).to eq(true)
|
60
66
|
expect(WebMock).to have_requested(:post, 'https://a2p-api.megalabs.ru/sms/v1/sms')
|
61
67
|
.with(body: {
|
62
68
|
from: from,
|
@@ -65,6 +71,20 @@ RSpec.describe MegalabsSms::Client do
|
|
65
71
|
# rubocop:enable Style/NumericLiterals
|
66
72
|
message: message
|
67
73
|
}.to_json)
|
74
|
+
expect(client).to have_received(:log_message).with("Successfully sent: #{valid_response}")
|
75
|
+
end
|
76
|
+
|
77
|
+
it 'returns false when the API returns an error' do
|
78
|
+
error_response = '{"result":{"msg_id":"id","status":{"code":1,"description":"error"}}}'
|
79
|
+
|
80
|
+
stub_request(:post, 'https://a2p-api.megalabs.ru/sms/v1/sms')
|
81
|
+
.to_return(status: 200, body: error_response, headers: { 'Content-Type': 'application/json' })
|
82
|
+
|
83
|
+
allow(client).to receive(:log_message).and_return(false)
|
84
|
+
|
85
|
+
result = client.send_sms(from, to, message)
|
86
|
+
expect(result).to eq(false)
|
87
|
+
expect(client).to have_received(:log_message).with("Failed to send: #{error_response}")
|
68
88
|
end
|
69
89
|
end
|
70
90
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: megalabs_sms
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Vitalii Dementev
|
8
8
|
bindir: bin
|
9
9
|
cert_chain: []
|
10
|
-
date: 2025-03-
|
10
|
+
date: 2025-03-14 00:00:00.000000000 Z
|
11
11
|
dependencies:
|
12
12
|
- !ruby/object:Gem::Dependency
|
13
13
|
name: net-http
|
@@ -87,15 +87,23 @@ executables: []
|
|
87
87
|
extensions: []
|
88
88
|
extra_rdoc_files: []
|
89
89
|
files:
|
90
|
+
- CHANGELOG.md
|
91
|
+
- LICENSE
|
90
92
|
- README.md
|
91
93
|
- lib/megalabs_sms.rb
|
92
94
|
- lib/megalabs_sms/version.rb
|
93
95
|
- spec/megalabs_sms/client_spec.rb
|
94
96
|
- spec/spec_helper.rb
|
95
|
-
homepage: https://github.
|
97
|
+
homepage: https://dementevvv.github.io/megalabs_sms/
|
96
98
|
licenses:
|
97
99
|
- MIT
|
98
|
-
metadata:
|
100
|
+
metadata:
|
101
|
+
homepage_uri: https://dementevvv.github.io/megalabs_sms/
|
102
|
+
documentation_uri: https://dementevvv.github.io/megalabs_sms/
|
103
|
+
source_code_uri: https://github.com/DementevVV/megalabs_sms
|
104
|
+
changelog_uri: https://github.com/DementevVV/megalabs_sms/blob/master/CHANGELOG.md
|
105
|
+
bug_tracker_uri: https://github.com/DementevVV/megalabs_sms/issues
|
106
|
+
rubygems_mfa_required: 'true'
|
99
107
|
rdoc_options: []
|
100
108
|
require_paths:
|
101
109
|
- lib
|