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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e1438f40054c6f5347c861fddb4530b2f86f7321ef4998eba159c18e70db520c
4
- data.tar.gz: fccbb5e7fb2c505e5a8bd80cc9e1410db5673b71aa2ac6ffac0bf41a89610797
3
+ metadata.gz: 2f18c87d0d06449a2d1990eefe98e92a66e2e543e6d37cadb8a59597fb165232
4
+ data.tar.gz: 27b96c2afab4411c63474d807692336d32e95196d1172114f5ffaf9ddbb6d650
5
5
  SHA512:
6
- metadata.gz: 86070669bff561d19b18242c595acc0bfc22ea6607bead4456e74391671e143ee6f1e269b1d4cd989d751d3ffda4c68fd85144d331a7e0499a64d7c114e344be
7
- data.tar.gz: '028935012e1fd06b533aa264d8d46f0eb87ee245fc08fe5bad688c89ceb2db77610ae75caea32a3193145d0d442f4f2d32f31e1b218a6951ef78e49c6b40e558'
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-05-09
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-05-06
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
@@ -3,4 +3,3 @@
3
3
  require_relative "sms_pilot/version"
4
4
  require_relative "sms_pilot/errors"
5
5
  require_relative "sms_pilot/client"
6
-
@@ -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
- fail SmsPilot::InvalidAPIkeyError, "API key must be a String, you pass a #{api_key.class} (#{api_key})" unless api_key.is_a? String
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
- @response_data.dig("error", "description_ru") if rejected?
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
- # Validates phone
340
+ # @!group Validations
334
341
 
342
+ # Validates api_key
343
+ #
335
344
  # @private
336
- # @return [nil]
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
- # @raise [SmsPilot::InvalidPhoneError] if you pass anythig but a String with the <tt>phone</tt> argument
339
- # @raise [SmsPilot::InvalidPhoneError] if your phone is empty
340
- # @raise [SmsPilot::InvalidPhoneError] if your phone has no digits
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 def validate_phone!(phone)
343
- fail SmsPilot::InvalidPhoneError, "phone must be a String, you pass a #{phone.class} (#{phone})" unless phone.is_a? String
344
- fail SmsPilot::InvalidPhoneError, "phone cannot be empty" if phone == ""
345
- fail SmsPilot::InvalidPhoneError, "phone must contain digits" if phone.scan(/\d/).none?
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
@@ -5,5 +5,6 @@ module SmsPilot
5
5
  class InvalidAPIkeyError < ArgumentError; end
6
6
  class InvalidMessageError < ArgumentError; end
7
7
  class InvalidPhoneError < ArgumentError; end
8
+ class InvalidLocaleError < ArgumentError; end
8
9
 
9
10
  end
@@ -3,6 +3,6 @@
3
3
  module SmsPilot
4
4
 
5
5
  # Gem version
6
- VERSION = "0.0.4"
6
+ VERSION = "0.0.5"
7
7
 
8
8
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sms-pilot-api-v1
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sergey Pedan