megalabs_sms 0.1.1 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6e446dd9f5f499406d31feb11b439f9a2a70801655e93568a57377cedd143949
4
- data.tar.gz: c0708973c1a366d0589f39ff6382ebf87489adfd7869200811fe708ca6ffa2a9
3
+ metadata.gz: d82e07153abbae70e6bd8cb9a967290b1ec0dc6f29f43a1d3e1f4335b7690883
4
+ data.tar.gz: f4532bc91e3b879133cb33c9929ceafecd3e42a325146429fc299b620cb72bc5
5
5
  SHA512:
6
- metadata.gz: 0b4c585530127f78b2df5311ec6c7ae9bebc6111ea048a6e9b04606cd9204b2364853259f7d159f86d25964fd1196857c0e1769307799a4ce07b886ae95f662d
7
- data.tar.gz: 2daf04602c196d85edbc7883a59f02ffb665da4ce0a8d0b900b7c4dae4ad8e96859d8a2ce1c861cc38fd0870d6ded9b445b2407ee36a19fb9d9d7f73b412df4d
6
+ metadata.gz: 6bab059a7777d377914b460e34249a04011a98abe3096cafe8a385a5097ad34e0807542e67b3dd1c80b6fc9ad14179f84a26ffeb3dfedbffcc9dcecb70e23782
7
+ data.tar.gz: 9e23e8a94f9018fef98f1046fc63f1f3f6ca6657adc9daa0c4323b7c32674516799dc8aebce6856254e271fc8d064709c25c79b03482d5842350bc953aace426
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # Changelog
2
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
+
3
11
  ## [0.1.1] - 2024-03-13
4
12
 
5
13
  ### Добавлено
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MegalabsSms
4
- VERSION = '0.1.1'
4
+ VERSION = '0.2.0'
5
5
  end
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 [String] статус отправки или текст ошибки
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 [String] сообщение об успехе или ошибке эмуляции
80
+ # @return [Boolean] true если эмулируется успех, false если эмулируется ошибка
81
81
  #
82
82
  def handle_stub_response
83
- return log_message('Stubbed error: SMS not sent') if @error_stub
84
-
85
- log_message('Stubbed success: SMS would be sent') if @success_stub
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 [String] тело ответа или сообщение об ошибке
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
- response_body = perform_http_request(uri, request)
136
+ success = perform_http_request(uri, request)
133
137
  sleep(@sleep_time) if @sleep_time.positive?
134
- response_body
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 [String] тело ответа или сообщение об ошибке
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 [String] либо оригинальное тело (если все хорошо), либо сообщение об ошибке
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
- return body if result&.fetch('code', nil)&.zero? && result&.fetch('description', '')&.downcase == 'ok'
172
-
173
- log_message("Failed to send: #{body}")
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 a success message (stub)' do
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('[MegalabsSms] Stubbed success: SMS would be sent')
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 an error message (stub)' do
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('[MegalabsSms] Stubbed error: SMS not sent')
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(valid_response)
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.1.1
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-13 00:00:00.000000000 Z
10
+ date: 2025-03-14 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: net-http