textris 0.3.0 → 0.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 41a7e696b4cae68e7793538ad07e490c415f048c
4
- data.tar.gz: 1cd679882d001d36860d0e9e0129dd096d036422
3
+ metadata.gz: 22e7162b57526b114af677ddabf1d417f97c31b7
4
+ data.tar.gz: 15f9e84235920d7413b252ed751e80023a5b15b7
5
5
  SHA512:
6
- metadata.gz: 67b9819759fb074b871ce4ef1f4aef2c6baeddd9472e111b75beb1ac8ce33530126481d04aac3f47ac3274229bf910341d8a7f9ba926e79b813e36f114a181ac
7
- data.tar.gz: 08cffc6511507e0c2898d0f0ea07ea9667e9947054db10fc89031ce0b4d977f1ea1480a3ab9f4643856e867c3a9a31e29495a6b57ce51011f59173fb03eff165
6
+ metadata.gz: 93312a94c10bab68d1faff3fc82d8fb882a5ffc0bd462e93e075cf28e2087176e10c48f2ee6dabe27cd6bd6b78ec89505037fe2aa103597052543782ce0cfc16
7
+ data.tar.gz: 1ef7417175290cbec6812c6c9c718d15f88c0fd98c1bafe25d7f0607a591d582f5c5403539bcb8f4058f443326a533ae002b9b66f9c8a1ad5f9bd915aafd15e6
data/README.md CHANGED
@@ -1,5 +1,6 @@
1
1
  # textris
2
2
 
3
+ [![GitHub version](https://badge.fury.io/gh/visualitypl%2Ftextris.svg)](http://badge.fury.io/gh/visualitypl%2Ftextris)
3
4
  [![Build Status](https://scrutinizer-ci.com/g/visualitypl/textris/badges/build.png?b=master)](https://scrutinizer-ci.com/g/visualitypl/textris/build-status/master)
4
5
  [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/visualitypl/textris/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/visualitypl/textris/?branch=master)
5
6
  [![Code Climate](https://codeclimate.com/github/visualitypl/textris/badges/gpa.svg)](https://codeclimate.com/github/visualitypl/textris)
@@ -11,7 +12,7 @@ Unlike similar gems, **textris** has some unique features:
11
12
 
12
13
  - e-mail proxy allowing to inspect messages using [Mailinator](https://mailinator.com/) or similar service
13
14
  - phone number E164 validation and normalization with the [phony](https://github.com/floere/phony) gem
14
- - built-in support for the Twilio API thanks to the [twilio-ruby](https://github.com/twilio/twilio-ruby) gem
15
+ - built-in support for the Twilio API thanks to integration with the [twilio-ruby](https://github.com/twilio/twilio-ruby) gem
15
16
  - multiple, per-environment configurable and chainable delivery methods
16
17
  - extensible with any number of custom delivery methods (also chainable)
17
18
  - background and scheduled texting thanks to integration with the [sidekiq](https://github.com/mperham/sidekiq) gem
@@ -74,7 +75,7 @@ UserTexter.delay_until(1.day.from_now).welcome(user)
74
75
 
75
76
  > You should not call `deliver` after the action invocation when using delay. It will be called by the *Textris::Delay::Sidekiq::Worker* worker.
76
77
 
77
- > You can safely pass ActiveRecord records and arrays as delayed action arguments. **textris** will store their `id`'s and find them upon scheduled delivery.
78
+ > You can safely pass ActiveRecord records and arrays as delayed action arguments. **textris** will store their `id`s and find them upon scheduled delivery.
78
79
 
79
80
  Keep in mind that **textris** does not install *sidekiq* for you. If you don't have it yet, [install Redis](http://redis.io/topics/quickstart) on your machine and add the *sidekiq* gem to `Gemfile`:
80
81
 
@@ -123,6 +124,9 @@ Below you'll find sample settings for any of supported delivery methods along wi
123
124
  # Send messages via the Twilio REST API
124
125
  config.textris_delivery_method = :twilio
125
126
 
127
+ # Don't send anything, log messages into Rails logger
128
+ config.textris_delivery_method = :log
129
+
126
130
  # Don't send anything, access your messages via Textris::Base.deliveries
127
131
  config.textris_delivery_method = :test
128
132
 
@@ -133,11 +137,17 @@ config.textris_delivery_method = :mail
133
137
  config.textris_delivery_method = [:mail, :test]
134
138
  ```
135
139
 
136
- > Unless otherwise configured, *Twilio* will be the default delivery method in `development` and `production` environments, while the *test* method will be used in `test` environment by default.
140
+ > Unless otherwise configured, default delivery methods will be: *log* in `development` environment, *test* in `test` environment and *mail* in `production` environment. All these come with reasonable defaults and will work with no further configuration.
137
141
 
138
142
  #### Twilio
139
143
 
140
- In order to use Twilio with **textris**, you must pre-configure the *twilio-ruby* settings. Create the `config/initializers/twilio.rb`:
144
+ **textris** connects with the Twilio API using *twilio-ruby* gem. It does not, however, install the gem for you. If you don't have it yet, add the *twilio-ruby* gem to `Gemfile`:
145
+
146
+ ```ruby
147
+ gem 'twilio-ruby'
148
+ ```
149
+
150
+ Then, pre-configure the *twilio-ruby* settings by creating the `config/initializers/twilio.rb` file:
141
151
 
142
152
  ```ruby
143
153
  Twilio.configure do |config|
@@ -146,6 +156,14 @@ Twilio.configure do |config|
146
156
  end
147
157
  ```
148
158
 
159
+ #### Log
160
+
161
+ **textris** logger has similar logging behavior to ActionMailer. It will log single line to *info* log with production in mind and then a couple details to *debug* log. You can change the log level for the whole output:
162
+
163
+ ```ruby
164
+ config.textris_log_level = :info
165
+ ```
166
+
149
167
  #### Custom delivery methods
150
168
 
151
169
  Currently, **textris** comes with `twilio`, `mail` and `test` delivery methods built-in, but you can easily implement your own. Place desired delivery class in `app/deliveries/<name>_delivery.rb` (e.g. `app/deliveries/my_provider_delivery.rb`):
@@ -224,3 +242,14 @@ You can add optional interpolation modifiers using the `%{variable:modifiers}` s
224
242
  3. Commit your changes (`git commit -am 'Add some feature'`)
225
243
  4. Push to the branch (`git push origin my-new-feature`)
226
244
  5. Create a new Pull Request
245
+
246
+ ### Adding delivery methods
247
+
248
+ Implementing new delivery methods in Pull Requests is strongly encouraged. Start by [implementing custom delivery method](#custom-delivery-methods). Then, you can prepare it for a Pull Request by adhering to following guidelines:
249
+
250
+ 1. Delivery class should be placed in `lib/textris/delivery/service_name.rb` and named in a way that will best indicate the service with which it's supposed to work with.
251
+ 5. Add your method to code example in [Choosing and chaining delivery methods](#choosing-and-chaining-delivery-methods) in README. Also, add sub-chapter for it if it depends on other gems or requires explanation.
252
+ 6. Your delivery code is expected to throw exceptions with self-explanatory messages upon failure. Include specs that test this. Mock external API requests with [webmock](https://github.com/bblimke/webmock).
253
+ 2. If delivery depends on any gems, don't add them as runtime dependencies. You can (and should in order to write complete specs) add them as development dependencies.
254
+ 3. Delivery code must load without exceptions even when dependent libraries are missing. Specs should test such case (you can use `remove_const` to undefine loaded consts).
255
+ 4. New deliveries are expected to have 100% test coverage. Run `COVERAGE=1 bundle exec rake spec` to generate *simplecov* coverage into the **coverage/index.html** file.
@@ -26,4 +26,5 @@ require 'textris/delivery'
26
26
  require 'textris/delivery/base'
27
27
  require 'textris/delivery/test'
28
28
  require 'textris/delivery/mail'
29
+ require 'textris/delivery/log'
29
30
  require 'textris/delivery/twilio'
@@ -10,6 +10,7 @@ module Textris
10
10
  objects.collect do |object|
11
11
  serialize_active_record_object(object) ||
12
12
  serialize_active_record_array(object) ||
13
+ serialize_active_record_relation(object) ||
13
14
  object
14
15
  end
15
16
  rescue NameError
@@ -19,7 +20,6 @@ module Textris
19
20
  def deserialize(objects)
20
21
  objects.collect do |object|
21
22
  deserialize_active_record_object(object) ||
22
- deserialize_active_record_array(object) ||
23
23
  object
24
24
  end
25
25
  end
@@ -32,27 +32,24 @@ module Textris
32
32
  end
33
33
  end
34
34
 
35
- def deserialize_active_record_object(object)
36
- if object.is_a?(Array) &&
37
- object.try(:length) == 3 &&
38
- object[0] == ACTIVERECORD_POINTER
39
- object[1].constantize.find(object[2])
35
+ def serialize_active_record_relation(array)
36
+ if array.class < ActiveRecord::Relation
37
+ [ACTIVERECORD_ARRAY_POINTER, array.model.to_s, array.map(&:id)]
40
38
  end
41
39
  end
42
40
 
43
41
  def serialize_active_record_array(array)
44
- if array.class < ActiveRecord::Relation
45
- [ACTIVERECORD_ARRAY_POINTER, array.model.to_s, array.map(&:id)]
46
- elsif array.is_a?(Array) &&
42
+ if array.is_a?(Array) &&
47
43
  (model = get_active_record_common_model(array))
48
44
  [ACTIVERECORD_ARRAY_POINTER, model, array.map(&:id)]
49
45
  end
50
46
  end
51
47
 
52
- def deserialize_active_record_array(object)
48
+ def deserialize_active_record_object(object)
53
49
  if object.is_a?(Array) &&
54
50
  object.try(:length) == 3 &&
55
- object[0] == ACTIVERECORD_ARRAY_POINTER
51
+ [ACTIVERECORD_POINTER,
52
+ ACTIVERECORD_ARRAY_POINTER].include?(object[0])
56
53
  object[1].constantize.find(object[2])
57
54
  end
58
55
  end
@@ -6,10 +6,12 @@ module Textris
6
6
  methods = Rails.application.config.try(:textris_delivery_method)
7
7
  methods = [*methods].compact
8
8
  if methods.blank?
9
- if Rails.env.test?
9
+ if Rails.env.development?
10
+ methods = [:log]
11
+ elsif Rails.env.test?
10
12
  methods = [:test]
11
13
  else
12
- methods = [:twilio]
14
+ methods = [:mail]
13
15
  end
14
16
  end
15
17
 
@@ -0,0 +1,29 @@
1
+ module Textris
2
+ module Delivery
3
+ class Log < Textris::Delivery::Base
4
+ AVAILABLE_LOG_LEVELS = %w{debug info warn error fatal unknown}
5
+
6
+ def deliver(to)
7
+ log :info, "Sent text to #{Phony.format(to)}"
8
+ log :debug, "Texter: #{message.texter || 'UnknownTexter'}" + "#" +
9
+ "#{message.action || 'unknown_action'}"
10
+ log :debug, "Date: #{Time.now}"
11
+ log :debug, "From: #{message.from || 'unknown'}"
12
+ log :debug, "To: #{message.to.map { |i| Phony.format(to) }.join(', ')}"
13
+ log :debug, "Content: #{message.content}"
14
+ end
15
+
16
+ private
17
+
18
+ def log(level, message)
19
+ level = Rails.application.config.try(:textris_log_level) || level
20
+
21
+ unless AVAILABLE_LOG_LEVELS.include?(level.to_s)
22
+ raise(ArgumentError, "Wrong log level: #{level}")
23
+ end
24
+
25
+ Rails.logger.send(level, message)
26
+ end
27
+ end
28
+ end
29
+ end
@@ -56,7 +56,7 @@ module Textris
56
56
  end
57
57
 
58
58
  def get_template_interpolation(key, variables)
59
- content = case key
59
+ case key
60
60
  when 'app', 'env'
61
61
  get_rails_variable(key)
62
62
  when 'texter', 'action', 'from_name', 'from_phone', 'content'
@@ -40,6 +40,16 @@ module Textris
40
40
  end
41
41
  end
42
42
 
43
+ def from
44
+ if @from_name.present? && @from_phone.present?
45
+ "#{@from_name} <#{Phony.format(@from_phone)}>"
46
+ elsif @from_name.present?
47
+ @from_name
48
+ elsif @from_phone.present?
49
+ Phony.format(@from_phone)
50
+ end
51
+ end
52
+
43
53
  private
44
54
 
45
55
  def parse_from(from)
@@ -1,3 +1,3 @@
1
1
  module Textris
2
- VERSION = "0.3.0"
2
+ VERSION = '0.3.1'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: textris
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Karol Słuszniak
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-16 00:00:00.000000000 Z
11
+ date: 2014-12-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -183,6 +183,7 @@ files:
183
183
  - lib/textris/delay/sidekiq/worker.rb
184
184
  - lib/textris/delivery.rb
185
185
  - lib/textris/delivery/base.rb
186
+ - lib/textris/delivery/log.rb
186
187
  - lib/textris/delivery/mail.rb
187
188
  - lib/textris/delivery/test.rb
188
189
  - lib/textris/delivery/twilio.rb