sms_traffic_sdk 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.env.template +4 -0
- data/.fasterer.yml +2 -0
- data/.github/workflows/ci.yml +75 -0
- data/.gitignore +12 -0
- data/.rspec +2 -0
- data/.rubocop.yml +20 -0
- data/.tool-versions +1 -0
- data/Gemfile +17 -0
- data/LICENSE +21 -0
- data/README.md +93 -0
- data/Rakefile +10 -0
- data/bin/console +18 -0
- data/bin/setup +7 -0
- data/exe/konsierge-sms.rb +2 -0
- data/lib/sms_traffic/client/deliver_reply.rb +60 -0
- data/lib/sms_traffic/client/reply.rb +18 -0
- data/lib/sms_traffic/client/response.rb +39 -0
- data/lib/sms_traffic/client/status_reply.rb +44 -0
- data/lib/sms_traffic/client.rb +79 -0
- data/lib/sms_traffic/configuration.rb +73 -0
- data/lib/sms_traffic/sms.rb +59 -0
- data/lib/sms_traffic/version.rb +3 -0
- data/lib/sms_traffic.rb +21 -0
- data/sms_traffic_sdk.gemspec +29 -0
- metadata +111 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 97b598c2448cc5371fb1d2dd91d0f5706473ea3657e5494e4e72edb53a304792
|
4
|
+
data.tar.gz: 4f42caff68f07f17e841119111b013dd40be2cda9b321bb8e7bdafa4509ddb2e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2b09f25a918672f487011f79861441f96b650625f1fc9cf0db213a9204b8cfab9f78b1d883b3ed76da5ba0d6b47d6bd1d52710c0cb902e386cffc0f625f76c13
|
7
|
+
data.tar.gz: fe2cfb46546cf8438b325e2a676e28792d2ff585a13ac75304af2a3079a2401f72acbb7c00c7af2f5fe3a2748a799d622f204dec1442b025215f96ccfd38239b
|
data/.env.template
ADDED
data/.fasterer.yml
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
name: Ruby Gem CI/CD
|
2
|
+
|
3
|
+
on:
|
4
|
+
push:
|
5
|
+
branches: [ main ]
|
6
|
+
pull_request:
|
7
|
+
branches: [ main ]
|
8
|
+
release:
|
9
|
+
types: [created]
|
10
|
+
on:
|
11
|
+
push:
|
12
|
+
tags:
|
13
|
+
- 'v*'
|
14
|
+
|
15
|
+
jobs:
|
16
|
+
build:
|
17
|
+
runs-on: ubuntu-latest
|
18
|
+
strategy:
|
19
|
+
matrix:
|
20
|
+
ruby-version: ['2.7', '3.0', '3.1', '3.2', '3.3']
|
21
|
+
|
22
|
+
steps:
|
23
|
+
- uses: actions/checkout@v2
|
24
|
+
|
25
|
+
- name: Set up Ruby
|
26
|
+
uses: ruby/setup-ruby@v1
|
27
|
+
with:
|
28
|
+
ruby-version: ${{ matrix.ruby-version }}
|
29
|
+
|
30
|
+
- name: Install Bundler for Ruby < 3.0
|
31
|
+
if: ${{ matrix.ruby-version < '3.0' }}
|
32
|
+
run: gem install bundler -v 2.4.22 --no-document
|
33
|
+
|
34
|
+
- name: Install Bundler for Ruby >= 3.0
|
35
|
+
if: ${{ matrix.ruby-version >= '3.0' }}
|
36
|
+
run: gem install bundler -v '~> 2.5' --no-document
|
37
|
+
|
38
|
+
- name: Install dependencies
|
39
|
+
run: bundle install --jobs 4 --retry 3
|
40
|
+
|
41
|
+
- name: Run tests and linting
|
42
|
+
run: bundle exec rake
|
43
|
+
|
44
|
+
- name: Upload coverage reports to Codecov
|
45
|
+
uses: codecov/codecov-action@v4.0.1
|
46
|
+
with:
|
47
|
+
token: ${{ secrets.CODECOV_TOKEN }}
|
48
|
+
file: ./coverage/.resultset.json
|
49
|
+
fail_ci_if_error: false
|
50
|
+
|
51
|
+
publish:
|
52
|
+
needs: build
|
53
|
+
runs-on: ubuntu-latest
|
54
|
+
steps:
|
55
|
+
- uses: actions/checkout@v2
|
56
|
+
|
57
|
+
- name: Set up Ruby for publish
|
58
|
+
uses: ruby/setup-ruby@v1
|
59
|
+
with:
|
60
|
+
ruby-version: '3.3'
|
61
|
+
|
62
|
+
- name: Install dependencies
|
63
|
+
run: |
|
64
|
+
gem install bundler --no-document
|
65
|
+
bundle install --jobs 4 --retry 3
|
66
|
+
|
67
|
+
- name: Build gem
|
68
|
+
run: |
|
69
|
+
gem build *.gemspec
|
70
|
+
|
71
|
+
- name: Push gem to RubyGems
|
72
|
+
run: |
|
73
|
+
gem push *.gem
|
74
|
+
env:
|
75
|
+
GEM_HOST_API_KEY: ${{secrets.GEM_HOST_API_KEY}}
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 2.7.4
|
3
|
+
NewCops: enable
|
4
|
+
SuggestExtensions: false
|
5
|
+
Exclude:
|
6
|
+
- 'bin/**/*'
|
7
|
+
|
8
|
+
Style/Documentation:
|
9
|
+
Enabled: false
|
10
|
+
|
11
|
+
Style/FrozenStringLiteralComment:
|
12
|
+
EnforcedStyle: 'never'
|
13
|
+
|
14
|
+
Layout/HashAlignment:
|
15
|
+
EnforcedHashRocketStyle: table
|
16
|
+
EnforcedColonStyle: table
|
17
|
+
|
18
|
+
Metrics/BlockLength:
|
19
|
+
Exclude:
|
20
|
+
- 'spec/**/*'
|
data/.tool-versions
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby 3.3.3
|
data/Gemfile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
gemspec
|
4
|
+
|
5
|
+
group :development, :test do
|
6
|
+
gem 'dotenv', '~> 2.7'
|
7
|
+
gem 'fasterer', '~> 0.9'
|
8
|
+
gem 'rack', '~> 3.1.7'
|
9
|
+
gem 'rake', '~> 13.2'
|
10
|
+
gem 'rspec', '~> 3.10'
|
11
|
+
gem 'rspec-mocks', '~> 3.10'
|
12
|
+
gem 'rubocop', '~> 1.18'
|
13
|
+
gem 'rubocop-performance', '~> 1.11'
|
14
|
+
gem 'rubocop-rspec', '~> 2.11'
|
15
|
+
gem 'simplecov', '~> 0.22'
|
16
|
+
gem 'webmock', '~> 3.14'
|
17
|
+
end
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2024 David Rybolovlev
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
# SmsTraffic
|
2
|
+
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/sms_traffic.svg)](https://badge.fury.io/rb/sms_traffic)
|
4
|
+
[![Maintainability](https://api.codeclimate.com/v1/badges/bb4795be5024dd81d927/maintainability)](https://codeclimate.com/github/golifox/sms_traffic/maintainability)
|
5
|
+
[![Coverage](https://codecov.io/github/golifox/sms_traffic/graph/badge.svg?token=74C0YBJP3F)](https://codecov.io/github/golifox/sms_traffic)
|
6
|
+
[![CI](https://github.com/golifox/sms_traffic/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/golifox/sms_traffic/actions/workflows/ci.yml)
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'sms_traffic_sdk'
|
14
|
+
```
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle
|
19
|
+
|
20
|
+
Or install it yourself as:
|
21
|
+
|
22
|
+
$ gem install sms_traffic_sdk
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
|
27
|
+
Define settings:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
# initializers/sms_traffic.rb
|
31
|
+
require 'sms_traffic_sdk'
|
32
|
+
|
33
|
+
SmsTraffic.configure do |config|
|
34
|
+
config.login = 'login'
|
35
|
+
config.password = 'password'
|
36
|
+
config.originator = 'default_originator'
|
37
|
+
config.server = 'https://api.smstraffic.ru'
|
38
|
+
config.debug = true
|
39
|
+
config.debugger = Rails.logger
|
40
|
+
end
|
41
|
+
```
|
42
|
+
|
43
|
+
Initialize sms:
|
44
|
+
```ruby
|
45
|
+
sms = SmsTraffic::Sms.new('phone', 'text', 'originator') # initialize sms, originator by default from settings
|
46
|
+
# => #<SmsTraffic::Sms:0x0000 @errors=[], @message="text", @originator="default_originator", @phone="phone", @status="not-sent">
|
47
|
+
|
48
|
+
```
|
49
|
+
|
50
|
+
Send it and get sent sms id and dispatch code:
|
51
|
+
```ruby
|
52
|
+
sms.deliver # send sms. returns sms id or dispatch code if something went wrong
|
53
|
+
# => true
|
54
|
+
sms.id # get sms id
|
55
|
+
# => '123456789'
|
56
|
+
```
|
57
|
+
|
58
|
+
Get current sms status and update it:
|
59
|
+
```ruby
|
60
|
+
sms.status # get sms delivery status
|
61
|
+
# => 'sent'
|
62
|
+
sms.update_status # updates sms delivery status. returns status or status check response code on error
|
63
|
+
# => 'delivered'
|
64
|
+
```
|
65
|
+
|
66
|
+
Get any sms status:
|
67
|
+
```ruby
|
68
|
+
response = SmsTraffic.status(sms_id) # status - sms delivery status unless error or return error
|
69
|
+
reply = response.reply
|
70
|
+
# => #<SmsTraffic::Client::StatusReply:0x0000>
|
71
|
+
status = reply.status
|
72
|
+
# => "Delivered"
|
73
|
+
reply.hash
|
74
|
+
# =>
|
75
|
+
# {"error"=>nil,
|
76
|
+
# "submition_date"=>"2024-07-15 11:36:25",
|
77
|
+
# "send_date"=>"2024-07-15 11:36:25",
|
78
|
+
# "last_status_change_date"=>"2024-07-15 11:36:25",
|
79
|
+
# "sms_id"=>"865149797164801235",
|
80
|
+
# "status"=>"Delivered"}
|
81
|
+
|
82
|
+
```
|
83
|
+
|
84
|
+
## Development
|
85
|
+
|
86
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
87
|
+
|
88
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
89
|
+
|
90
|
+
## License
|
91
|
+
|
92
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
93
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'dotenv'
|
5
|
+
require 'sms_traffic'
|
6
|
+
|
7
|
+
Dotenv.load('.env')
|
8
|
+
|
9
|
+
SmsTraffic.configure do |config|
|
10
|
+
config.host = ENV['SMS_TRAFFIC_HOST']
|
11
|
+
config.login = ENV['SMS_TRAFFIC_LOGIN']
|
12
|
+
config.password = ENV['SMS_TRAFFIC_PASSWORD']
|
13
|
+
config.originator = ENV['SMS_TRAFFIC_ORIGINATOR']
|
14
|
+
end
|
15
|
+
|
16
|
+
|
17
|
+
require 'irb'
|
18
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
module SmsTraffic
|
2
|
+
class Client
|
3
|
+
class DeliverReply < Client::Reply
|
4
|
+
OK_REPLY = 'OK'.freeze unless const_defined?(:OK_REPLY)
|
5
|
+
|
6
|
+
def ok?
|
7
|
+
fetch_value(hash, :result) == OK_REPLY
|
8
|
+
end
|
9
|
+
|
10
|
+
def error?
|
11
|
+
!ok?
|
12
|
+
end
|
13
|
+
|
14
|
+
def result
|
15
|
+
fetch_value(hash, :result)
|
16
|
+
end
|
17
|
+
|
18
|
+
def code
|
19
|
+
fetch_value(hash, :code)
|
20
|
+
end
|
21
|
+
|
22
|
+
def description
|
23
|
+
fetch_value(hash, :description)
|
24
|
+
end
|
25
|
+
|
26
|
+
def message_infos
|
27
|
+
fetch_value(hash, :message_infos)
|
28
|
+
end
|
29
|
+
|
30
|
+
def sms_id(phone = nil)
|
31
|
+
message_info = fetch_value(message_infos, :message_info)
|
32
|
+
return fetch_value(message_info, :sms_id) if message_info.is_a?(Hash)
|
33
|
+
|
34
|
+
raise ArgumentError, 'phone is required' unless phone
|
35
|
+
|
36
|
+
fetch_value(find_message_info(phone), :sms_id)
|
37
|
+
end
|
38
|
+
|
39
|
+
def sms_ids
|
40
|
+
message_info = fetch_value(message_infos, :message_info)
|
41
|
+
|
42
|
+
return [fetch_value(message_info, :sms_id)] if message_info.size == 1
|
43
|
+
|
44
|
+
message_info.each_with_object({}) do |info, hash|
|
45
|
+
hash[fetch_value(info, :phone)] = fetch_value(info, :sms_id)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def error_description
|
50
|
+
"#{result}: code: #{code}, description: #{description}" if error?
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
|
55
|
+
def find_message_info(phone)
|
56
|
+
fetch_value(message_infos, :message_info).find { |info| fetch_value(info, :phone) == phone }
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module SmsTraffic
|
2
|
+
class Client
|
3
|
+
class Reply
|
4
|
+
attr_reader :xml, :hash
|
5
|
+
|
6
|
+
def initialize(xml, xml_parser: SmsTraffic.configuration.xml_parser)
|
7
|
+
@xml = xml.gsub!(/\s+/, ' ')
|
8
|
+
@hash = fetch_value(xml_parser.parse(xml), :reply)
|
9
|
+
end
|
10
|
+
|
11
|
+
private
|
12
|
+
|
13
|
+
def fetch_value(hash, key)
|
14
|
+
hash.fetch(key.to_s, nil) || hash.fetch(key.to_sym, nil)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require_relative 'deliver_reply'
|
2
|
+
require_relative 'status_reply'
|
3
|
+
|
4
|
+
module SmsTraffic
|
5
|
+
class Client
|
6
|
+
class Response
|
7
|
+
attr_reader :origin, :reply, :code, :body, :message
|
8
|
+
|
9
|
+
REPLY_HASH_MAP = {
|
10
|
+
:status => StatusReply,
|
11
|
+
:deliver => DeliverReply,
|
12
|
+
nil => Reply
|
13
|
+
}.freeze
|
14
|
+
|
15
|
+
def initialize(response, type: nil)
|
16
|
+
@origin = response
|
17
|
+
@code = response.code.to_i
|
18
|
+
@body = response.body
|
19
|
+
@message = response.message
|
20
|
+
|
21
|
+
return unless success?
|
22
|
+
|
23
|
+
@reply = REPLY_HASH_MAP[type].new(body)
|
24
|
+
end
|
25
|
+
|
26
|
+
def success?
|
27
|
+
code == 200
|
28
|
+
end
|
29
|
+
|
30
|
+
def failure?
|
31
|
+
!success?
|
32
|
+
end
|
33
|
+
|
34
|
+
def error_description
|
35
|
+
"#{code}: #{message}" if failure?
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module SmsTraffic
|
2
|
+
class Client
|
3
|
+
class StatusReply < Reply
|
4
|
+
def sms_array
|
5
|
+
fetch_value(hash, :sms).is_a?(Array) ? fetch_value(hash, :sms) : [hash]
|
6
|
+
end
|
7
|
+
|
8
|
+
def status(sms_id = nil)
|
9
|
+
return fetch_value(sms_array.first, :status) if sms_array.size == 1
|
10
|
+
|
11
|
+
raise ArgumentError, 'sms_id is required' if sms_id.nil?
|
12
|
+
|
13
|
+
statuses[sms_id.to_s]
|
14
|
+
end
|
15
|
+
|
16
|
+
# @return [Hash] statuses hash (key: sms_id, value: status)
|
17
|
+
# @example
|
18
|
+
#
|
19
|
+
# {
|
20
|
+
# '1234567890': 'DELIVERED',
|
21
|
+
# '0987654321': 'FAILED'
|
22
|
+
# }
|
23
|
+
def statuses
|
24
|
+
sms_array.each_with_object({}) do |sms, hash|
|
25
|
+
hash[fetch_value(sms, :sms_id)] = fetch_value(sms, :status)
|
26
|
+
end.compact
|
27
|
+
end
|
28
|
+
|
29
|
+
def error(sms_id = nil)
|
30
|
+
return fetch_value(sms_array.first, :error) if sms_array.size == 1
|
31
|
+
|
32
|
+
raise ArgumentError, 'sms_id is required' if sms_id.nil?
|
33
|
+
|
34
|
+
errors[sms_id.to_s]
|
35
|
+
end
|
36
|
+
|
37
|
+
def errors
|
38
|
+
sms_array.each_with_object({}) do |sms, hash|
|
39
|
+
hash[fetch_value(sms, :sms_id)] = fetch_value(sms, :error)&.strip
|
40
|
+
end.compact
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
|
3
|
+
module SmsTraffic
|
4
|
+
class Client
|
5
|
+
class << self
|
6
|
+
attr_reader :uri
|
7
|
+
|
8
|
+
# @param phone [String] phone number
|
9
|
+
# @param message [String] message
|
10
|
+
# @param originator [String] originator (subject)
|
11
|
+
# @param options [Hash] additional options
|
12
|
+
def deliver(phone, message, originator = nil, **options)
|
13
|
+
originator ||= SmsTraffic.configuration.originator
|
14
|
+
form_data = deliver_params(phone, message, originator, **options)
|
15
|
+
response = perform_request(form_data)
|
16
|
+
Response.new(response, type: :deliver)
|
17
|
+
end
|
18
|
+
|
19
|
+
# @param message_id [String] message id
|
20
|
+
def status(*message_ids)
|
21
|
+
form_data = status_params(message_ids)
|
22
|
+
response = perform_request(form_data)
|
23
|
+
Response.new(response, type: :status)
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
# @return [Net::HTTP]
|
29
|
+
def connection
|
30
|
+
host, logger = SmsTraffic.configuration.values_at(:host, :logger)
|
31
|
+
|
32
|
+
@uri ||= URI.join(host, 'multi.php')
|
33
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
34
|
+
http.read_timeout = 30
|
35
|
+
http.use_ssl = (uri.scheme == 'https')
|
36
|
+
http.set_debug_output(logger)
|
37
|
+
http
|
38
|
+
end
|
39
|
+
|
40
|
+
# @param form_data [Hash<Symbol, String|Integer>]
|
41
|
+
# @return [Net::HTTPResponse]
|
42
|
+
def perform_request(form_data)
|
43
|
+
connection.start do |http|
|
44
|
+
request = Net::HTTP::Post.new(uri.request_uri)
|
45
|
+
request.set_form_data(form_data)
|
46
|
+
http.request(request)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
# @param phone [String] phone number or phone numbers separated by comma
|
51
|
+
# @param message [String] message
|
52
|
+
# @param originator [String] originator (subject)
|
53
|
+
# @param options [Hash] additional options
|
54
|
+
# @return [Hash<Symbol, String|Integer>]
|
55
|
+
def deliver_params(phone, message, originator, **options)
|
56
|
+
{
|
57
|
+
login: SmsTraffic.configuration.login,
|
58
|
+
password: SmsTraffic.configuration.password,
|
59
|
+
phones: phone,
|
60
|
+
message: message,
|
61
|
+
want_sms_ids: 1,
|
62
|
+
rus: 5,
|
63
|
+
originator: originator
|
64
|
+
}.merge(options)
|
65
|
+
end
|
66
|
+
|
67
|
+
# @param message_ids [Array<String>] message ids
|
68
|
+
# @return [Hash<Symbol, String|Integer>]
|
69
|
+
def status_params(*message_ids)
|
70
|
+
{
|
71
|
+
login: SmsTraffic.configuration.login,
|
72
|
+
password: SmsTraffic.configuration.password,
|
73
|
+
operation: 'status',
|
74
|
+
sms_id: message_ids.join(',')
|
75
|
+
}
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
require 'singleton'
|
2
|
+
require 'nori'
|
3
|
+
|
4
|
+
module SmsTraffic
|
5
|
+
class Configuration
|
6
|
+
include Singleton
|
7
|
+
|
8
|
+
attr_accessor :login, :password, :host, :originator, :debug, :logger, :xml_parser
|
9
|
+
|
10
|
+
def initialize
|
11
|
+
self.debug = default_configuration[:debug]
|
12
|
+
self.logger = default_configuration[:logger]
|
13
|
+
self.xml_parser = default_configuration[:xml_parser]
|
14
|
+
super
|
15
|
+
end
|
16
|
+
|
17
|
+
def reset!
|
18
|
+
default_configuration.each do |key, value|
|
19
|
+
send("#{key}=", value)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def validate!
|
24
|
+
validation_rules.each do |key, value|
|
25
|
+
raise ArgumentError, "#{key} should be defined for #{self}." if value.nil?
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def to_h
|
30
|
+
{
|
31
|
+
login: login,
|
32
|
+
password: password,
|
33
|
+
host: host,
|
34
|
+
originator: originator,
|
35
|
+
debug: debug,
|
36
|
+
logger: logger,
|
37
|
+
xml_parser: xml_parser
|
38
|
+
}
|
39
|
+
end
|
40
|
+
|
41
|
+
def values_at(*keys)
|
42
|
+
to_h.values_at(*keys)
|
43
|
+
end
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
def default_configuration
|
48
|
+
{
|
49
|
+
login: nil,
|
50
|
+
password: nil,
|
51
|
+
host: nil,
|
52
|
+
originator: nil,
|
53
|
+
debug: false,
|
54
|
+
logger: $stdout,
|
55
|
+
xml_parser: default_xml_parser
|
56
|
+
}
|
57
|
+
end
|
58
|
+
|
59
|
+
def validation_rules
|
60
|
+
{
|
61
|
+
'Login' => login,
|
62
|
+
'Password' => password,
|
63
|
+
'Server' => host,
|
64
|
+
'Originator' => originator,
|
65
|
+
'Debug logger' => debug && !logger ? nil : true
|
66
|
+
}
|
67
|
+
end
|
68
|
+
|
69
|
+
def default_xml_parser
|
70
|
+
@default_xml_parser ||= Nori.new(advanced_typecasting: true).freeze
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
module SmsTraffic
|
2
|
+
class Sms
|
3
|
+
attr_accessor :phone, :message, :originator
|
4
|
+
attr_reader :id, :status, :errors
|
5
|
+
|
6
|
+
def initialize(phone, message, originator: nil)
|
7
|
+
@phone = phone
|
8
|
+
@message = message
|
9
|
+
@originator = originator || SmsTraffic.configuration.originator
|
10
|
+
@status = 'not-sent'
|
11
|
+
@errors = []
|
12
|
+
validate!
|
13
|
+
end
|
14
|
+
|
15
|
+
def deliver # rubocop:disable Metrics/MethodLength
|
16
|
+
# @type [Client::Response]
|
17
|
+
response = Client.deliver(phone, message, originator)
|
18
|
+
|
19
|
+
unless response.success?
|
20
|
+
@errors << response.error_description
|
21
|
+
return false
|
22
|
+
end
|
23
|
+
|
24
|
+
# @type [Client::Response::Reply]
|
25
|
+
reply = response.reply
|
26
|
+
|
27
|
+
if reply.ok?
|
28
|
+
@status = 'sent'
|
29
|
+
@id = reply.sms_id
|
30
|
+
true
|
31
|
+
else
|
32
|
+
@errors << reply.error_description
|
33
|
+
false
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def update_status
|
38
|
+
return status if id.nil?
|
39
|
+
|
40
|
+
response = Client.status(id)
|
41
|
+
return response.code unless response.success?
|
42
|
+
|
43
|
+
@status = response.reply.status.downcase
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
def validate!
|
49
|
+
raise ArgumentError, "Phone should be assigned to #{self.class}." if phone.nil?
|
50
|
+
|
51
|
+
unless phone.to_s =~ /^[0-9]{11}$/
|
52
|
+
raise ArgumentError, 'Phone number should contain only numbers. Minimum length is 11.'
|
53
|
+
end
|
54
|
+
|
55
|
+
raise ArgumentError, "Message should be specified to #{self.class}." if message.nil?
|
56
|
+
raise ArgumentError, "Originator should be specified to #{self.class}." if originator.nil?
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
data/lib/sms_traffic.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'zeitwerk'
|
2
|
+
|
3
|
+
loader = Zeitwerk::Loader.for_gem
|
4
|
+
loader.ignore("#{__dir__}/sms_traffic/version.rb")
|
5
|
+
loader.enable_reloading
|
6
|
+
loader.setup
|
7
|
+
|
8
|
+
require_relative 'sms_traffic/version'
|
9
|
+
|
10
|
+
module SmsTraffic
|
11
|
+
class << self
|
12
|
+
def configuration
|
13
|
+
@configuration ||= Configuration.instance
|
14
|
+
end
|
15
|
+
|
16
|
+
def configure(&_block)
|
17
|
+
yield(configuration) if block_given?
|
18
|
+
configuration.validate!
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
lib = File.expand_path('lib', __dir__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
|
4
|
+
require_relative 'lib/sms_traffic/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'sms_traffic_sdk'
|
8
|
+
spec.version = SmsTraffic::VERSION
|
9
|
+
spec.authors = ['David Rybolovlev']
|
10
|
+
spec.email = ['i@golifox.ru']
|
11
|
+
|
12
|
+
spec.summary = 'Send sms via SmsTraffic service.'
|
13
|
+
spec.description = 'Send sms via SmsTraffic service.'
|
14
|
+
spec.homepage = 'https://github.com/golifox/sms_traffic'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = 'exe'
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ['lib']
|
21
|
+
|
22
|
+
spec.required_ruby_version = '>= 2.7.0'
|
23
|
+
|
24
|
+
spec.add_dependency 'nokogiri', '~> 1.12'
|
25
|
+
spec.add_dependency 'nori', '~> 2.6'
|
26
|
+
spec.add_dependency 'zeitwerk', '~> 2.5'
|
27
|
+
|
28
|
+
spec.metadata['rubygems_mfa_required'] = 'true'
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sms_traffic_sdk
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- David Rybolovlev
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-07-15 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: nokogiri
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.12'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.12'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: nori
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '2.6'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '2.6'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: zeitwerk
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '2.5'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '2.5'
|
55
|
+
description: Send sms via SmsTraffic service.
|
56
|
+
email:
|
57
|
+
- i@golifox.ru
|
58
|
+
executables:
|
59
|
+
- konsierge-sms.rb
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".env.template"
|
64
|
+
- ".fasterer.yml"
|
65
|
+
- ".github/workflows/ci.yml"
|
66
|
+
- ".gitignore"
|
67
|
+
- ".rspec"
|
68
|
+
- ".rubocop.yml"
|
69
|
+
- ".tool-versions"
|
70
|
+
- Gemfile
|
71
|
+
- LICENSE
|
72
|
+
- README.md
|
73
|
+
- Rakefile
|
74
|
+
- bin/console
|
75
|
+
- bin/setup
|
76
|
+
- exe/konsierge-sms.rb
|
77
|
+
- lib/sms_traffic.rb
|
78
|
+
- lib/sms_traffic/client.rb
|
79
|
+
- lib/sms_traffic/client/deliver_reply.rb
|
80
|
+
- lib/sms_traffic/client/reply.rb
|
81
|
+
- lib/sms_traffic/client/response.rb
|
82
|
+
- lib/sms_traffic/client/status_reply.rb
|
83
|
+
- lib/sms_traffic/configuration.rb
|
84
|
+
- lib/sms_traffic/sms.rb
|
85
|
+
- lib/sms_traffic/version.rb
|
86
|
+
- sms_traffic_sdk.gemspec
|
87
|
+
homepage: https://github.com/golifox/sms_traffic
|
88
|
+
licenses:
|
89
|
+
- MIT
|
90
|
+
metadata:
|
91
|
+
rubygems_mfa_required: 'true'
|
92
|
+
post_install_message:
|
93
|
+
rdoc_options: []
|
94
|
+
require_paths:
|
95
|
+
- lib
|
96
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: 2.7.0
|
101
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
requirements: []
|
107
|
+
rubygems_version: 3.5.11
|
108
|
+
signing_key:
|
109
|
+
specification_version: 4
|
110
|
+
summary: Send sms via SmsTraffic service.
|
111
|
+
test_files: []
|