active_sms 0.2.0 → 0.2.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 +4 -4
- data/.rubocop.yml +3 -0
- data/README.md +65 -21
- data/active_sms.gemspec +0 -1
- data/lib/active_sms.rb +1 -1
- data/lib/active_sms/backend/base.rb +12 -5
- data/lib/active_sms/backend/logger.rb +6 -6
- data/lib/active_sms/backend/null_sender.rb +5 -5
- data/lib/active_sms/configuration.rb +6 -5
- data/lib/active_sms/response.rb +11 -3
- data/lib/active_sms/sending.rb +3 -3
- data/lib/active_sms/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 59e9b08c771768bd28c41dd03a642630f202797d
|
4
|
+
data.tar.gz: 2894e6c82dd8e1c5aae0fe5bfb6548c748f17fbe
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a0d690df8f46ccd4496df62a2104c8fb83d032e2430e8cbeeeeb720ad1afe2a7aa3cca21150c6873c06623cda55e02d28848f88f74bb0fa06709fabdeeea93a4
|
7
|
+
data.tar.gz: 66c9c108d4f6bb4cd20dc73eb234f1a7e8174c840b6a9be38dc702ef40dac3d278dd6814d20fe4d7e15412b14bc70d5ebaf9827317ef8f002dd2c762336d3625
|
data/.rubocop.yml
CHANGED
data/README.md
CHANGED
@@ -20,24 +20,27 @@ gem "active_sms"
|
|
20
20
|
Then somewhere in your initialization code:
|
21
21
|
|
22
22
|
```ruby
|
23
|
+
require "active_sms"
|
24
|
+
require "logger"
|
25
|
+
|
23
26
|
ActiveSMS.configure do |config|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
27
|
+
config.register_backend :my_backend_name,
|
28
|
+
ActiveSMS::Backend::Logger,
|
29
|
+
logger: Logger.new(STDOUT),
|
30
|
+
severity: :info
|
28
31
|
|
29
|
-
|
32
|
+
config.default_backend = :my_backend_name
|
30
33
|
end
|
31
34
|
```
|
32
35
|
|
33
36
|
Now, whenever you need to send SMS, just do:
|
34
37
|
|
35
38
|
```ruby
|
36
|
-
phone = "
|
39
|
+
phone = "+10000000000"
|
37
40
|
text = "My sms text"
|
38
41
|
|
39
|
-
# Should print to console [SMS]
|
40
|
-
ActiveSMS.send_sms(
|
42
|
+
# Should print to console [SMS] +10000000000: text
|
43
|
+
ActiveSMS.send_sms(phone, text)
|
41
44
|
```
|
42
45
|
|
43
46
|
Now your code is capable of sending sms.
|
@@ -46,29 +49,37 @@ Later you may add any sms-backend you want, or even write your own.
|
|
46
49
|
### Adding real sms backend
|
47
50
|
|
48
51
|
If you followed steps above, you code still doesn't *really* send sms.
|
49
|
-
It uses `ActiveSMS::Backend::
|
50
|
-
which actually
|
51
|
-
To actually send sms you need
|
52
|
-
or
|
52
|
+
It uses `ActiveSMS::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.
|
53
56
|
|
54
|
-
|
57
|
+
Here's a list of my implementations for some sms services.
|
55
58
|
|
56
59
|
<table>
|
57
60
|
<tr>
|
58
61
|
<th>Gem name</th>
|
59
62
|
<th>Sms service name</th>
|
60
63
|
</tr>
|
64
|
+
<tr>
|
65
|
+
<td>
|
66
|
+
<a href="https://github.com/Fedcomp/active_sms-backend-aws">
|
67
|
+
active_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>
|
61
72
|
<tr>
|
62
73
|
<td>
|
63
74
|
<a href="https://github.com/Fedcomp/active_sms-backend-smsru">
|
64
75
|
active_sms-backend-smsru
|
65
|
-
</a>
|
76
|
+
</a> (russian)
|
66
77
|
</td>
|
67
|
-
<td><a href="https://sms.ru">sms
|
78
|
+
<td><a href="https://sms.ru">sms.ru</a></td>
|
68
79
|
</tr>
|
69
80
|
</table>
|
70
81
|
|
71
|
-
|
82
|
+
These gems documentation should be self explanatory.
|
72
83
|
|
73
84
|
### Writing your own sms backend
|
74
85
|
|
@@ -82,18 +93,22 @@ class ActiveSMS::Backend::MyCustomBackend < ActiveSMS::Backend::Base
|
|
82
93
|
# your initialization which parses params if needed.
|
83
94
|
# the params here is the ones you set in initializer
|
84
95
|
|
96
|
+
# (you may also use keyword arguments instead)
|
85
97
|
@token = params.delete(:token)
|
86
98
|
end
|
87
99
|
|
88
100
|
def send_sms(phone, sms_text)
|
89
101
|
# your code to call your sms service
|
90
|
-
# or somehow send actual sms
|
102
|
+
# or somehow else send actual sms
|
91
103
|
|
92
104
|
# if everything went fine, you may use helper from base class:
|
93
105
|
respond_with_status :success
|
94
106
|
|
95
|
-
#
|
107
|
+
# any other than :success response considered as failure
|
96
108
|
respond_with_status :not_enough_funds
|
109
|
+
|
110
|
+
# optionally you may return some metadata
|
111
|
+
respond_with_status :success, meta: { funds_left: 42 }
|
97
112
|
end
|
98
113
|
end
|
99
114
|
```
|
@@ -101,6 +116,9 @@ end
|
|
101
116
|
Then in initializer:
|
102
117
|
|
103
118
|
```ruby
|
119
|
+
require "active_sms"
|
120
|
+
require_relative "mycustombackend"
|
121
|
+
|
104
122
|
ActiveSMS.configure do |c|
|
105
123
|
c.register_backend :my_custom_backend,
|
106
124
|
ActiveSMS::Backend::MyCustomBackend,
|
@@ -123,6 +141,10 @@ else
|
|
123
141
|
fail_status = result.status
|
124
142
|
# :not_enough_funds for example
|
125
143
|
end
|
144
|
+
|
145
|
+
# (optionally) Read metadata if any.
|
146
|
+
# Not recommended for control flow.
|
147
|
+
result.meta
|
126
148
|
```
|
127
149
|
|
128
150
|
### Multiple backends
|
@@ -130,6 +152,9 @@ end
|
|
130
152
|
You can specify which backend to use per call:
|
131
153
|
|
132
154
|
```ruby
|
155
|
+
require "active_sms"
|
156
|
+
require_relative "mycustombackend"
|
157
|
+
|
133
158
|
ActiveSMS.configure do |c|
|
134
159
|
c.register_backend :my_custom_backend,
|
135
160
|
ActiveSMS::Backend::MyCustomBackend,
|
@@ -161,6 +186,10 @@ to actually send them using your service.
|
|
161
186
|
Here's how you can achieve that:
|
162
187
|
|
163
188
|
```ruby
|
189
|
+
require "active_sms"
|
190
|
+
require_relative "mycustombackend"
|
191
|
+
require_relative "mycustombackend2"
|
192
|
+
|
164
193
|
ActiveSMS.configure do |c|
|
165
194
|
if development?
|
166
195
|
c.register_backend :my_custom_backend,
|
@@ -168,7 +197,7 @@ ActiveSMS.configure do |c|
|
|
168
197
|
logger: Logger.new(STDOUT),
|
169
198
|
severity: :info
|
170
199
|
|
171
|
-
# You can also specify different formatter for second one
|
200
|
+
# You can also, for example, specify different formatter for second one
|
172
201
|
logger = Logger.new(STDOUT)
|
173
202
|
logger.formatter = proc do |severity, datetime, progname, msg|
|
174
203
|
"[MYBackend2]: #{msg}\n"
|
@@ -181,6 +210,7 @@ ActiveSMS.configure do |c|
|
|
181
210
|
end
|
182
211
|
|
183
212
|
if test?
|
213
|
+
# Null sender does nothing when called for sending sms
|
184
214
|
c.register_backend :my_custom_backend, ActiveSMS::Backend::NullSender
|
185
215
|
c.register_backend :my_custom_backend2, ActiveSMS::Backend::NullSender
|
186
216
|
end
|
@@ -211,8 +241,8 @@ ActiveSMS.send_sms(phone, text, backend: :my_custom_backend2)
|
|
211
241
|
# in different environments.
|
212
242
|
```
|
213
243
|
|
214
|
-
Of course `development?` and `production?`
|
215
|
-
You have to detect environment yourself.
|
244
|
+
Of course `development?`, `test?` and `production?` are not real methods.
|
245
|
+
You have to detect environment yourself somehow.
|
216
246
|
|
217
247
|
While possible, i strongly discourage to use more than two backends
|
218
248
|
(One default, another is required in certain situations for some reason).
|
@@ -227,6 +257,20 @@ For now you may just mock `ActiveSMS.send_sms` and check it was executed.
|
|
227
257
|
|
228
258
|
Bug reports and pull requests are welcome on GitHub at https://github.com/Fedcomp/active_sms
|
229
259
|
|
260
|
+
## Submitting a Pull Request
|
261
|
+
1. Fork the [official repository](https://github.com/Fedcomp/active_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
|
+
|
230
274
|
## License
|
231
275
|
|
232
276
|
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/active_sms.gemspec
CHANGED
data/lib/active_sms.rb
CHANGED
@@ -7,7 +7,7 @@ module ActiveSMS::Backend
|
|
7
7
|
# accept secrets which were defined in initializer
|
8
8
|
# or other configuration options if any.
|
9
9
|
#
|
10
|
-
# @params [Hash] List of arguments received from configure code.
|
10
|
+
# @param params [Hash] List of arguments received from configure code.
|
11
11
|
def initialize(params = {})
|
12
12
|
end
|
13
13
|
|
@@ -15,8 +15,8 @@ module ActiveSMS::Backend
|
|
15
15
|
# Every subclass should implement method itself.
|
16
16
|
# Raises error in default implementation.
|
17
17
|
#
|
18
|
-
# @_phone [String] Phone number to send sms (not used in this implementation)
|
19
|
-
# @_text [String] Sms text (not used in this implementation)
|
18
|
+
# @param _phone [String] Phone number to send sms (not used in this implementation)
|
19
|
+
# @param _text [String] Sms text (not used in this implementation)
|
20
20
|
def send_sms(_phone, _text)
|
21
21
|
raise NotImplementedError,
|
22
22
|
"You should create your own class for every sms service you use"
|
@@ -24,8 +24,15 @@ module ActiveSMS::Backend
|
|
24
24
|
|
25
25
|
protected
|
26
26
|
|
27
|
-
|
28
|
-
|
27
|
+
# Returns ActiveSMS::Reponse object with status and meta
|
28
|
+
#
|
29
|
+
# @param status [Symbol]
|
30
|
+
# Query status, any other than :success considered as failure
|
31
|
+
# @param meta [Hash]
|
32
|
+
# Optional metadata you can return from api or implementation
|
33
|
+
# @return [ActiveSMS::Reponse] Response object with meta and status
|
34
|
+
def respond_with_status(status, meta: nil)
|
35
|
+
ActiveSMS::Response.new(status: status, meta: meta)
|
29
36
|
end
|
30
37
|
end
|
31
38
|
end
|
@@ -1,11 +1,11 @@
|
|
1
|
-
# Sms backend for logging outgoing sms
|
2
|
-
# instead of actually sending them
|
1
|
+
# Sms backend for logging outgoing sms instead of actually sending them
|
3
2
|
class ActiveSMS::Backend::Logger < ActiveSMS::Backend::Base
|
4
|
-
#
|
3
|
+
# Log level validation, all invalid values lead to ArgumentError.
|
4
|
+
# Has anyone heard how to receive log levels from ruby logger itself?
|
5
5
|
LOG_SEVERITY = [:debug, :info, :warn, :error, :fatal, :unknown].freeze
|
6
6
|
|
7
7
|
# @param logger [::Logger] Class implementing logger interface
|
8
|
-
# @param severity [Symbol]
|
8
|
+
# @param severity [Symbol] Severity to log with
|
9
9
|
def initialize(logger: Logger.new(STDOUT), severity: :info)
|
10
10
|
@logger = logger
|
11
11
|
@severity = severity
|
@@ -18,8 +18,8 @@ class ActiveSMS::Backend::Logger < ActiveSMS::Backend::Base
|
|
18
18
|
|
19
19
|
# Method that sends phone and text to logger
|
20
20
|
#
|
21
|
-
# @phone [String] Phone number to send sms
|
22
|
-
# @text [String] Sms text
|
21
|
+
# @param phone [String] Phone number to send sms
|
22
|
+
# @param text [String] Sms text
|
23
23
|
def send_sms(phone, text)
|
24
24
|
@logger.send(@severity, "[SMS] #{phone}: #{text}")
|
25
25
|
respond_with_status :success
|
@@ -1,10 +1,10 @@
|
|
1
|
-
# Sms backend
|
2
|
-
# Purely for usage in tests
|
1
|
+
# Sms backend for mocking sending.
|
2
|
+
# Purely for usage in tests.
|
3
3
|
class ActiveSMS::Backend::NullSender < ActiveSMS::Backend::Base
|
4
|
-
# Method that emulates sms sending. Does nothing.
|
4
|
+
# Method that emulates sms sending. Does nothing.
|
5
5
|
#
|
6
|
-
# @_phone [String] Phone number to send sms (not used in this implementation)
|
7
|
-
# @_text [String] Sms text (not used in this implementation)
|
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
8
|
def send_sms(_phone, _text)
|
9
9
|
respond_with_status :success
|
10
10
|
end
|
@@ -30,7 +30,7 @@ module ActiveSMS
|
|
30
30
|
|
31
31
|
# Specify default sms backend. It must be registered.
|
32
32
|
#
|
33
|
-
# @value [Symbol] Backend key which will be used as default
|
33
|
+
# @param value [Symbol] Backend key which will be used as default
|
34
34
|
def default_backend=(value)
|
35
35
|
raise ArgumentError, "default_backend must be a symbol!" unless value.is_a? Symbol
|
36
36
|
|
@@ -43,9 +43,10 @@ module ActiveSMS
|
|
43
43
|
|
44
44
|
# Register sms provider backend
|
45
45
|
#
|
46
|
-
# @key [Symbol] Key for acessing backend in any part of ActiveSMS
|
47
|
-
# @classname [Class] Real class implementation of sms backend
|
48
|
-
# @params [Hash]
|
46
|
+
# @param key [Symbol] Key for acessing backend in any part of ActiveSMS
|
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
|
49
50
|
def register_backend(key, classname, params = {})
|
50
51
|
raise ArgumentError, "backend key must be a symbol!" unless key.is_a? Symbol
|
51
52
|
|
@@ -62,7 +63,7 @@ module ActiveSMS
|
|
62
63
|
|
63
64
|
# Removes registered sms backend
|
64
65
|
#
|
65
|
-
# @key [Symbol] Key of already registered backend
|
66
|
+
# @param key [Symbol] Key of already registered backend
|
66
67
|
def remove_backend(key)
|
67
68
|
if key == default_backend
|
68
69
|
raise ArgumentError, "Removing default_backend is prohibited"
|
data/lib/active_sms/response.rb
CHANGED
@@ -1,11 +1,19 @@
|
|
1
1
|
# Response object.
|
2
2
|
# Generated on each ActiveSMS.send_sms by backend implementations.
|
3
3
|
class ActiveSMS::Response
|
4
|
-
#
|
4
|
+
# see initialize
|
5
5
|
attr_reader :status
|
6
6
|
|
7
|
-
|
8
|
-
|
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
|
9
17
|
end
|
10
18
|
|
11
19
|
# @return [Boolean] whether request was succesful or not.
|
data/lib/active_sms/sending.rb
CHANGED
@@ -3,9 +3,9 @@ module ActiveSMS
|
|
3
3
|
class << self
|
4
4
|
# Core of the gem, method responsible for sending sms
|
5
5
|
#
|
6
|
-
# @phone [String] Phone number for sms
|
7
|
-
# @text [String] Text for sms
|
8
|
-
# @args [Hash] Additional options for delivery. Currently only :backend
|
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
9
|
def send_sms(phone, text, args = {})
|
10
10
|
backend_name = args.delete(:backend)
|
11
11
|
backend_class(backend_name).new(backend_params(backend_name))
|
data/lib/active_sms/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_sms
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Fedcomp
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-10-
|
11
|
+
date: 2016-10-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|