textris 0.1.0 → 0.1.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/README.md +91 -35
- data/lib/textris.rb +1 -0
- data/lib/textris/delivery.rb +9 -7
- data/lib/textris/delivery/base.rb +13 -0
- data/lib/textris/delivery/mail.rb +1 -7
- data/lib/textris/delivery/test.rb +1 -7
- data/lib/textris/message.rb +4 -2
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dec9ef9f16f25185395f550204e2e5afbd067d8b
|
4
|
+
data.tar.gz: b5f8bf92434c2e95ff71a0bf6d9ee2a4692b0091
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5c721b8a4f1e67a3e62ee457869f8afb0f83c39f92e979dc7c63fa1300a03538b9a5ac1b9917b7e39ea44ad9ff89fca1734cedd37d34185c99655d5e6420e2f9
|
7
|
+
data.tar.gz: 4e723b946fbd68b08ef751d2785171a24a0a4c66fec6a1fd6de67cc4992c44952ebdc152321dffb4f6fc9034961eaf510a9579dbdead94534b372ec6055672f1
|
data/README.md
CHANGED
@@ -4,17 +4,22 @@ Simple gem for implementing texter classes which allow sending SMS messages in s
|
|
4
4
|
|
5
5
|
Unlike similar gems, **Textris** has some unique features:
|
6
6
|
|
7
|
-
- phone number E164 validation and normalization with the [Phony](https://github.com/floere/phony) gem
|
8
|
-
- multiple, per-environment configurable delivery methods
|
9
7
|
- e-mail proxy allowing to inspect messages using [Mailinator](https://mailinator.com/) or similar service
|
8
|
+
- phone number E164 validation and normalization with the [Phony](https://github.com/floere/phony) gem
|
9
|
+
- multiple, per-environment configurable and chainable delivery methods
|
10
|
+
- extensible with any number of custom delivery methods (also chainable)
|
10
11
|
- support for testing using self-explanatory `Textris::Base.deliveries`
|
11
12
|
- simple, extensible code written from the ground up instead of copying *ActionMailer*
|
12
13
|
|
14
|
+
Currently, this gem comes with `test` and `mail` delivery methods, so there's no method for any real SMS gateway yet. Still, you can easily implement your own - see the [Custom delivery methods](#custom-delivery-methods) chapter below.
|
15
|
+
|
13
16
|
## Installation
|
14
17
|
|
15
18
|
Add to `Gemfile`:
|
16
19
|
|
17
|
-
|
20
|
+
```ruby
|
21
|
+
gem 'textris'
|
22
|
+
```
|
18
23
|
|
19
24
|
And run:
|
20
25
|
|
@@ -24,43 +29,87 @@ And run:
|
|
24
29
|
|
25
30
|
Place texter classes in `app/texters` (e.g. `app/texters/user_texter.rb`):
|
26
31
|
|
27
|
-
|
28
|
-
|
32
|
+
```ruby
|
33
|
+
class UserTexter < Textris::Base
|
34
|
+
default :from => "Our Team <+48 666-777-888>"
|
29
35
|
|
30
|
-
|
31
|
-
|
36
|
+
def welcome(user)
|
37
|
+
@user = user
|
32
38
|
|
33
|
-
|
34
|
-
|
35
|
-
|
39
|
+
text :to => @user.phone
|
40
|
+
end
|
41
|
+
end
|
42
|
+
```
|
36
43
|
|
37
|
-
Place relevant view templates in `app/views/<texter_name>/<action_name>.text
|
44
|
+
Place relevant view templates in `app/views/<texter_name>/<action_name>.text.*` (e.g. `app/views/user_texter/welcome.text.erb`):
|
38
45
|
|
39
|
-
|
46
|
+
```erb
|
47
|
+
Welcome to our system, <%= @user.name %>!
|
48
|
+
```
|
40
49
|
|
41
50
|
Invoke them from application logic:
|
42
51
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
52
|
+
```ruby
|
53
|
+
class User < ActiveRecord::Base
|
54
|
+
after_create do
|
55
|
+
UserTexter.welcome(self).deliver
|
56
|
+
end
|
57
|
+
end
|
58
|
+
```
|
59
|
+
|
60
|
+
### Custom delivery methods
|
61
|
+
|
62
|
+
Place desired delivery method in `app/deliveries/<name>_delivery.rb` (e.g. `app/deliveries/my_provider_delivery.rb`):
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
class MyProviderDelivery < Textris::Delivery::Base
|
66
|
+
# Implement sending message to single phone number
|
67
|
+
self.send_message(phone, message)
|
68
|
+
some_send_method(:phone => phone, :text => message.content)
|
69
|
+
end
|
70
|
+
|
71
|
+
# ...or implement sending message to multiple phone numbers at once
|
72
|
+
self.send_message_to_all(message)
|
73
|
+
other_send_method(:phone_array => message.to, :text => message.content)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
```
|
77
|
+
|
78
|
+
> **NOTE**: You can also place your custom deliveries in `app/texters` if you don't want to clutter the *app* directory too much.
|
79
|
+
|
80
|
+
Only one of methods above must be implemented for the delivery class to work. In case of multiple phone numbers and no implementation of *send_message_to_all*, the *send_message* method will be invoked multiple times.
|
81
|
+
|
82
|
+
After implementing your own deliveries, you can activate them by setting app configuration:
|
83
|
+
|
84
|
+
```ruby
|
85
|
+
# Use your new delivery
|
86
|
+
config.textris_delivery_method = :my_provider
|
87
|
+
|
88
|
+
# Chain your new delivery with others, including stock ones
|
89
|
+
config.textris_delivery_method = [:my_provider, :mail]
|
90
|
+
```
|
48
91
|
|
49
92
|
## Testing
|
50
93
|
|
51
94
|
Access all messages that were sent with the `:test` delivery:
|
52
95
|
|
53
|
-
|
96
|
+
```ruby
|
97
|
+
Textris::Base.deliveries
|
98
|
+
```
|
54
99
|
|
55
100
|
You may want to clear the delivery queue before each test:
|
56
101
|
|
57
|
-
|
58
|
-
|
59
|
-
|
102
|
+
```ruby
|
103
|
+
before(:each) do
|
104
|
+
Textris::Base.deliveries.clear
|
105
|
+
end
|
106
|
+
```
|
60
107
|
|
61
108
|
Keep in mind that messages targeting multiple phone numbers, like:
|
62
109
|
|
63
|
-
|
110
|
+
```ruby
|
111
|
+
text :to => ['48111222333', '48222333444']
|
112
|
+
```
|
64
113
|
|
65
114
|
will yield multiple message deliveries, each for specific phone number.
|
66
115
|
|
@@ -70,25 +119,32 @@ You can change default settings by placing them in any of environment files, lik
|
|
70
119
|
|
71
120
|
Choose the delivery method:
|
72
121
|
|
73
|
-
|
74
|
-
|
122
|
+
```ruby
|
123
|
+
# Don't send anything, access your messages via Textris::Base.deliveries
|
124
|
+
config.textris_delivery_method = :test
|
125
|
+
|
126
|
+
# Send e-mails instead of SMSes in order to inspect their content
|
127
|
+
config.textris_delivery_method = :mail
|
75
128
|
|
76
|
-
|
77
|
-
|
129
|
+
# Chain multiple delivery methods (e.g. to have e-mail backups of your messages)
|
130
|
+
config.textris_delivery_method = [:mail, :test]
|
131
|
+
```
|
78
132
|
|
79
|
-
Configure the mail delivery:
|
133
|
+
Configure the mail delivery with custom templates:
|
80
134
|
|
81
|
-
|
82
|
-
|
135
|
+
```ruby
|
136
|
+
# E-mail sender, here: "our-team-48666777888@test.app-name.com"
|
137
|
+
config.textris_mail_from_template = '%{from_name:d}-%{from_phone}@%{env:d}.%{app:d}.com'
|
83
138
|
|
84
|
-
|
85
|
-
|
139
|
+
# E-mail target, here: "app-name-test-48111222333-texts@mailinator.com"
|
140
|
+
config.textris_mail_to_template = '%{app:d}-%{env:d}-%{to_phone}-texts@mailinator.com'
|
86
141
|
|
87
|
-
|
88
|
-
|
142
|
+
# E-mail subject, here: "User texter: Welcome"
|
143
|
+
config.textris_mail_subject_template = '%{texter:dh} texter: %{action:h}'
|
89
144
|
|
90
|
-
|
91
|
-
|
145
|
+
# E-mail body, here: "Welcome to our system, Mr Jones!"
|
146
|
+
config.textris_mail_body_template = '%{content}'
|
147
|
+
```
|
92
148
|
|
93
149
|
### Template interpolation
|
94
150
|
|
data/lib/textris.rb
CHANGED
data/lib/textris/delivery.rb
CHANGED
@@ -3,14 +3,16 @@ module Textris
|
|
3
3
|
module_function
|
4
4
|
|
5
5
|
def get
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
::Textris::Delivery::Test
|
11
|
-
else
|
12
|
-
::Textris::Delivery::Test
|
6
|
+
methods = Rails.application.config.try(:textris_delivery_method)
|
7
|
+
methods = [*methods].compact
|
8
|
+
if methods.blank?
|
9
|
+
methods = [:test]
|
13
10
|
end
|
11
|
+
|
12
|
+
methods.map do |method|
|
13
|
+
"Textris::Delivery::#{method.to_s.camelize}".safe_constantize ||
|
14
|
+
"#{method.to_s.camelize}Delivery".safe_constantize
|
15
|
+
end.compact
|
14
16
|
end
|
15
17
|
end
|
16
18
|
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
module Textris
|
2
2
|
module Delivery
|
3
|
-
class Mail
|
3
|
+
class Mail < Textris::Delivery::Base
|
4
4
|
class Mailer < ActionMailer::Base
|
5
5
|
def notify(from, to, subject, body)
|
6
6
|
mail :from => from, :to => to, :subject => subject, :body => body
|
@@ -8,12 +8,6 @@ module Textris
|
|
8
8
|
end
|
9
9
|
|
10
10
|
class << self
|
11
|
-
def send_message_to_all(message)
|
12
|
-
message.to.each do |to|
|
13
|
-
send_message(to, message)
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
11
|
private
|
18
12
|
|
19
13
|
def send_message(to, message)
|
data/lib/textris/message.rb
CHANGED
@@ -26,8 +26,10 @@ module Textris
|
|
26
26
|
end
|
27
27
|
|
28
28
|
def deliver
|
29
|
-
|
30
|
-
delivery
|
29
|
+
deliveries = ::Textris::Delivery.get
|
30
|
+
deliveries.each do |delivery|
|
31
|
+
delivery.send_message_to_all(self)
|
32
|
+
end
|
31
33
|
|
32
34
|
self
|
33
35
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: textris
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Karol Słuszniak
|
@@ -51,6 +51,7 @@ files:
|
|
51
51
|
- lib/textris.rb
|
52
52
|
- lib/textris/base.rb
|
53
53
|
- lib/textris/delivery.rb
|
54
|
+
- lib/textris/delivery/base.rb
|
54
55
|
- lib/textris/delivery/mail.rb
|
55
56
|
- lib/textris/delivery/test.rb
|
56
57
|
- lib/textris/message.rb
|