textris 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2fd3e9eb6888e74d470bde1afe44a785ecbec855
4
- data.tar.gz: b32e98392db542754742c288849125af7f7b28fd
3
+ metadata.gz: dec9ef9f16f25185395f550204e2e5afbd067d8b
4
+ data.tar.gz: b5f8bf92434c2e95ff71a0bf6d9ee2a4692b0091
5
5
  SHA512:
6
- metadata.gz: 352443cb1e2001e43ae58a11d863f80ec5b11b84956aae8f5cd654f75a0da56f313d79b683b27194a5768d8485733a5f11a016366cbf404450bb1ba6bf5b620b
7
- data.tar.gz: 4b90542d764e5b78a0cd682065f5df71050cb05f0a971d21979a43627c2e631b8f55b7420efa97fe2ef07fe2416799e4112688a337755355bdfc0e7533322a4a
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
- gem 'textris'
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
- class UserTexter < Textris::Base
28
- default :from => "Our Team <+48 666-777-888>"
32
+ ```ruby
33
+ class UserTexter < Textris::Base
34
+ default :from => "Our Team <+48 666-777-888>"
29
35
 
30
- def welcome(user)
31
- @user = user
36
+ def welcome(user)
37
+ @user = user
32
38
 
33
- text :to => @user.phone
34
- end
35
- end
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.*`, (e.g. `app/views/user_texter/welcome.text.erb`):
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
- Welcome to our system, <%= @user.name %>!
46
+ ```erb
47
+ Welcome to our system, <%= @user.name %>!
48
+ ```
40
49
 
41
50
  Invoke them from application logic:
42
51
 
43
- class User < ActiveRecord::Base
44
- after_create do
45
- UserTexter.welcome(self).deliver
46
- end
47
- end
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
- Textris::Base.deliveries
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
- before(:each) do
58
- Textris::Base.deliveries.clear
59
- end
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
- text :to => ['48111222333', '48222333444']
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
- # Don't send anything, access your messages via Textris::Base.deliveries
74
- config.textris_delivery_method = :test
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
- # Send e-mails instead of SMSes in order to inspect their content
77
- config.textris_delivery_method = :mail
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
- # E-mail sender, here: "our-team-48666777888@test.app-name.com"
82
- config.textris_mail_from_template = '%{from_name:d}-%{from_phone}@%{env:d}.%{app:d}.com'
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
- # E-mail target, here: "app-name-test-48111222333-texts@mailinator.com"
85
- config.textris_mail_to_template = '%{app:d}-%{env:d}-%{to_phone}-texts@mailinator.com'
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
- # E-mail subject, here: "User texter: Welcome"
88
- config.textris_mail_subject_template = '%{texter:dh} texter: %{action:h}'
142
+ # E-mail subject, here: "User texter: Welcome"
143
+ config.textris_mail_subject_template = '%{texter:dh} texter: %{action:h}'
89
144
 
90
- # E-mail body, here: "Welcome to our system, Mr Jones!"
91
- config.textris_mail_body_template = '%{content}'
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
 
@@ -4,5 +4,6 @@ require 'action_mailer'
4
4
  require 'textris/base'
5
5
  require 'textris/message'
6
6
  require 'textris/delivery'
7
+ require 'textris/delivery/base'
7
8
  require 'textris/delivery/test'
8
9
  require 'textris/delivery/mail'
@@ -3,14 +3,16 @@ module Textris
3
3
  module_function
4
4
 
5
5
  def get
6
- case Rails.application.config.try(:textris_delivery_method).to_s
7
- when 'mail'
8
- ::Textris::Delivery::Mail
9
- when 'test'
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
@@ -0,0 +1,13 @@
1
+ module Textris
2
+ module Delivery
3
+ class Base
4
+ class << self
5
+ def send_message_to_all(message)
6
+ message.to.each do |to|
7
+ send_message(to, message)
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
13
+ 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)
@@ -1,13 +1,7 @@
1
1
  module Textris
2
2
  module Delivery
3
- class Test
3
+ class Test < Textris::Delivery::Base
4
4
  class << self
5
- def send_message_to_all(message)
6
- message.to.each do |to|
7
- send_message(to, message)
8
- end
9
- end
10
-
11
5
  def messages
12
6
  @messages ||= []
13
7
  end
@@ -26,8 +26,10 @@ module Textris
26
26
  end
27
27
 
28
28
  def deliver
29
- delivery = ::Textris::Delivery.get
30
- delivery.send_message_to_all(self)
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.0
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