sms-pilot-api-v1 0.0.4 → 0.0.5
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 +2 -2
- data/README.md +1 -0
- data/lib/sms_pilot.rb +0 -1
- data/lib/sms_pilot/client.rb +60 -17
- data/lib/sms_pilot/errors.rb +1 -0
- data/lib/sms_pilot/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2f18c87d0d06449a2d1990eefe98e92a66e2e543e6d37cadb8a59597fb165232
|
4
|
+
data.tar.gz: 27b96c2afab4411c63474d807692336d32e95196d1172114f5ffaf9ddbb6d650
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5f59608daac142c675c71485c2ab63fa0a1291ebecc887f939a3a272d586a806f8cea5068174064ba9ff193172f0975836512136ac3a75f7b843f3afb1ab113e
|
7
|
+
data.tar.gz: 9aeae9908fd518c5156b472f27c618f57c48d1370e09a2ffa8ab40430c083a908bc684fc15e3a1e76397f1d47b66c4c43c7c87da0fa89651a7006e5210317ddd
|
data/CHANGELOG.md
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
-
## [0.0.4] - 2021
|
3
|
+
## [0.0.4] - 9 May 2021
|
4
4
|
|
5
5
|
- Drop dependence on HTTP.rb gem
|
6
6
|
- Corrects what `#send_sms` returns (could return String errors instead of Booleans)
|
7
7
|
- Adds extensive [documentation](https://rubydoc.info/github/sergeypedan/sms-pilot-api-v1/master/SmsPilot/Client) via YARD & RubyDoc
|
8
8
|
|
9
|
-
## [0.0.3] - 2021
|
9
|
+
## [0.0.3] - 6 May 2021
|
10
10
|
|
11
11
|
- Initial release
|
data/README.md
CHANGED
@@ -33,6 +33,7 @@ bin/console
|
|
33
33
|
require "sms_pilot"
|
34
34
|
|
35
35
|
client = SmsPilot::Client.new(api_key: "XXXXXXXXXXXXYYYYYYYYYYYYZZZZZZZZXXXXXXXXXXXXYYYYYYYYYYYYZZZZZZZZ")
|
36
|
+
client = SmsPilot::Client.new(api_key: "XXXXXXXXXXXXYYYYYYYYYYYYZZZZZZZZXXXXXXXXXXXXYYYYYYYYYYYYZZZZZZZZ", locale: :en) # Available locales are [:en, :ru]
|
36
37
|
#<SmsPilot::Client:0x00007fb1c602d490 @api_key="XXXXX...", @error=nil, @response_status=nil, @response_headers=nil, @response_body=nil, @response_data={}, @url=nil>
|
37
38
|
```
|
38
39
|
|
data/lib/sms_pilot.rb
CHANGED
data/lib/sms_pilot/client.rb
CHANGED
@@ -7,7 +7,7 @@ require "uri"
|
|
7
7
|
module SmsPilot
|
8
8
|
|
9
9
|
# @!attribute [r] api_key
|
10
|
-
# @return [String] Your API key
|
10
|
+
# @return [String] Your API key
|
11
11
|
#
|
12
12
|
# @!attribute [r] error
|
13
13
|
# Error message returned from the API, combined with the error code
|
@@ -17,6 +17,12 @@ module SmsPilot
|
|
17
17
|
# @see #error_code
|
18
18
|
# @see #error_description
|
19
19
|
#
|
20
|
+
# @!attribute [r] locale
|
21
|
+
# @return [Symbol] Chosen locale (affects only the language of errors)
|
22
|
+
#
|
23
|
+
# @!attribute [r] phone
|
24
|
+
# @return [nil, String] phone after normalization
|
25
|
+
#
|
20
26
|
# @!attribute [r] response_body
|
21
27
|
# Response format is JSON (because we request it that way in {#build_uri}.
|
22
28
|
# @example
|
@@ -81,9 +87,11 @@ module SmsPilot
|
|
81
87
|
# Check current API endpoint URL at {https://smspilot.ru/apikey.php#api1}
|
82
88
|
#
|
83
89
|
API_ENDPOINT = "https://smspilot.ru/api.php".freeze
|
90
|
+
AVAILABLE_LOCALES = [:ru, :en].freeze
|
84
91
|
|
85
92
|
attr_reader :api_key
|
86
93
|
attr_reader :error
|
94
|
+
attr_reader :locale
|
87
95
|
attr_reader :phone
|
88
96
|
attr_reader :response_body
|
89
97
|
attr_reader :response_data
|
@@ -102,12 +110,10 @@ module SmsPilot
|
|
102
110
|
# @example
|
103
111
|
# client = SmsPilot::Client.new(api_key: ENV["SMS_PILOT_API_KEY"])
|
104
112
|
#
|
105
|
-
def initialize(api_key:)
|
106
|
-
|
107
|
-
fail SmsPilot::InvalidAPIkeyError, "API key cannot be empty" if api_key == ""
|
108
|
-
|
109
|
-
@api_key = api_key
|
113
|
+
def initialize(api_key:, locale: AVAILABLE_LOCALES[0])
|
114
|
+
@api_key = validate_api_key!(api_key)
|
110
115
|
@error = nil
|
116
|
+
@locale = validate_locale!(locale)
|
111
117
|
@response_status = nil
|
112
118
|
@response_headers = {}
|
113
119
|
@response_body = nil
|
@@ -213,7 +219,8 @@ module SmsPilot
|
|
213
219
|
# @see https://smspilot.ru/apikey.php#err Error codes at the API documentation website
|
214
220
|
#
|
215
221
|
def error_description
|
216
|
-
@
|
222
|
+
method_name = (@locale == :ru) ? "description_ru" : "description"
|
223
|
+
@response_data.dig("error", method_name) if rejected?
|
217
224
|
end
|
218
225
|
|
219
226
|
|
@@ -330,24 +337,42 @@ module SmsPilot
|
|
330
337
|
end
|
331
338
|
|
332
339
|
|
333
|
-
#
|
340
|
+
# @!group Validations
|
334
341
|
|
342
|
+
# Validates api_key
|
343
|
+
#
|
335
344
|
# @private
|
336
|
-
# @return [
|
345
|
+
# @return [String] the original value passed into the method, only if it was valid
|
346
|
+
# @param [String] api_key
|
347
|
+
|
348
|
+
# @raise [SmsPilot::InvalidError] if api_key is not a String
|
349
|
+
# @raise [SmsPilot::InvalidError] if api_key is an empty String
|
337
350
|
#
|
338
|
-
|
339
|
-
|
340
|
-
|
351
|
+
private def validate_api_key!(api_key)
|
352
|
+
fail SmsPilot::InvalidAPIkeyError, "API key must be a String, you pass a #{api_key.class} (#{api_key})" unless api_key.is_a? String
|
353
|
+
fail SmsPilot::InvalidAPIkeyError, "API key cannot be empty" if api_key == ""
|
354
|
+
return api_key
|
355
|
+
end
|
356
|
+
|
357
|
+
|
358
|
+
# Validates locale
|
341
359
|
#
|
342
|
-
private
|
343
|
-
|
344
|
-
|
345
|
-
|
360
|
+
# @private
|
361
|
+
# @return [Symbol] the original value passed into the method, only if it was valid
|
362
|
+
# @param [Symbol] locale
|
363
|
+
|
364
|
+
# @raise [SmsPilot::InvalidError] if locale is not a Symbol
|
365
|
+
# @raise [SmsPilot::InvalidError] if locale is unrecognized
|
366
|
+
#
|
367
|
+
private def validate_locale!(locale)
|
368
|
+
fail SmsPilot::InvalidLocaleError, "locale must be a Symbol" unless locale.is_a? Symbol
|
369
|
+
fail SmsPilot::InvalidLocaleError, "API does not support locale :#{locale}; choose one of #{AVAILABLE_LOCALES.inspect}" unless AVAILABLE_LOCALES.include? locale
|
370
|
+
return locale
|
346
371
|
end
|
347
372
|
|
348
373
|
|
349
374
|
# Validates message
|
350
|
-
|
375
|
+
#
|
351
376
|
# @private
|
352
377
|
# @return [nil]
|
353
378
|
#
|
@@ -359,5 +384,23 @@ module SmsPilot
|
|
359
384
|
fail SmsPilot::InvalidMessageError, "SMS message cannot be empty" if message == ""
|
360
385
|
end
|
361
386
|
|
387
|
+
|
388
|
+
# Validates phone
|
389
|
+
#
|
390
|
+
# @private
|
391
|
+
# @return [nil]
|
392
|
+
#
|
393
|
+
# @raise [SmsPilot::InvalidPhoneError] if you pass anythig but a String with the <tt>phone</tt> argument
|
394
|
+
# @raise [SmsPilot::InvalidPhoneError] if your phone is empty
|
395
|
+
# @raise [SmsPilot::InvalidPhoneError] if your phone has no digits
|
396
|
+
#
|
397
|
+
private def validate_phone!(phone)
|
398
|
+
fail SmsPilot::InvalidPhoneError, "phone must be a String, you pass a #{phone.class} (#{phone})" unless phone.is_a? String
|
399
|
+
fail SmsPilot::InvalidPhoneError, "phone cannot be empty" if phone == ""
|
400
|
+
fail SmsPilot::InvalidPhoneError, "phone must contain digits" if phone.scan(/\d/).none?
|
401
|
+
end
|
402
|
+
|
403
|
+
# @!endgroup
|
404
|
+
|
362
405
|
end
|
363
406
|
end
|
data/lib/sms_pilot/errors.rb
CHANGED
data/lib/sms_pilot/version.rb
CHANGED