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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ef5f991fb1af59f48bbc7ccab3e1ab3b417c76fc22a584922fa5bca8879e294c
4
- data.tar.gz: 83031cf779a24ed4b1d44de6d08f33ef611a0b9fdc63742c14ec792080c54fa5
3
+ metadata.gz: 398583e8c6b3d5ab1ffa33d2e85a412fd779d3332775980d447e2354cdfaf19e
4
+ data.tar.gz: dfc1c5f424e7c95e9a0829883b701d66da85134d18dc2267e76f3db7756d357a
5
5
  SHA512:
6
- metadata.gz: 84a60a7e124256682fd89d4d411c7402feb6597b0541493c2bd08b88b95a515cdf4ae9c0fb435939d7b6f98a56b2d9dd513cd344023bb2f446fbcffc8507eac0
7
- data.tar.gz: d0a91bb376ef6090356040cc8eddca2272ce1917e044a7a7cea43f6fe812d265ea1c548eb63e95b5c163a1baed78758bb35a5a0075dea096674bbd4649f0a4ab
6
+ metadata.gz: d48222754c8acd857fa867385250ce8de32d92c10e624dfef768077f6399eb289258ddb89137d327a243e5d7a59e4527509c7bfb186fed885e6452284030b079
7
+ data.tar.gz: f5d14f8fc7558024c8fa5a49a3b2e270487de1cd6d2d4e8aa93d45c10e4d948837c8ac4dc13d85e115ae2ce24e956eaefb3ae43d943bfe2732eee93478a55be6
data/.inch.yml ADDED
@@ -0,0 +1,11 @@
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
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
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- sms-pilot-api-v1 (0.0.5)
4
+ sms-pilot-api-v1 (0.0.6)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -37,4 +37,4 @@ DEPENDENCIES
37
37
  sms-pilot-api-v1!
38
38
 
39
39
  BUNDLED WITH
40
- 2.2.11
40
+ 2.2.17
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
@@ -1,5 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # Module that for now encompasses only API client
4
+ #
5
+ module SmsPilot
6
+ end
7
+
3
8
  require_relative "sms_pilot/version"
4
9
  require_relative "sms_pilot/errors"
5
10
  require_relative "sms_pilot/client"
@@ -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
- @error = "HTTP request failed with code #{response.code}"
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
- @response_data = JSON.parse @response_body
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
- @response_data["balance"]&.to_f if sms_sent?
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
- @response_data["error"].is_a? Hash
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
- @response_data["cost"]&.to_f if sms_sent?
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
- @response_data["send"] != nil
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
- # @return [nil]
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
- # @return [nil]
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
@@ -3,6 +3,6 @@
3
3
  module SmsPilot
4
4
 
5
5
  # Gem version
6
- VERSION = "0.0.6"
6
+ VERSION = "0.0.7"
7
7
 
8
8
  end
@@ -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
- "changelog_uri" => "#{spec.homepage}/blob/master/CHANGELOG.md",
18
+ "changelog_uri" => "#{spec.homepage}/blob/master/CHANGELOG.md",
19
19
  "documentation_uri" => "https://rubydoc.info/github/sergeypedan/#{spec.name}/master/",
20
- "homepage_uri" => spec.homepage,
21
- "source_code_uri" => spec.homepage
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 = "bin"
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.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sergey Pedan
8
8
  autorequire:
9
- bindir: bin
9
+ bindir: exe
10
10
  cert_chain: []
11
- date: 2021-05-08 00:00:00.000000000 Z
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