sms-pilot-api-v1 0.0.7 → 0.0.8

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: 398583e8c6b3d5ab1ffa33d2e85a412fd779d3332775980d447e2354cdfaf19e
4
- data.tar.gz: dfc1c5f424e7c95e9a0829883b701d66da85134d18dc2267e76f3db7756d357a
3
+ metadata.gz: fb76209647bdda0c204159de74aeb9afd98cb01b6d8979ae9857407e76ff61b1
4
+ data.tar.gz: 6bcea2c04668198bcbf07babcfec333e90e1cb920a06eb9cb371d2a291f1ca66
5
5
  SHA512:
6
- metadata.gz: d48222754c8acd857fa867385250ce8de32d92c10e624dfef768077f6399eb289258ddb89137d327a243e5d7a59e4527509c7bfb186fed885e6452284030b079
7
- data.tar.gz: f5d14f8fc7558024c8fa5a49a3b2e270487de1cd6d2d4e8aa93d45c10e4d948837c8ac4dc13d85e115ae2ce24e956eaefb3ae43d943bfe2732eee93478a55be6
6
+ metadata.gz: b21cecdb44421ffe495e0963b9928dedab5e8740680f94937b85eceb1de256bdef23d967e8012e46c124f3a24f27f3fb8eedd541391964ec6dcdd83c21af7300
7
+ data.tar.gz: a693db49eac9e534a6be8c3132804376783a42c56ae532782cd6459f9c516c6f4610ff234d4a9cb522fe30019c64724c042a649e43a475c6c10bbe7e65baf27e
data/README.md CHANGED
@@ -44,11 +44,16 @@ client = SmsPilot::Client.new(api_key: key)
44
44
  client = SmsPilot::Client.new(api_key: key, locale: :en) # Available locales are [:en, :ru]
45
45
  ```
46
46
 
47
+ Method [documentation](https://rubydoc.info/github/sergeypedan/sms-pilot-api-v1/master/SmsPilot/Client#initialize-instance_method) at RubyDoc.
48
+
47
49
  ### Before sending
48
50
 
51
+ There are a bunch of methods describing the state of affairs:
52
+
49
53
  ```ruby
50
54
  client.api_key # => "YOUR API KEY"
51
55
  client.balance # => nil
56
+ client.broadcast_id # => nil
52
57
  client.error # => nil
53
58
  client.phone # => nil
54
59
  client.rejected? # => false
@@ -63,6 +68,10 @@ client.sms_status # => nil
63
68
  client.url # => nil
64
69
  ```
65
70
 
71
+ before the request is sent they return obvious nils or empty structures; after the request they are populated with data.
72
+
73
+ See [structured documentation](https://rubydoc.info/github/sergeypedan/sms-pilot-api-v1/master/SmsPilot/Client) for those methods at RubyDoc.
74
+
66
75
  ### Sending SMS
67
76
 
68
77
  ```ruby
@@ -72,12 +81,14 @@ client.send_sms("+7 (902) 123-45-67", "Привет, мир!")
72
81
 
73
82
  Returns result of `sms_sent?`, so it’s either `true` or `false`.
74
83
 
84
+ Method [documentation](https://rubydoc.info/github/sergeypedan/sms-pilot-api-v1/master/SmsPilot/Client#send_sms-instance_method) at RubyDoc.
75
85
 
76
86
  ### Sending SMS succeeded
77
87
 
78
88
  ```ruby
79
89
  client.api_key # => "YOUR API KEY"
80
90
  client.balance # => 20006.97
91
+ client.broadcast_id # => 10000
81
92
  client.error # => nil
82
93
  client.phone # => "79021234567"
83
94
  client.rejected? # => false
@@ -97,6 +108,7 @@ client.url # => "https://smspilot.ru/api.php?apikey=1234567890&form
97
108
  ```ruby
98
109
  client.api_key # => "YOUR API KEY"
99
110
  client.balance # => nil
111
+ client.broadcast_id # => 10000
100
112
  client.error # => "Неправильный API-ключ (см. настройки API в личном кабинете) (код ошибки: 101)"
101
113
  client.phone # => "79021234567"
102
114
  client.rejected? # => true
@@ -116,6 +128,7 @@ client.url # => "https://smspilot.ru/api.php?apikey=1234567890&form
116
128
  ```ruby
117
129
  client.api_key # => "YOUR API KEY"
118
130
  client.balance # => nil
131
+ client.broadcast_id # => 10000
119
132
  client.error # => "HTTP request failed with code 404"
120
133
  client.phone # => "79021234567"
121
134
  client.rejected? # => false
@@ -133,9 +146,9 @@ client.url # => "https://smspilot.ru/api.php?apikey=1234567890&form
133
146
 
134
147
  ## SMS pilot API docs
135
148
 
136
- - [web version](https://smspilot.ru/apikey.php) — см. вкладку PHP, в остальных ничего нет
149
+ - [Web version](https://smspilot.ru/apikey.php) — см. вкладку PHP, в остальных ничего нет
137
150
  - [PDF version](https://smspilot.ru/download/SMSPilotRu-HTTP-v1.9.19.pdf) — тут намного подробнее
138
- - [Коды ошибок](https://smspilot.ru/apikey.php#err)
151
+ - [API error code](https://smspilot.ru/apikey.php#err)
139
152
 
140
153
 
141
154
  ## Test API key
@@ -172,3 +185,8 @@ SMS rejected:
172
185
  }
173
186
  }
174
187
  ```
188
+
189
+
190
+ ## Documentation
191
+
192
+ See [structured documentation](https://rubydoc.info/github/sergeypedan/sms-pilot-api-v1/master/SmsPilot/Client) at RubyDoc.
@@ -7,7 +7,9 @@ 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
+ # @example
12
+ # client.api_key #=> "XXX..."
11
13
  #
12
14
  # @!attribute [r] error
13
15
  # Error message returned from the API, combined with the error code
@@ -18,10 +20,16 @@ module SmsPilot
18
20
  # @see #error_description
19
21
  #
20
22
  # @!attribute [r] locale
21
- # @return [Symbol] Chosen locale (affects only the language of errors)
23
+ # Chosen locale (affects only the language of errors)
24
+ #
25
+ # @return [Symbol]
26
+ # @example
27
+ # client.locale #=> :ru
22
28
  #
23
29
  # @!attribute [r] phone
24
30
  # @return [nil, String] phone after normalization
31
+ # @example
32
+ # client.phone #=> "79021234567"
25
33
  #
26
34
  # @!attribute [r] response_body
27
35
  # Response format is JSON (because we request it that way in {#build_uri}).
@@ -34,6 +42,7 @@ module SmsPilot
34
42
  #
35
43
  # @!attribute [r] response_headers
36
44
  # @example
45
+ # client.response_headers #=>
37
46
  # {
38
47
  # "Access-Control-Allow-Origin" => "*",
39
48
  # "Connection" => "close",
@@ -57,16 +66,14 @@ module SmsPilot
57
66
  # @see #response_data
58
67
  # @see #response_headers
59
68
  #
60
- # @!attribute [r] url
61
- # @example
62
- # client.url #=> "https://smspilot.ru/api.php?api_key=XXX&format=json&send=TEXT&to=79021234567"
63
- # @return [String] URL generated by combining <tt>API_ENDPOINT</tt>, your API key, SMS text & phone
64
- #
65
69
  class Client
66
70
 
67
71
  # Check current API endpoint URL at {https://smspilot.ru/apikey.php#api1}
68
72
  #
69
73
  API_ENDPOINT = "https://smspilot.ru/api.php".freeze
74
+
75
+ # Locale influences only the language of API errors
76
+ #
70
77
  AVAILABLE_LOCALES = [:ru, :en].freeze
71
78
 
72
79
  attr_reader :api_key
@@ -76,7 +83,6 @@ module SmsPilot
76
83
  attr_reader :response_body
77
84
  attr_reader :response_headers
78
85
  attr_reader :response_status
79
- attr_reader :url
80
86
 
81
87
 
82
88
  # @param api_key [String]
@@ -101,7 +107,6 @@ module SmsPilot
101
107
  @response_status = nil
102
108
  @response_headers = {}
103
109
  @response_body = nil
104
- @url = nil
105
110
  end
106
111
 
107
112
 
@@ -119,6 +124,7 @@ module SmsPilot
119
124
  # @raise [SmsPilot::InvalidMessageError] if your message is empty
120
125
  # @raise [SmsPilot::InvalidPhoneError] if your phone is empty
121
126
  # @raise [SmsPilot::InvalidPhoneError] if your phone has no digits
127
+ # @raise [URI::InvalidURIError] but is almost impossible, because we provide the URL ourselves
122
128
  #
123
129
  # @example
124
130
  # client.send_sms("+7 (902) 123-45-67", "Привет, мир!") # => true
@@ -128,14 +134,9 @@ module SmsPilot
128
134
  validate_message! message
129
135
 
130
136
  @phone = normalize_phone(phone)
131
- uri = build_uri(@phone, message)
132
- @url = uri.to_s
137
+ @uri = build_uri(@phone, message)
133
138
 
134
- response = Net::HTTP.get_response(uri)
135
-
136
- @response_body = response.body
137
- @response_status = response.code.to_i
138
- @response_headers = response.each_capitalized.to_h
139
+ response = persist_response_details Net::HTTP.get_response(@uri)
139
140
 
140
141
  @error = "HTTP request failed with code #{response.code}" and return false unless response.is_a?(Net::HTTPSuccess)
141
142
  @error = "#{error_description} (error code: #{error_code})" and return false if rejected?
@@ -169,6 +170,20 @@ module SmsPilot
169
170
  end
170
171
 
171
172
 
173
+ # SMS broadcast ID (API documentation calls it “server ID” but it makes no sense, as it is clearly the ID of the transmission, not of a server)
174
+ #
175
+ # @example
176
+ # client.broadcast_id #=> 10000
177
+ #
178
+ # @return [nil, Integer]
179
+ #
180
+ # @see #response_data
181
+ #
182
+ def broadcast_id
183
+ @response_data.dig("send", 0, "server_id")&.to_i if sms_sent?
184
+ end
185
+
186
+
172
187
  # Numerical code of the error that occured when sending the SMS. In the range from 0 to 715 (which may change).
173
188
  #
174
189
  # @return [nil, Integer] <tt>nil</tt> is returned before sending SMS. Otherwise <tt>Integer</tt>
@@ -306,6 +321,18 @@ module SmsPilot
306
321
  @response_data.dig("send", 0, "status")&.to_i if sms_sent?
307
322
  end
308
323
 
324
+
325
+ # URL generated by combining <tt>API_ENDPOINT</tt>, your API key, SMS text & phone
326
+ #
327
+ # @example
328
+ # client.url #=> "https://smspilot.ru/api.php?api_key=XXX&format=json&send=TEXT&to=79021234567"
329
+ #
330
+ # @return [nil, String]
331
+ #
332
+ def url
333
+ @uri&.to_s
334
+ end
335
+
309
336
  # @!endgroup
310
337
 
311
338
 
@@ -317,7 +344,7 @@ module SmsPilot
317
344
  # #=> #<URI::HTTPS https://smspilot.ru/api.php?apikey=XXX…&format=json&send=Hello%2C+World%21&to=79021234567>
318
345
  #
319
346
  # @return [URI]
320
- # @raise [URI::InvalidURIError] but is very unlikely because we provide the URL ourselves
347
+ # @raise [URI::InvalidURIError] but is almost impossible, because we provide the URL ourselves
321
348
  #
322
349
  # @see #api_key
323
350
  # @see #phone
@@ -346,6 +373,21 @@ module SmsPilot
346
373
  end
347
374
 
348
375
 
376
+ # Saves response details into instance variables
377
+ # @private
378
+ #
379
+ # @return [response]
380
+ # @raise [TypeError] unless a Net::HTTPResponse passed
381
+ #
382
+ private def persist_response_details(response)
383
+ fail TypeError, "Net::HTTPResponse expected, you pass a #{response.class}" unless response.is_a? Net::HTTPResponse
384
+ @response_body = response.body
385
+ @response_status = response.code.to_i
386
+ @response_headers = response.each_capitalized.to_h
387
+ response
388
+ end
389
+
390
+
349
391
  # @!group Validations
350
392
 
351
393
  # Validates api_key
@@ -353,7 +395,7 @@ module SmsPilot
353
395
  # @private
354
396
  # @return [String] the original value passed into the method, only if it was valid
355
397
  # @param [String] api_key
356
-
398
+ #
357
399
  # @raise [SmsPilot::InvalidError] if api_key is not a String
358
400
  # @raise [SmsPilot::InvalidError] if api_key is an empty String
359
401
  #
@@ -369,7 +411,7 @@ module SmsPilot
369
411
  # @private
370
412
  # @return [Symbol] the original value passed into the method, only if it was valid
371
413
  # @param [Symbol] locale
372
-
414
+ #
373
415
  # @raise [SmsPilot::InvalidError] if locale is not a Symbol
374
416
  # @raise [SmsPilot::InvalidError] if locale is unrecognized
375
417
  #
@@ -3,6 +3,6 @@
3
3
  module SmsPilot
4
4
 
5
5
  # Gem version
6
- VERSION = "0.0.7"
6
+ VERSION = "0.0.8"
7
7
 
8
8
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sms-pilot-api-v1
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sergey Pedan
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-05-09 00:00:00.000000000 Z
11
+ date: 2021-05-10 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Simple wrapper around SMS pilot API v1. Version 1 because it returns
14
14
  more data within its standard response
@@ -19,7 +19,6 @@ extensions: []
19
19
  extra_rdoc_files: []
20
20
  files:
21
21
  - ".gitignore"
22
- - ".inch.yml"
23
22
  - ".rspec"
24
23
  - ".yardopts"
25
24
  - CHANGELOG.md
data/.inch.yml DELETED
@@ -1,11 +0,0 @@
1
- files:
2
-
3
- # defaults to ["{app,lib}/**/*.rb"]
4
- included:
5
- - lib/sms_pilot/client.rb
6
-
7
- # defaults to []
8
- excluded:
9
- - lib/sms_pilot.rb
10
- - lib/sms_pilot/errors.rb
11
- - lib/sms_pilot/version.rb