any_sms 0.3.0
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/.gitignore +9 -0
- data/.rspec +2 -0
- data/.rubocop.yml +16 -0
- data/.travis.yml +9 -0
- data/Gemfile +3 -0
- data/Guardfile +19 -0
- data/LICENSE.txt +21 -0
- data/README.md +276 -0
- data/Rakefile +6 -0
- data/any_sms.gemspec +38 -0
- data/lib/any_sms/backend/base.rb +37 -0
- data/lib/any_sms/backend/logger.rb +27 -0
- data/lib/any_sms/backend/null_sender.rb +11 -0
- data/lib/any_sms/configuration.rb +86 -0
- data/lib/any_sms/matchers/send_sms.rb +68 -0
- data/lib/any_sms/response.rb +28 -0
- data/lib/any_sms/sending.rb +40 -0
- data/lib/any_sms/version.rb +3 -0
- data/lib/any_sms.rb +11 -0
- metadata +176 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 4ea6a7e81eb0c54c44900ee3389fc9db1e150b54
|
|
4
|
+
data.tar.gz: 250a5d9943723104eb332ec503fb5a38a313a623
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 3ff7f28fb99ff628fe20bcfe44c59d6fe0e2cda9ec6c876af2d23d84694febc9a9b58e388404c5612a955758a31011637e78e58331bfe437361a4b3cf84bb92f
|
|
7
|
+
data.tar.gz: b51a1f1302d4eaf978fe5485924b145665ec9fb1597693098f35aaf25252107f504bd15efe630d5bbbeb64d27c396074fcfac80b28b80b887e3d977cf7a8a9fe
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
AllCops:
|
|
2
|
+
TargetRubyVersion: 2.1
|
|
3
|
+
|
|
4
|
+
Style/StringLiterals:
|
|
5
|
+
EnforcedStyle: double_quotes
|
|
6
|
+
ConsistentQuotesInMultiline: true
|
|
7
|
+
|
|
8
|
+
Metrics/LineLength:
|
|
9
|
+
Max: 90
|
|
10
|
+
|
|
11
|
+
Style/ClassAndModuleChildren:
|
|
12
|
+
Enabled: false
|
|
13
|
+
|
|
14
|
+
# I need to use singleton so gem stays simple
|
|
15
|
+
Style/ClassVars:
|
|
16
|
+
Enabled: false
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Guardfile
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
guard :rspec, cmd: "bundle exec rspec" do
|
|
2
|
+
require "guard/rspec/dsl"
|
|
3
|
+
dsl = Guard::RSpec::Dsl.new(self)
|
|
4
|
+
|
|
5
|
+
# RSpec files
|
|
6
|
+
rspec = dsl.rspec
|
|
7
|
+
watch(rspec.spec_helper) { rspec.spec_dir }
|
|
8
|
+
watch(rspec.spec_support) { rspec.spec_dir }
|
|
9
|
+
watch(rspec.spec_files)
|
|
10
|
+
|
|
11
|
+
# Lib files
|
|
12
|
+
watch(%r{^lib/any_sms.rb$}) { rspec.spec_dir }
|
|
13
|
+
watch(%r{^lib/(.+)\.rb$}) { |m| rspec.spec.call(m[1]) }
|
|
14
|
+
watch(%r{^spec/support/(.+)\.rb$}) { |m| rspec.spec.call(m[1]) }
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
guard "yard" do
|
|
18
|
+
watch(%r{lib/.+\.rb})
|
|
19
|
+
end
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2016 Fedcomp
|
|
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
|
|
13
|
+
all 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
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
# AnySMS
|
|
2
|
+
|
|
3
|
+
[](https://travis-ci.org/Fedcomp/any_sms)
|
|
4
|
+
[](https://badge.fury.io/rb/any_sms)
|
|
5
|
+
|
|
6
|
+
Unified way to send SMS in ruby!
|
|
7
|
+
Allows you to switch SMS services
|
|
8
|
+
without having to rewrite any code that actually sends SMS.
|
|
9
|
+
Supports multiple backends.
|
|
10
|
+
Sending SMS is not pain anymore!
|
|
11
|
+
|
|
12
|
+
## Installation and usage
|
|
13
|
+
|
|
14
|
+
Add this line to your application's Gemfile:
|
|
15
|
+
|
|
16
|
+
```ruby
|
|
17
|
+
gem "any_sms"
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Then somewhere in your initialization code:
|
|
21
|
+
|
|
22
|
+
```ruby
|
|
23
|
+
require "any_sms"
|
|
24
|
+
require "logger"
|
|
25
|
+
|
|
26
|
+
AnySMS.configure do |config|
|
|
27
|
+
config.register_backend :my_backend_name,
|
|
28
|
+
AnySMS::Backend::Logger,
|
|
29
|
+
logger: Logger.new(STDOUT),
|
|
30
|
+
severity: :info
|
|
31
|
+
|
|
32
|
+
config.default_backend = :my_backend_name
|
|
33
|
+
end
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Now, whenever you need to send SMS, just do:
|
|
37
|
+
|
|
38
|
+
```ruby
|
|
39
|
+
phone = "+10000000000"
|
|
40
|
+
text = "My sms text"
|
|
41
|
+
|
|
42
|
+
# Should print to console [SMS] +10000000000: text
|
|
43
|
+
AnySMS.send_sms(phone, text)
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Now your code is capable of sending sms.
|
|
47
|
+
Later you may add any sms-backend you want, or even write your own.
|
|
48
|
+
|
|
49
|
+
### Adding real sms backend
|
|
50
|
+
|
|
51
|
+
If you followed steps above, you code still doesn't *really* send sms.
|
|
52
|
+
It uses `AnySMS::Backend::Logger`
|
|
53
|
+
which actually just print sms contents to console.
|
|
54
|
+
To actually send sms you need *gem-provider*
|
|
55
|
+
or your own simple class.
|
|
56
|
+
|
|
57
|
+
Here's a list of my implementations for some sms services.
|
|
58
|
+
|
|
59
|
+
<table>
|
|
60
|
+
<tr>
|
|
61
|
+
<th>Gem name</th>
|
|
62
|
+
<th>Sms service name</th>
|
|
63
|
+
</tr>
|
|
64
|
+
<tr>
|
|
65
|
+
<td>
|
|
66
|
+
<a href="https://github.com/Fedcomp/any_sms-backend-aws">
|
|
67
|
+
any_sms-backend-aws
|
|
68
|
+
</a>
|
|
69
|
+
</td>
|
|
70
|
+
<td><a href="https://aws.amazon.com/ru/documentation/sns/">Amazon Web Services SNS</a></td>
|
|
71
|
+
</tr>
|
|
72
|
+
<tr>
|
|
73
|
+
<td>
|
|
74
|
+
<a href="https://github.com/Fedcomp/any_sms-backend-smsru">
|
|
75
|
+
any_sms-backend-smsru
|
|
76
|
+
</a> (russian)
|
|
77
|
+
</td>
|
|
78
|
+
<td><a href="https://sms.ru">sms.ru</a></td>
|
|
79
|
+
</tr>
|
|
80
|
+
</table>
|
|
81
|
+
|
|
82
|
+
These gems documentation should be self explanatory.
|
|
83
|
+
|
|
84
|
+
### Writing your own sms backend
|
|
85
|
+
|
|
86
|
+
Here's simple class that can be used by AnySMS:
|
|
87
|
+
|
|
88
|
+
```ruby
|
|
89
|
+
require "any_sms"
|
|
90
|
+
|
|
91
|
+
class AnySMS::Backend::MyCustomBackend < AnySMS::Backend::Base
|
|
92
|
+
def initialize(params = {})
|
|
93
|
+
# your initialization which parses params if needed.
|
|
94
|
+
# the params here is the ones you set in initializer
|
|
95
|
+
|
|
96
|
+
# (you may also use keyword arguments instead)
|
|
97
|
+
@token = params.delete(:token)
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def send_sms(phone, sms_text)
|
|
101
|
+
# your code to call your sms service
|
|
102
|
+
# or somehow else send actual sms
|
|
103
|
+
|
|
104
|
+
# if everything went fine, you may use helper from base class:
|
|
105
|
+
respond_with_status :success
|
|
106
|
+
|
|
107
|
+
# any other than :success response considered as failure
|
|
108
|
+
respond_with_status :not_enough_funds
|
|
109
|
+
|
|
110
|
+
# optionally you may return some metadata
|
|
111
|
+
respond_with_status :success, meta: { funds_left: 42 }
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
Then in initializer:
|
|
117
|
+
|
|
118
|
+
```ruby
|
|
119
|
+
require "any_sms"
|
|
120
|
+
require_relative "mycustombackend"
|
|
121
|
+
|
|
122
|
+
AnySMS.configure do |c|
|
|
123
|
+
c.register_backend :my_custom_backend,
|
|
124
|
+
AnySMS::Backend::MyCustomBackend,
|
|
125
|
+
token: ENV["token"]
|
|
126
|
+
|
|
127
|
+
c.default_backend = :my_custom_backend
|
|
128
|
+
end
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
Usage:
|
|
132
|
+
|
|
133
|
+
```ruby
|
|
134
|
+
# somewhere in your code
|
|
135
|
+
result = AnySMS.send_sms(phone, text)
|
|
136
|
+
|
|
137
|
+
if result.success?
|
|
138
|
+
# do stuff
|
|
139
|
+
else
|
|
140
|
+
# request failed
|
|
141
|
+
fail_status = result.status
|
|
142
|
+
# :not_enough_funds for example
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
# (optionally) Read metadata if any.
|
|
146
|
+
# Not recommended for control flow.
|
|
147
|
+
result.meta
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
### Multiple backends
|
|
151
|
+
|
|
152
|
+
You can specify which backend to use per call:
|
|
153
|
+
|
|
154
|
+
```ruby
|
|
155
|
+
require "any_sms"
|
|
156
|
+
require_relative "mycustombackend"
|
|
157
|
+
|
|
158
|
+
AnySMS.configure do |c|
|
|
159
|
+
c.register_backend :my_custom_backend,
|
|
160
|
+
AnySMS::Backend::MyCustomBackend,
|
|
161
|
+
token: ENV["token"]
|
|
162
|
+
|
|
163
|
+
c.register_backend :null_sender, AnySMS::Backend::NullSender
|
|
164
|
+
c.default_backend = :my_custom_backend
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
phone = "799999999"
|
|
168
|
+
text = "My sms text"
|
|
169
|
+
|
|
170
|
+
# Uses default backend
|
|
171
|
+
AnySMS.send_sms(phone, text)
|
|
172
|
+
|
|
173
|
+
# Uses backend you specify
|
|
174
|
+
AnySMS.send_sms(phone, text, backend: :null_sender)
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
### Real life example
|
|
178
|
+
|
|
179
|
+
If you develop application in group,
|
|
180
|
+
you probably don't want them all to send real SMS,
|
|
181
|
+
and instead you would prefer to emulate sending,
|
|
182
|
+
including SMS text preview (like SMS codes for registration).
|
|
183
|
+
|
|
184
|
+
However, on production you want
|
|
185
|
+
to actually send them using your service.
|
|
186
|
+
Here's how you can achieve that:
|
|
187
|
+
|
|
188
|
+
```ruby
|
|
189
|
+
require "any_sms"
|
|
190
|
+
require_relative "mycustombackend"
|
|
191
|
+
require_relative "mycustombackend2"
|
|
192
|
+
|
|
193
|
+
AnySMS.configure do |c|
|
|
194
|
+
if development?
|
|
195
|
+
c.register_backend :my_custom_backend,
|
|
196
|
+
AnySMS::Backend::Logger,
|
|
197
|
+
logger: Logger.new(STDOUT),
|
|
198
|
+
severity: :info
|
|
199
|
+
|
|
200
|
+
# You can also, for example, specify different formatter for second one
|
|
201
|
+
logger = Logger.new(STDOUT)
|
|
202
|
+
logger.formatter = proc do |severity, datetime, progname, msg|
|
|
203
|
+
"[MYBackend2]: #{msg}\n"
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
c.register_backend :my_custom_backend2,
|
|
207
|
+
AnySMS::Backend::Logger,
|
|
208
|
+
logger: logger,
|
|
209
|
+
severity: :info
|
|
210
|
+
end
|
|
211
|
+
|
|
212
|
+
if test?
|
|
213
|
+
# Null sender does nothing when called for sending sms
|
|
214
|
+
c.register_backend :my_custom_backend, AnySMS::Backend::NullSender
|
|
215
|
+
c.register_backend :my_custom_backend2, AnySMS::Backend::NullSender
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
if production?
|
|
219
|
+
c.register_backend :my_custom_backend,
|
|
220
|
+
AnySMS::Backend::MyCustomBackend,
|
|
221
|
+
token: ENV["token"]
|
|
222
|
+
|
|
223
|
+
c.register_backend :my_custom_backend2,
|
|
224
|
+
AnySMS::Backend::MyCustomBackend2,
|
|
225
|
+
token: ENV["token2"]
|
|
226
|
+
end
|
|
227
|
+
|
|
228
|
+
c.default_backend = :my_custom_backend
|
|
229
|
+
end
|
|
230
|
+
|
|
231
|
+
phone = "799999999"
|
|
232
|
+
text = "My sms text"
|
|
233
|
+
|
|
234
|
+
# Uses default backend
|
|
235
|
+
AnySMS.send_sms(phone, text)
|
|
236
|
+
|
|
237
|
+
# Uses backend you specify
|
|
238
|
+
AnySMS.send_sms(phone, text, backend: :my_custom_backend2)
|
|
239
|
+
|
|
240
|
+
# depending on your initializer it may use different backends (like in this example)
|
|
241
|
+
# in different environments.
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
Of course `development?`, `test?` and `production?` are not real methods.
|
|
245
|
+
You have to detect environment yourself somehow.
|
|
246
|
+
|
|
247
|
+
While possible, i strongly discourage to use more than two backends
|
|
248
|
+
(One default, another is required in certain situations for some reason).
|
|
249
|
+
It may make your code mess ;)
|
|
250
|
+
|
|
251
|
+
## Testing
|
|
252
|
+
|
|
253
|
+
I am planning to make rspec matcher to test if sms was sent.
|
|
254
|
+
For now you may just mock `AnySMS.send_sms` and check it was executed.
|
|
255
|
+
|
|
256
|
+
## Contributing
|
|
257
|
+
|
|
258
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/Fedcomp/any_sms
|
|
259
|
+
|
|
260
|
+
## Submitting a Pull Request
|
|
261
|
+
1. Fork the [official repository](https://github.com/Fedcomp/any_sms).
|
|
262
|
+
2. Create a topic branch.
|
|
263
|
+
3. Implement your feature or bug fix.
|
|
264
|
+
4. Add, commit, and push your changes.
|
|
265
|
+
5. Submit a pull request.
|
|
266
|
+
|
|
267
|
+
* Please add tests if you changed code. Contributions without tests won't be accepted.
|
|
268
|
+
* If you don't know how to add tests, please put in a PR and leave a comment
|
|
269
|
+
asking for help.
|
|
270
|
+
* Please don't update the Gem version.
|
|
271
|
+
|
|
272
|
+
( Inspired by https://github.com/thoughtbot/factory_girl/blob/master/CONTRIBUTING.md )
|
|
273
|
+
|
|
274
|
+
## License
|
|
275
|
+
|
|
276
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/any_sms.gemspec
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
lib = File.expand_path("../lib", __FILE__)
|
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
3
|
+
require "any_sms/version"
|
|
4
|
+
|
|
5
|
+
Gem::Specification.new do |spec|
|
|
6
|
+
spec.name = "any_sms"
|
|
7
|
+
spec.version = AnySMS::VERSION
|
|
8
|
+
spec.authors = ["Fedcomp"]
|
|
9
|
+
spec.email = ["aglergen@gmail.com"]
|
|
10
|
+
|
|
11
|
+
spec.summary = "Easily send sms using various sms backends!"
|
|
12
|
+
|
|
13
|
+
spec.homepage = "https://github.com/Fedcomp/any_sms"
|
|
14
|
+
spec.license = "MIT"
|
|
15
|
+
|
|
16
|
+
unless spec.respond_to?(:metadata)
|
|
17
|
+
raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
|
21
|
+
|
|
22
|
+
spec.files = `git ls-files -z`
|
|
23
|
+
.split("\x0")
|
|
24
|
+
.reject { |f| f.match(%r{^(test|spec|features)/}) }
|
|
25
|
+
spec.bindir = "exe"
|
|
26
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
27
|
+
spec.require_paths = ["lib"]
|
|
28
|
+
|
|
29
|
+
spec.add_development_dependency "bundler", "~> 1.12"
|
|
30
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
|
31
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
|
32
|
+
|
|
33
|
+
spec.add_development_dependency "guard", "~> 2.14"
|
|
34
|
+
spec.add_development_dependency "guard-rspec", "~> 4.7"
|
|
35
|
+
spec.add_development_dependency "pry-byebug", "~> 3.4"
|
|
36
|
+
spec.add_development_dependency "rubocop"
|
|
37
|
+
spec.add_development_dependency "guard-yard"
|
|
38
|
+
end
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
module AnySMS::Backend
|
|
2
|
+
# Base class for any sms provider service.
|
|
3
|
+
# Provides basic structure and helper methods.
|
|
4
|
+
# While not necessary to be subclassed now, may be necessary later.
|
|
5
|
+
class Base
|
|
6
|
+
# In initializer you may
|
|
7
|
+
# accept secrets which were defined in initializer
|
|
8
|
+
# or other configuration options if any.
|
|
9
|
+
#
|
|
10
|
+
# @param params [Hash] List of arguments received from configure code.
|
|
11
|
+
def initialize(params = {}); end
|
|
12
|
+
|
|
13
|
+
# Interface for sending sms.
|
|
14
|
+
# Every subclass should implement method itself.
|
|
15
|
+
# Raises error in default implementation.
|
|
16
|
+
#
|
|
17
|
+
# @param _phone [String] Phone number to send sms (not used in this implementation)
|
|
18
|
+
# @param _text [String] Sms text (not used in this implementation)
|
|
19
|
+
def send_sms(_phone, _text)
|
|
20
|
+
raise NotImplementedError,
|
|
21
|
+
"You should create your own class for every sms service you use"
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
protected
|
|
25
|
+
|
|
26
|
+
# Returns AnySMS::Reponse object with status and meta
|
|
27
|
+
#
|
|
28
|
+
# @param status [Symbol]
|
|
29
|
+
# Query status, any other than :success considered as failure
|
|
30
|
+
# @param meta [Hash]
|
|
31
|
+
# Optional metadata you can return from api or implementation
|
|
32
|
+
# @return [AnySMS::Reponse] Response object with meta and status
|
|
33
|
+
def respond_with_status(status, meta: nil)
|
|
34
|
+
AnySMS::Response.new(status: status, meta: meta)
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# Sms backend for logging outgoing sms instead of actually sending them
|
|
2
|
+
class AnySMS::Backend::Logger < AnySMS::Backend::Base
|
|
3
|
+
# Log level validation, all invalid values lead to ArgumentError.
|
|
4
|
+
# Has anyone heard how to receive log levels from ruby logger itself?
|
|
5
|
+
LOG_SEVERITY = [:debug, :info, :warn, :error, :fatal, :unknown].freeze
|
|
6
|
+
|
|
7
|
+
# @param logger [::Logger] Class implementing logger interface
|
|
8
|
+
# @param severity [Symbol] Severity to log with
|
|
9
|
+
def initialize(logger: Logger.new(STDOUT), severity: :info)
|
|
10
|
+
@logger = logger
|
|
11
|
+
@severity = severity
|
|
12
|
+
|
|
13
|
+
raise ArgumentError, "Invalid log severity" unless LOG_SEVERITY.include?(@severity)
|
|
14
|
+
|
|
15
|
+
return if @logger.respond_to?(@severity)
|
|
16
|
+
raise ArgumentError, "Class should implement logger interface"
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# Method that sends phone and text to logger
|
|
20
|
+
#
|
|
21
|
+
# @param phone [String] Phone number to send sms
|
|
22
|
+
# @param text [String] Sms text
|
|
23
|
+
def send_sms(phone, text)
|
|
24
|
+
@logger.send(@severity, "[SMS] #{phone}: #{text}")
|
|
25
|
+
respond_with_status :success
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# Sms backend for mocking sending.
|
|
2
|
+
# Purely for usage in tests.
|
|
3
|
+
class AnySMS::Backend::NullSender < AnySMS::Backend::Base
|
|
4
|
+
# Method that emulates sms sending. Does nothing.
|
|
5
|
+
#
|
|
6
|
+
# @param _phone [String] Phone number to send sms (not used in this implementation)
|
|
7
|
+
# @param _text [String] Sms text (not used in this implementation)
|
|
8
|
+
def send_sms(_phone, _text)
|
|
9
|
+
respond_with_status :success
|
|
10
|
+
end
|
|
11
|
+
end
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
# :nodoc:
|
|
2
|
+
module AnySMS
|
|
3
|
+
# @return [AnySMS::Configuration] object with configuration options
|
|
4
|
+
def self.config
|
|
5
|
+
@@config ||= Configuration.new
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
# Allows to configure AnySMS options and register backends
|
|
9
|
+
def self.configure
|
|
10
|
+
yield(config)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
# resets AnySMS configuration to default
|
|
14
|
+
def self.reset!
|
|
15
|
+
@@config = nil
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# Configuration object for AnySMS
|
|
19
|
+
class Configuration
|
|
20
|
+
# returns key of the default sms backend
|
|
21
|
+
attr_reader :default_backend
|
|
22
|
+
|
|
23
|
+
# returns list of registered sms backends
|
|
24
|
+
attr_reader :backends
|
|
25
|
+
|
|
26
|
+
def initialize
|
|
27
|
+
register_backend :null_sender, AnySMS::Backend::NullSender
|
|
28
|
+
self.default_backend = :null_sender
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# Specify default sms backend. It must be registered.
|
|
32
|
+
#
|
|
33
|
+
# @param value [Symbol] Backend key which will be used as default
|
|
34
|
+
def default_backend=(value)
|
|
35
|
+
raise ArgumentError, "default_backend must be a symbol!" unless value.is_a? Symbol
|
|
36
|
+
|
|
37
|
+
unless @backends.keys.include? value
|
|
38
|
+
raise ArgumentError, "Unregistered backend cannot be set as default!"
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
@default_backend = value
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# Register sms provider backend
|
|
45
|
+
#
|
|
46
|
+
# @param key [Symbol] Key for acessing backend in any part of AnySMS
|
|
47
|
+
# @param classname [Class] Real class implementation of sms backend
|
|
48
|
+
# @param params [Hash]
|
|
49
|
+
# Optional params for backend. Useful for passing tokens and options
|
|
50
|
+
def register_backend(key, classname, params = {})
|
|
51
|
+
raise ArgumentError, "backend key must be a symbol!" unless key.is_a? Symbol
|
|
52
|
+
|
|
53
|
+
unless classname.class == Class
|
|
54
|
+
raise ArgumentError, "backend class must be class (not instance or string)"
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
unless classname.method_defined? :send_sms
|
|
58
|
+
raise ArgumentError, "backend must provide method send_sms"
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
define_backend(key, classname, params)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# Removes registered sms backend
|
|
65
|
+
#
|
|
66
|
+
# @param key [Symbol] Key of already registered backend
|
|
67
|
+
def remove_backend(key)
|
|
68
|
+
if key == default_backend
|
|
69
|
+
raise ArgumentError, "Removing default_backend is prohibited"
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
@backends.delete key
|
|
73
|
+
true
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
private
|
|
77
|
+
|
|
78
|
+
def define_backend(key, classname, params)
|
|
79
|
+
@backends ||= {}
|
|
80
|
+
@backends[key] = {
|
|
81
|
+
class: classname,
|
|
82
|
+
params: params
|
|
83
|
+
}
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
end
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
require "rspec/matchers"
|
|
2
|
+
|
|
3
|
+
# rubocop:disable Metrics/BlockLength
|
|
4
|
+
RSpec::Matchers.define :send_sms do |options|
|
|
5
|
+
supports_block_expectations
|
|
6
|
+
|
|
7
|
+
match do |block|
|
|
8
|
+
options ||= {}
|
|
9
|
+
@phone = options.delete(:to)
|
|
10
|
+
@text = options.delete(:text)
|
|
11
|
+
|
|
12
|
+
# we need to know actual phone and text for failure message
|
|
13
|
+
called = false
|
|
14
|
+
allow(AnySMS).to receive(:send_sms) do |actual_phone, actual_text|
|
|
15
|
+
puts "called"
|
|
16
|
+
@actual_phone = actual_phone
|
|
17
|
+
@actual_text = actual_text
|
|
18
|
+
called = true
|
|
19
|
+
|
|
20
|
+
result
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
block.call
|
|
24
|
+
|
|
25
|
+
expect(called).to be(true)
|
|
26
|
+
if @phone && @text
|
|
27
|
+
expect(@actual_phone).to eq(@phone)
|
|
28
|
+
expect(@actual_text).to eq(@text)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
true
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
chain :and_return do |return_options|
|
|
35
|
+
@result = AnySMS::Response.new(return_options)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
failure_message do
|
|
39
|
+
message = "expected block to send "
|
|
40
|
+
|
|
41
|
+
if @phone && @text
|
|
42
|
+
message += "sms message" \
|
|
43
|
+
" to phone number \"#{@phone}\"" \
|
|
44
|
+
" with text \"#{@text}\""
|
|
45
|
+
else
|
|
46
|
+
message += "any sms message"
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
"#{message}, nothing was sent"
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
failure_message_when_negated do
|
|
53
|
+
"expected block to not send any sms message, " \
|
|
54
|
+
"instead sent sms to phone number \"#{@actual_phone}\" " \
|
|
55
|
+
"with message \"#{@actual_text}\""
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# Not tested
|
|
59
|
+
description do
|
|
60
|
+
"send sms"
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
private
|
|
64
|
+
|
|
65
|
+
def result
|
|
66
|
+
@result ||= AnySMS::Response.new(status: :success)
|
|
67
|
+
end
|
|
68
|
+
end
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# Response object.
|
|
2
|
+
# Generated on each AnySMS.send_sms by backend implementations.
|
|
3
|
+
class AnySMS::Response
|
|
4
|
+
# see initialize
|
|
5
|
+
attr_reader :status
|
|
6
|
+
|
|
7
|
+
# see initialize
|
|
8
|
+
attr_accessor :meta
|
|
9
|
+
|
|
10
|
+
# @param status [Symbol]
|
|
11
|
+
# Status of sms request. Anything other than *:success* considered as failure.
|
|
12
|
+
# @param meta [Hash]
|
|
13
|
+
# Meta information which optionally can be returned by backend.
|
|
14
|
+
def initialize(status:, meta: nil)
|
|
15
|
+
@status = status
|
|
16
|
+
@meta = meta
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# @return [Boolean] whether request was succesful or not.
|
|
20
|
+
def success?
|
|
21
|
+
@status == :success
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# @return [Boolean] whether request has failed or not.
|
|
25
|
+
def failed?
|
|
26
|
+
!success?
|
|
27
|
+
end
|
|
28
|
+
end
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# :nodoc:
|
|
2
|
+
module AnySMS
|
|
3
|
+
class << self
|
|
4
|
+
# Core of the gem, method responsible for sending sms
|
|
5
|
+
#
|
|
6
|
+
# @param phone [String] Phone number for sms
|
|
7
|
+
# @param text [String] Text for sms
|
|
8
|
+
# @param args [Hash] Additional options for delivery. Currently only :backend
|
|
9
|
+
def send_sms(phone, text, args = {})
|
|
10
|
+
backend_name = args.delete(:backend)
|
|
11
|
+
backend_class(backend_name).new(backend_params(backend_name))
|
|
12
|
+
.send_sms(phone, text)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
private
|
|
16
|
+
|
|
17
|
+
def backend_class(name)
|
|
18
|
+
return default_backend_class if name.nil?
|
|
19
|
+
|
|
20
|
+
if AnySMS.config.backends[name].nil?
|
|
21
|
+
raise ArgumentError, "#{name} backend is not registered"
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
AnySMS.config.backends[name][:class]
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def default_backend_class
|
|
28
|
+
AnySMS.config.backends[AnySMS.config.default_backend][:class]
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def backend_params(name)
|
|
32
|
+
return default_backend_params if name.nil?
|
|
33
|
+
AnySMS.config.backends[name][:params]
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def default_backend_params
|
|
37
|
+
AnySMS.config.backends[AnySMS.config.default_backend][:params]
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
data/lib/any_sms.rb
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
require "any_sms/version"
|
|
2
|
+
require "any_sms/backend/base"
|
|
3
|
+
require "any_sms/backend/null_sender"
|
|
4
|
+
require "any_sms/backend/logger"
|
|
5
|
+
require "any_sms/configuration"
|
|
6
|
+
require "any_sms/response"
|
|
7
|
+
require "any_sms/sending"
|
|
8
|
+
|
|
9
|
+
# :nodoc:
|
|
10
|
+
module AnySMS
|
|
11
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: any_sms
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.3.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Fedcomp
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2016-12-24 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: bundler
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '1.12'
|
|
20
|
+
type: :development
|
|
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: rake
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '10.0'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '10.0'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: rspec
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - "~>"
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '3.0'
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - "~>"
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '3.0'
|
|
55
|
+
- !ruby/object:Gem::Dependency
|
|
56
|
+
name: guard
|
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - "~>"
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: '2.14'
|
|
62
|
+
type: :development
|
|
63
|
+
prerelease: false
|
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - "~>"
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: '2.14'
|
|
69
|
+
- !ruby/object:Gem::Dependency
|
|
70
|
+
name: guard-rspec
|
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
|
72
|
+
requirements:
|
|
73
|
+
- - "~>"
|
|
74
|
+
- !ruby/object:Gem::Version
|
|
75
|
+
version: '4.7'
|
|
76
|
+
type: :development
|
|
77
|
+
prerelease: false
|
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
79
|
+
requirements:
|
|
80
|
+
- - "~>"
|
|
81
|
+
- !ruby/object:Gem::Version
|
|
82
|
+
version: '4.7'
|
|
83
|
+
- !ruby/object:Gem::Dependency
|
|
84
|
+
name: pry-byebug
|
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
|
86
|
+
requirements:
|
|
87
|
+
- - "~>"
|
|
88
|
+
- !ruby/object:Gem::Version
|
|
89
|
+
version: '3.4'
|
|
90
|
+
type: :development
|
|
91
|
+
prerelease: false
|
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
93
|
+
requirements:
|
|
94
|
+
- - "~>"
|
|
95
|
+
- !ruby/object:Gem::Version
|
|
96
|
+
version: '3.4'
|
|
97
|
+
- !ruby/object:Gem::Dependency
|
|
98
|
+
name: rubocop
|
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
|
100
|
+
requirements:
|
|
101
|
+
- - ">="
|
|
102
|
+
- !ruby/object:Gem::Version
|
|
103
|
+
version: '0'
|
|
104
|
+
type: :development
|
|
105
|
+
prerelease: false
|
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
107
|
+
requirements:
|
|
108
|
+
- - ">="
|
|
109
|
+
- !ruby/object:Gem::Version
|
|
110
|
+
version: '0'
|
|
111
|
+
- !ruby/object:Gem::Dependency
|
|
112
|
+
name: guard-yard
|
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
|
114
|
+
requirements:
|
|
115
|
+
- - ">="
|
|
116
|
+
- !ruby/object:Gem::Version
|
|
117
|
+
version: '0'
|
|
118
|
+
type: :development
|
|
119
|
+
prerelease: false
|
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
121
|
+
requirements:
|
|
122
|
+
- - ">="
|
|
123
|
+
- !ruby/object:Gem::Version
|
|
124
|
+
version: '0'
|
|
125
|
+
description:
|
|
126
|
+
email:
|
|
127
|
+
- aglergen@gmail.com
|
|
128
|
+
executables: []
|
|
129
|
+
extensions: []
|
|
130
|
+
extra_rdoc_files: []
|
|
131
|
+
files:
|
|
132
|
+
- ".gitignore"
|
|
133
|
+
- ".rspec"
|
|
134
|
+
- ".rubocop.yml"
|
|
135
|
+
- ".travis.yml"
|
|
136
|
+
- Gemfile
|
|
137
|
+
- Guardfile
|
|
138
|
+
- LICENSE.txt
|
|
139
|
+
- README.md
|
|
140
|
+
- Rakefile
|
|
141
|
+
- any_sms.gemspec
|
|
142
|
+
- lib/any_sms.rb
|
|
143
|
+
- lib/any_sms/backend/base.rb
|
|
144
|
+
- lib/any_sms/backend/logger.rb
|
|
145
|
+
- lib/any_sms/backend/null_sender.rb
|
|
146
|
+
- lib/any_sms/configuration.rb
|
|
147
|
+
- lib/any_sms/matchers/send_sms.rb
|
|
148
|
+
- lib/any_sms/response.rb
|
|
149
|
+
- lib/any_sms/sending.rb
|
|
150
|
+
- lib/any_sms/version.rb
|
|
151
|
+
homepage: https://github.com/Fedcomp/any_sms
|
|
152
|
+
licenses:
|
|
153
|
+
- MIT
|
|
154
|
+
metadata:
|
|
155
|
+
allowed_push_host: https://rubygems.org
|
|
156
|
+
post_install_message:
|
|
157
|
+
rdoc_options: []
|
|
158
|
+
require_paths:
|
|
159
|
+
- lib
|
|
160
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
161
|
+
requirements:
|
|
162
|
+
- - ">="
|
|
163
|
+
- !ruby/object:Gem::Version
|
|
164
|
+
version: '0'
|
|
165
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
166
|
+
requirements:
|
|
167
|
+
- - ">="
|
|
168
|
+
- !ruby/object:Gem::Version
|
|
169
|
+
version: '0'
|
|
170
|
+
requirements: []
|
|
171
|
+
rubyforge_project:
|
|
172
|
+
rubygems_version: 2.5.1
|
|
173
|
+
signing_key:
|
|
174
|
+
specification_version: 4
|
|
175
|
+
summary: Easily send sms using various sms backends!
|
|
176
|
+
test_files: []
|