sms-ru-client 1.0.1
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 +7 -0
- data/README.md +89 -0
- data/lib/sms-ru-client.rb +1 -0
- data/lib/sms_ru/client/api/sms.rb +25 -0
- data/lib/sms_ru/client/api.rb +1 -0
- data/lib/sms_ru/client/models/requests/base.rb +12 -0
- data/lib/sms_ru/client/models/requests/send_sms.rb +19 -0
- data/lib/sms_ru/client/models/responses/base.rb +13 -0
- data/lib/sms_ru/client/models/responses/send_sms.rb +37 -0
- data/lib/sms_ru/client/models.rb +10 -0
- data/lib/sms_ru/client.rb +70 -0
- data/lib/sms_ru/configurable.rb +34 -0
- data/lib/sms_ru.rb +18 -0
- metadata +90 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: fdb9c999f90ca43a9b942d26e06d9135dba773a4623d0f4029beef5692e4d20b
|
4
|
+
data.tar.gz: 7dc839efcdf592f156a3292401a5ecc91588fba2d40f2503cf395cde5ec2357a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6a93c6952370e6aca25a8116a0090c857f1b50c1f78b249002572d3663f6a92a60dadb53c55f1744f45c6b6b51b8c95e5ec9966cb7cbb3a3bc36494e1d4c96bc
|
7
|
+
data.tar.gz: ca6302411defdf000dacd442826ff233fc4850484b742c73225e78b7394b4d574f2ff57e00c508c5fd84669fb57f02f6691fa78679d38e810784abc92f6719f8
|
data/README.md
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
# SmsRu
|
2
|
+
|
3
|
+
Ruby Client Library for working with [SMS.RU](https://sms.ru).
|
4
|
+
|
5
|
+
NOTE: current implementation does not support ALL features, and includes only:
|
6
|
+
|
7
|
+
* Send SMS
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'sms-ru-client'
|
15
|
+
```
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install sms-ru-client
|
24
|
+
|
25
|
+
## Configuration
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
SmsRu.configure do |config|
|
29
|
+
config.api_endpoint = 'https://sms.ru/'
|
30
|
+
config.logger = Logger.new(STDERR)
|
31
|
+
config.api_id = 'your api_id'
|
32
|
+
end
|
33
|
+
```
|
34
|
+
|
35
|
+
or
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
client = SmsRu::Client.new(
|
39
|
+
api_endpoint: 'https://sms.ru/'
|
40
|
+
logger: Logger.new(STDERR)
|
41
|
+
api_id: 'your api_id'
|
42
|
+
)
|
43
|
+
```
|
44
|
+
|
45
|
+
## Usage
|
46
|
+
|
47
|
+
### Send SMS
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
request = SmsRu::Requests::SendSms.new(
|
51
|
+
to: '79991234567',
|
52
|
+
message: 'hello world',
|
53
|
+
test: true
|
54
|
+
)
|
55
|
+
|
56
|
+
SmsRu.client.send_sms(request)
|
57
|
+
```
|
58
|
+
|
59
|
+
## Development
|
60
|
+
|
61
|
+
Setup:
|
62
|
+
|
63
|
+
```console
|
64
|
+
make setup
|
65
|
+
```
|
66
|
+
|
67
|
+
Run linters:
|
68
|
+
|
69
|
+
```console
|
70
|
+
make lint
|
71
|
+
```
|
72
|
+
|
73
|
+
Run tests:
|
74
|
+
|
75
|
+
```console
|
76
|
+
make test
|
77
|
+
```
|
78
|
+
|
79
|
+
## Contributing
|
80
|
+
|
81
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/zabolotnov87/sms_ru. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
82
|
+
|
83
|
+
## License
|
84
|
+
|
85
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
86
|
+
|
87
|
+
## Code of Conduct
|
88
|
+
|
89
|
+
Everyone interacting in the Test project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/zabolotnov87/sms_ru/blob/master/CODE_OF_CONDUCT.md).
|
@@ -0,0 +1 @@
|
|
1
|
+
require_relative './sms_ru'
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module SmsRu
|
2
|
+
class Client
|
3
|
+
module Api
|
4
|
+
module Sms
|
5
|
+
SEND_PATH = 'sms/send'.freeze
|
6
|
+
|
7
|
+
# @param request [SmsRu::Requests::SendSms, Hash]
|
8
|
+
# @raise [SmsRu::Responses::SendSms::Error]
|
9
|
+
# @return [SmsRu::Responses::SendSms]
|
10
|
+
#
|
11
|
+
def send_sms(request)
|
12
|
+
request = Requests::SendSms.new(request) unless request.is_a?(Requests::SendSms)
|
13
|
+
response = http_post(SEND_PATH, params: request)
|
14
|
+
json = JSON.parse(response.body)
|
15
|
+
result = Responses::SendSms.new(json)
|
16
|
+
return result if result.success?
|
17
|
+
|
18
|
+
raise result.to_error
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
include Sms
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require_relative 'api/sms'
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module SmsRu
|
2
|
+
class Client
|
3
|
+
module Models
|
4
|
+
module Requests
|
5
|
+
class SendSms < Base
|
6
|
+
property :test, transform_with: ->(value) { '1' if value }
|
7
|
+
property :msg, from: 'message'
|
8
|
+
property :to, transform_with: lambda { |value|
|
9
|
+
if value.is_a?(Array)
|
10
|
+
value.join(',')
|
11
|
+
else
|
12
|
+
value
|
13
|
+
end
|
14
|
+
}
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module SmsRu
|
2
|
+
class Client
|
3
|
+
module Models
|
4
|
+
module Responses
|
5
|
+
class Base < Hashie::Trash
|
6
|
+
include Hashie::Extensions::IgnoreUndeclared
|
7
|
+
include Hashie::Extensions::Dash::IndifferentAccess
|
8
|
+
include Hashie::Extensions::Dash::Coercion
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module SmsRu
|
2
|
+
class Client
|
3
|
+
module Models
|
4
|
+
module Responses
|
5
|
+
class SendSms < Base
|
6
|
+
class Error < StandardError
|
7
|
+
attr_reader :status, :code, :text
|
8
|
+
|
9
|
+
def initialize(status, code, text)
|
10
|
+
@status = status
|
11
|
+
@code = code
|
12
|
+
@text = text
|
13
|
+
super("status: #{status}, code: #{code}, text: #{text}")
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
SUCCESS_STATUS = 'OK'.freeze
|
18
|
+
|
19
|
+
property :status
|
20
|
+
property :code, from: 'status_code'
|
21
|
+
property :text, from: 'status_text'
|
22
|
+
property :balance
|
23
|
+
|
24
|
+
def success?
|
25
|
+
status == SUCCESS_STATUS
|
26
|
+
end
|
27
|
+
|
28
|
+
def to_error
|
29
|
+
return if success?
|
30
|
+
|
31
|
+
Error.new(status, code, text)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
require_relative 'models/requests/base'
|
2
|
+
require_relative 'models/requests/send_sms'
|
3
|
+
|
4
|
+
require_relative 'models/responses/base'
|
5
|
+
require_relative 'models/responses/send_sms'
|
6
|
+
|
7
|
+
module SmsRu
|
8
|
+
Requests = Client::Models::Requests
|
9
|
+
Responses = Client::Models::Responses
|
10
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require_relative 'client/models'
|
2
|
+
require_relative 'client/api'
|
3
|
+
|
4
|
+
module SmsRu
|
5
|
+
class Client
|
6
|
+
include Api
|
7
|
+
include Configurable
|
8
|
+
|
9
|
+
def initialize(options = {})
|
10
|
+
CONFIGURATION_OPTIONS.each do |attribute|
|
11
|
+
value = options[attribute] || SmsRu.send(attribute)
|
12
|
+
send("#{attribute}=", value)
|
13
|
+
end
|
14
|
+
connection
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def connection
|
20
|
+
@connection ||= Faraday.new(url: api_endpoint) do |connection|
|
21
|
+
setup_timeouts!(connection)
|
22
|
+
setup_auth!(connection)
|
23
|
+
setup_error_handling!(connection)
|
24
|
+
setup_log_filters!(connection)
|
25
|
+
setup_json!(connection)
|
26
|
+
|
27
|
+
connection.adapter(Faraday.default_adapter)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def setup_timeouts!(connection)
|
32
|
+
connection.options.open_timeout = open_timeout
|
33
|
+
connection.options.timeout = read_timeout
|
34
|
+
end
|
35
|
+
|
36
|
+
def setup_auth!(connection)
|
37
|
+
connection.params[:api_id] = api_id
|
38
|
+
end
|
39
|
+
|
40
|
+
def setup_error_handling!(connection)
|
41
|
+
connection.response(:raise_error)
|
42
|
+
end
|
43
|
+
|
44
|
+
def setup_log_filters!(connection)
|
45
|
+
options = { headers: true, bodies: true, log_level: :debug }
|
46
|
+
connection.response(:logger, logger, options) do |logger|
|
47
|
+
logger.filter(/(api_id=)([^&]+)/, '\1[FILTERED]')
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def setup_json!(connection)
|
52
|
+
connection.params[:json] = 1
|
53
|
+
end
|
54
|
+
|
55
|
+
def http_post(resource, params:, headers: {})
|
56
|
+
connection.post do |request|
|
57
|
+
request.url(resource)
|
58
|
+
request.headers.merge!(headers)
|
59
|
+
request.params.merge!(params)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def http_get(resource, params = {})
|
64
|
+
connection.get do |request|
|
65
|
+
request.url(resource)
|
66
|
+
request.params.update(params) unless params.empty?
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module SmsRu
|
2
|
+
module Configurable
|
3
|
+
CONFIGURATION_OPTIONS = %i[
|
4
|
+
api_endpoint
|
5
|
+
api_id
|
6
|
+
logger
|
7
|
+
open_timeout
|
8
|
+
read_timeout
|
9
|
+
].freeze
|
10
|
+
|
11
|
+
DEFAULTS_READ_TIMEOUT = 5
|
12
|
+
DEFAULTS_OPEN_TIMEOUT = 2
|
13
|
+
|
14
|
+
attr_accessor(*CONFIGURATION_OPTIONS)
|
15
|
+
|
16
|
+
def configure
|
17
|
+
yield self
|
18
|
+
set_defaults
|
19
|
+
end
|
20
|
+
|
21
|
+
def set_defaults
|
22
|
+
self.read_timeout ||= DEFAULTS_READ_TIMEOUT
|
23
|
+
self.open_timeout ||= DEFAULTS_OPEN_TIMEOUT
|
24
|
+
end
|
25
|
+
|
26
|
+
def options
|
27
|
+
CONFIGURATION_OPTIONS.to_h { |attr| [attr, send(attr)] }
|
28
|
+
end
|
29
|
+
|
30
|
+
def same_options?(options)
|
31
|
+
self.options.hash == options.hash
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
data/lib/sms_ru.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
require 'json'
|
3
|
+
require 'hashie'
|
4
|
+
|
5
|
+
require_relative 'sms_ru/configurable'
|
6
|
+
require_relative 'sms_ru/client'
|
7
|
+
|
8
|
+
module SmsRu
|
9
|
+
class << self
|
10
|
+
include Configurable
|
11
|
+
|
12
|
+
def client
|
13
|
+
return @client if defined?(@client) && @client.same_options?(options)
|
14
|
+
|
15
|
+
@client = Client.new(options)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
metadata
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sms-ru-client
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sergey Zaboltonov
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-09-05 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: faraday
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: hashie
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3.4'
|
34
|
+
- - "<"
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: '5.0'
|
37
|
+
type: :runtime
|
38
|
+
prerelease: false
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '3.4'
|
44
|
+
- - "<"
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '5.0'
|
47
|
+
description:
|
48
|
+
email:
|
49
|
+
- sergey.zabolotnov@gmail.com
|
50
|
+
executables: []
|
51
|
+
extensions: []
|
52
|
+
extra_rdoc_files: []
|
53
|
+
files:
|
54
|
+
- README.md
|
55
|
+
- lib/sms-ru-client.rb
|
56
|
+
- lib/sms_ru.rb
|
57
|
+
- lib/sms_ru/client.rb
|
58
|
+
- lib/sms_ru/client/api.rb
|
59
|
+
- lib/sms_ru/client/api/sms.rb
|
60
|
+
- lib/sms_ru/client/models.rb
|
61
|
+
- lib/sms_ru/client/models/requests/base.rb
|
62
|
+
- lib/sms_ru/client/models/requests/send_sms.rb
|
63
|
+
- lib/sms_ru/client/models/responses/base.rb
|
64
|
+
- lib/sms_ru/client/models/responses/send_sms.rb
|
65
|
+
- lib/sms_ru/configurable.rb
|
66
|
+
homepage: https://github.com/zabolotnov87/sms_ru
|
67
|
+
licenses:
|
68
|
+
- MIT
|
69
|
+
metadata:
|
70
|
+
rubygems_mfa_required: 'true'
|
71
|
+
post_install_message:
|
72
|
+
rdoc_options: []
|
73
|
+
require_paths:
|
74
|
+
- lib
|
75
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - "~>"
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '3.0'
|
80
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
requirements: []
|
86
|
+
rubygems_version: 3.3.7
|
87
|
+
signing_key:
|
88
|
+
specification_version: 4
|
89
|
+
summary: Ruby Client Library for working with SMS.RU
|
90
|
+
test_files: []
|