sms-pilot-api-v1 0.0.6 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.inch.yml +11 -0
- data/CHANGELOG.md +8 -0
- data/Gemfile.lock +2 -2
- data/README.md +3 -0
- data/lib/sms_pilot.rb +5 -0
- data/lib/sms_pilot/client.rb +54 -46
- data/lib/sms_pilot/version.rb +1 -1
- data/sms-pilot-api-v1.gemspec +7 -4
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 398583e8c6b3d5ab1ffa33d2e85a412fd779d3332775980d447e2354cdfaf19e
|
4
|
+
data.tar.gz: dfc1c5f424e7c95e9a0829883b701d66da85134d18dc2267e76f3db7756d357a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d48222754c8acd857fa867385250ce8de32d92c10e624dfef768077f6399eb289258ddb89137d327a243e5d7a59e4527509c7bfb186fed885e6452284030b079
|
7
|
+
data.tar.gz: f5d14f8fc7558024c8fa5a49a3b2e270487de1cd6d2d4e8aa93d45c10e4d948837c8ac4dc13d85e115ae2ce24e956eaefb3ae43d943bfe2732eee93478a55be6
|
data/.inch.yml
ADDED
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,13 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
+
## [0.0.7] - 9 May 2021
|
4
|
+
|
5
|
+
- Returns original values from validation methods
|
6
|
+
- Offloads parsing response body to a method
|
7
|
+
- Improves documentation
|
8
|
+
- Adds CodeClimate badges
|
9
|
+
- Writes tests for `#initialize` and `#api_key`
|
10
|
+
|
3
11
|
## [0.0.6] - 9 May 2021
|
4
12
|
|
5
13
|
- Corrects cost type
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,9 @@
|
|
1
1
|
# SmsPilot API v1 client
|
2
2
|
|
3
3
|
[![Gem Version](https://badge.fury.io/rb/sms-pilot-api-v1.svg)](https://badge.fury.io/rb/sms-pilot-api-v1)
|
4
|
+
[![Maintainability](https://api.codeclimate.com/v1/badges/42765c3098d5f531a3f7/maintainability)](https://codeclimate.com/github/sergeypedan/sms-pilot-api-v1/maintainability)
|
5
|
+
[![Test Coverage](https://api.codeclimate.com/v1/badges/42765c3098d5f531a3f7/test_coverage)](https://codeclimate.com/github/sergeypedan/sms-pilot-api-v1/test_coverage)
|
6
|
+
[![Inch CI documentation](https://inch-ci.org/github/sergeypedan/sms-pilot-api-v1.svg?branch=master&style=flat)](https://inch-ci.org/github/sergeypedan/sms-pilot-api-v1)
|
4
7
|
|
5
8
|
Simple wrapper around SMS pilot API v1. Version 1 because it returns more data within its standard response.
|
6
9
|
|
data/lib/sms_pilot.rb
CHANGED
data/lib/sms_pilot/client.rb
CHANGED
@@ -24,7 +24,7 @@ module SmsPilot
|
|
24
24
|
# @return [nil, String] phone after normalization
|
25
25
|
#
|
26
26
|
# @!attribute [r] response_body
|
27
|
-
# Response format is JSON (because we request it that way in {#build_uri}.
|
27
|
+
# Response format is JSON (because we request it that way in {#build_uri}).
|
28
28
|
# @example
|
29
29
|
# "{\"send\":[{\"server_id\":\"10000\",\"phone\":\"79021234567\",\"price\":\"1.68\",\"status\":\"0\"}],\"balance\":\"20006.97\",\"cost\":\"1.68\"}"
|
30
30
|
# @return [nil, String] Unmodified HTTP resonse body that API returned
|
@@ -32,26 +32,6 @@ module SmsPilot
|
|
32
32
|
# @see #response_headers
|
33
33
|
# @see #response_status
|
34
34
|
#
|
35
|
-
# @!attribute [r] response_data
|
36
|
-
# Parsed <tt>@response_body</tt>. May be an empty <tt>Hash</tt> if parsing fails.
|
37
|
-
# @example
|
38
|
-
# {
|
39
|
-
# "balance" => "20006.97",
|
40
|
-
# "cost" => "1.68",
|
41
|
-
# "send" => [
|
42
|
-
# {
|
43
|
-
# "phone" => "79021234567",
|
44
|
-
# "price" => "1.68",
|
45
|
-
# "server_id" => "10000",
|
46
|
-
# "status" => "0"
|
47
|
-
# }
|
48
|
-
# ]
|
49
|
-
# }
|
50
|
-
# @return [Hash]
|
51
|
-
# @see #response_body
|
52
|
-
# @see #response_headers
|
53
|
-
# @see #response_status
|
54
|
-
#
|
55
35
|
# @!attribute [r] response_headers
|
56
36
|
# @example
|
57
37
|
# {
|
@@ -94,7 +74,6 @@ module SmsPilot
|
|
94
74
|
attr_reader :locale
|
95
75
|
attr_reader :phone
|
96
76
|
attr_reader :response_body
|
97
|
-
attr_reader :response_data
|
98
77
|
attr_reader :response_headers
|
99
78
|
attr_reader :response_status
|
100
79
|
attr_reader :url
|
@@ -122,7 +101,6 @@ module SmsPilot
|
|
122
101
|
@response_status = nil
|
123
102
|
@response_headers = {}
|
124
103
|
@response_body = nil
|
125
|
-
@response_data = {}
|
126
104
|
@url = nil
|
127
105
|
end
|
128
106
|
|
@@ -159,19 +137,10 @@ module SmsPilot
|
|
159
137
|
@response_status = response.code.to_i
|
160
138
|
@response_headers = response.each_capitalized.to_h
|
161
139
|
|
162
|
-
unless response.is_a?(Net::HTTPSuccess)
|
163
|
-
|
164
|
-
return false
|
165
|
-
end
|
140
|
+
@error = "HTTP request failed with code #{response.code}" and return false unless response.is_a?(Net::HTTPSuccess)
|
141
|
+
@error = "#{error_description} (error code: #{error_code})" and return false if rejected?
|
166
142
|
|
167
|
-
|
168
|
-
|
169
|
-
if rejected?
|
170
|
-
@error = "#{error_description} (error code: #{error_code})"
|
171
|
-
return false
|
172
|
-
end
|
173
|
-
|
174
|
-
return true
|
143
|
+
true
|
175
144
|
|
176
145
|
rescue JSON::ParserError => error
|
177
146
|
@error = "API returned invalid JSON. #{error.message}"
|
@@ -196,7 +165,7 @@ module SmsPilot
|
|
196
165
|
# client.balance #=> 20215.25
|
197
166
|
#
|
198
167
|
def balance
|
199
|
-
|
168
|
+
response_data["balance"]&.to_f if sms_sent?
|
200
169
|
end
|
201
170
|
|
202
171
|
|
@@ -237,7 +206,36 @@ module SmsPilot
|
|
237
206
|
#
|
238
207
|
def rejected?
|
239
208
|
return false if sms_sent?
|
240
|
-
|
209
|
+
response_data["error"].is_a? Hash
|
210
|
+
end
|
211
|
+
|
212
|
+
|
213
|
+
# Parses <tt>@response_body</tt> and memoizes result in <tt>@response_data</tt>
|
214
|
+
#
|
215
|
+
# @example
|
216
|
+
# {
|
217
|
+
# "balance" => "20006.97",
|
218
|
+
# "cost" => "1.68",
|
219
|
+
# "send" => [
|
220
|
+
# {
|
221
|
+
# "phone" => "79021234567",
|
222
|
+
# "price" => "1.68",
|
223
|
+
# "server_id" => "10000",
|
224
|
+
# "status" => "0"
|
225
|
+
# }
|
226
|
+
# ]
|
227
|
+
# }
|
228
|
+
#
|
229
|
+
# @return [Hash]
|
230
|
+
# @raise [JSON::ParserError] which is rescued in {#send_sms}
|
231
|
+
#
|
232
|
+
# @see #response_body
|
233
|
+
# @see #response_headers
|
234
|
+
# @see #response_status
|
235
|
+
#
|
236
|
+
def response_data
|
237
|
+
return {} unless @response_body
|
238
|
+
@response_data ||= JSON.parse @response_body
|
241
239
|
end
|
242
240
|
|
243
241
|
|
@@ -268,7 +266,7 @@ module SmsPilot
|
|
268
266
|
# client.sms_cost #=> 2.63
|
269
267
|
#
|
270
268
|
def sms_cost
|
271
|
-
|
269
|
+
response_data["cost"]&.to_f if sms_sent?
|
272
270
|
end
|
273
271
|
|
274
272
|
|
@@ -283,7 +281,7 @@ module SmsPilot
|
|
283
281
|
# client.sms_sent? #=> true
|
284
282
|
#
|
285
283
|
def sms_sent?
|
286
|
-
|
284
|
+
response_data["send"] != nil
|
287
285
|
end
|
288
286
|
|
289
287
|
|
@@ -312,15 +310,20 @@ module SmsPilot
|
|
312
310
|
|
313
311
|
|
314
312
|
# The URI we will send an HTTP request to
|
315
|
-
#
|
316
313
|
# @private
|
317
|
-
# @return [URI]
|
318
|
-
# @raise [URI::InvalidURIError] but is very unlikely because we provide the URL ourselves
|
319
314
|
#
|
320
315
|
# @example
|
321
316
|
# build_uri("79021234567", "Hello, World!")
|
322
317
|
# #=> #<URI::HTTPS https://smspilot.ru/api.php?apikey=XXX…&format=json&send=Hello%2C+World%21&to=79021234567>
|
323
318
|
#
|
319
|
+
# @return [URI]
|
320
|
+
# @raise [URI::InvalidURIError] but is very unlikely because we provide the URL ourselves
|
321
|
+
#
|
322
|
+
# @see #api_key
|
323
|
+
# @see #phone
|
324
|
+
# @see #validate_phone!
|
325
|
+
# @see #validate_message!
|
326
|
+
#
|
324
327
|
private def build_uri(phone, text)
|
325
328
|
URI.parse(API_ENDPOINT).tap do |uri|
|
326
329
|
uri.query = URI.encode_www_form({ apikey: @api_key, format: :json, send: text, to: phone })
|
@@ -331,6 +334,7 @@ module SmsPilot
|
|
331
334
|
# Cleans up your phone from anything but digits. Also replaces 8 to 7 if it is the first digit.
|
332
335
|
#
|
333
336
|
# @private
|
337
|
+
# @param [String] phone
|
334
338
|
# @return [String]
|
335
339
|
#
|
336
340
|
# @example
|
@@ -377,9 +381,10 @@ module SmsPilot
|
|
377
381
|
|
378
382
|
|
379
383
|
# Validates message
|
380
|
-
#
|
381
384
|
# @private
|
382
|
-
#
|
385
|
+
#
|
386
|
+
# @param [String] message
|
387
|
+
# @return [String] the original value passed into the method, only if it was valid
|
383
388
|
#
|
384
389
|
# @raise [SmsPilot::InvalidMessageError] if you pass anythig but a String with the <tt>message</tt> argument
|
385
390
|
# @raise [SmsPilot::InvalidMessageError] if your message is empty
|
@@ -387,13 +392,15 @@ module SmsPilot
|
|
387
392
|
private def validate_message!(message)
|
388
393
|
fail SmsPilot::InvalidMessageError, "SMS message must be a String, you pass a #{ message.class} (#{ message})" unless message.is_a? String
|
389
394
|
fail SmsPilot::InvalidMessageError, "SMS message cannot be empty" if message == ""
|
395
|
+
message
|
390
396
|
end
|
391
397
|
|
392
398
|
|
393
399
|
# Validates phone
|
394
|
-
#
|
395
400
|
# @private
|
396
|
-
#
|
401
|
+
#
|
402
|
+
# @param [String] phone
|
403
|
+
# @return [String] the original value passed into the method, only if it was valid
|
397
404
|
#
|
398
405
|
# @raise [SmsPilot::InvalidPhoneError] if you pass anythig but a String with the <tt>phone</tt> argument
|
399
406
|
# @raise [SmsPilot::InvalidPhoneError] if your phone is empty
|
@@ -403,6 +410,7 @@ module SmsPilot
|
|
403
410
|
fail SmsPilot::InvalidPhoneError, "phone must be a String, you pass a #{phone.class} (#{phone})" unless phone.is_a? String
|
404
411
|
fail SmsPilot::InvalidPhoneError, "phone cannot be empty" if phone == ""
|
405
412
|
fail SmsPilot::InvalidPhoneError, "phone must contain digits" if phone.scan(/\d/).none?
|
413
|
+
phone
|
406
414
|
end
|
407
415
|
|
408
416
|
# @!endgroup
|
data/lib/sms_pilot/version.rb
CHANGED
data/sms-pilot-api-v1.gemspec
CHANGED
@@ -15,10 +15,10 @@ Gem::Specification.new do |spec|
|
|
15
15
|
spec.required_ruby_version = Gem::Requirement.new(">= 2.5.0")
|
16
16
|
|
17
17
|
spec.metadata = {
|
18
|
-
|
18
|
+
"changelog_uri" => "#{spec.homepage}/blob/master/CHANGELOG.md",
|
19
19
|
"documentation_uri" => "https://rubydoc.info/github/sergeypedan/#{spec.name}/master/",
|
20
|
-
|
21
|
-
|
20
|
+
"homepage_uri" => spec.homepage,
|
21
|
+
"source_code_uri" => spec.homepage
|
22
22
|
}
|
23
23
|
|
24
24
|
# Specify which files should be added to the gem when it is released.
|
@@ -27,7 +27,10 @@ Gem::Specification.new do |spec|
|
|
27
27
|
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features|pkg|doc)/}) }
|
28
28
|
end
|
29
29
|
|
30
|
-
spec.bindir = "
|
30
|
+
spec.bindir = "exe"
|
31
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
31
32
|
spec.require_paths = ["lib"]
|
32
33
|
|
34
|
+
# spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
35
|
+
|
33
36
|
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.
|
4
|
+
version: 0.0.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sergey Pedan
|
8
8
|
autorequire:
|
9
|
-
bindir:
|
9
|
+
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-05-
|
11
|
+
date: 2021-05-09 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,6 +19,7 @@ extensions: []
|
|
19
19
|
extra_rdoc_files: []
|
20
20
|
files:
|
21
21
|
- ".gitignore"
|
22
|
+
- ".inch.yml"
|
22
23
|
- ".rspec"
|
23
24
|
- ".yardopts"
|
24
25
|
- CHANGELOG.md
|