textris 0.4.0 → 0.4.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: 5932f935ac05fd8ca113cb3e218b171153b106c6
4
- data.tar.gz: 10c63364aeef04d95d9b6382c9949d1ff867bb47
3
+ metadata.gz: a8e727a1d0513890fb986e6ee295e831273c50e6
4
+ data.tar.gz: 99747c26382ad719e0741449f8afed9bb23048d0
5
5
  SHA512:
6
- metadata.gz: 8f371b94fd9a376866847ef1b0faef2896431fa90e526327a66081600b9988f4446b6a3466390512f971ddeeb6fb7d548b545315d7e9ba2393efe2c0fe4a0fcd
7
- data.tar.gz: d46bb126af552ef5008fd1aa6a5d3b2e68d16a5f22cd0952b1fb6f3255f7eb55bb571957d7b2c0fe1d68e0e0761cba6de95ad34495af9095f9cc6dff7a892896
6
+ metadata.gz: 82a8e78f5edb3dd26940dd23bed9c8d22b4006cc7e2692d4baf52a055a54632b99b29ca7828284dba2480260ef164f4a99ff3b340b73c044e4410f15d00063af
7
+ data.tar.gz: 694ba014f20b5e6a1d9476da92665741aecfe79ec2f67b5a533aa3fbe98bfd6cf05c8a88d513e87ba8d40e93edc11590f0ed6c309f9f55eb9be9aed630e5780b
data/README.md CHANGED
@@ -16,7 +16,8 @@ Unlike similar gems, **textris** has some unique features:
16
16
  - built-in support for the Twilio and Nexmo APIs with [twilio-ruby](https://github.com/twilio/twilio-ruby) and [nexmo](https://github.com/timcraft/nexmo) gems
17
17
  - multiple, per-environment configurable and chainable delivery methods
18
18
  - extensible with any number of custom delivery methods (also chainable)
19
- - background and scheduled texting thanks to integration with the [sidekiq](https://github.com/mperham/sidekiq) gem
19
+ - background and scheduled texting for Rails 4.2+ thanks to integration with [ActiveJob](http://edgeguides.rubyonrails.org/active_job_basics.html)
20
+ - scheduled texting for Rails 4.1 and older thanks to integration with the [sidekiq](https://github.com/mperham/sidekiq) gem
20
21
  - support for testing using self-explanatory `Textris::Base.deliveries`
21
22
  - simple, extensible, fully tested code written from the ground up instead of copying *ActionMailer*
22
23
 
@@ -68,6 +69,26 @@ end
68
69
 
69
70
  ### Background and scheduled
70
71
 
72
+ #### ActiveJob integration
73
+
74
+ As of version 0.4, **textris** supports native Rails 4.2+ way of background job handling, the [ActiveJob](http://edgeguides.rubyonrails.org/active_job_basics.html). You can delay delivery of your texters the same way as with ActionMailer mailers, like:
75
+
76
+ ```ruby
77
+ UserTexter.welcome(user).deliver_later
78
+ UserTexter.welcome(user).deliver_later(:wait => 1.hour)
79
+ UserTexter.welcome(user).deliver_later(:wait_until => 1.day.from_now)
80
+ UserTexter.welcome(user).deliver_later(:queue => :custom_queue)
81
+ UserTexter.welcome(user).deliver_now
82
+ ```
83
+
84
+ > You can safely pass ActiveRecord records as delayed action arguments. ActiveJob uses [GlobalID](https://github.com/rails/activemodel-globalid/) to serialize them for scheduled delivery.
85
+
86
+ By default, `textris` queue will be used by the *Textris::Delay::ActiveJob::Job* job.
87
+
88
+ #### Direct Sidekiq integration
89
+
90
+ > As of Rails 4.2, ActiveJob is the recommended way for background job handling and it does support Sidekiq as its backend, so please see [chapter above](#activejob-integration) if you're using Rails 4.2 or above. Otherwise, keep on reading to use textris with Sidekiq regardless of your Rails version.
91
+
71
92
  Thanks to Sidekiq integration, you can send text messages in the background to speed things up, retry in case of failures or just to do it at specific time. To do so, use one of three delay methods:
72
93
 
73
94
  ```ruby
@@ -76,7 +97,7 @@ UserTexter.delay_for(1.hour).welcome(user)
76
97
  UserTexter.delay_until(1.day.from_now).welcome(user)
77
98
  ```
78
99
 
79
- > You should not call `deliver` after the action invocation when using delay. It will be called by the *Textris::Delay::Sidekiq::Worker* worker.
100
+ Remember not to call `deliver` after the action invocation when using delay. It will be called by the *Textris::Delay::Sidekiq::Worker* worker.
80
101
 
81
102
  > You can safely pass ActiveRecord records and arrays as delayed action arguments. **textris** will store their `id`s and find them upon scheduled delivery.
82
103
 
@@ -252,6 +273,34 @@ You can add optional interpolation modifiers using the `%{variable:modifiers}` s
252
273
  - `h`: humanize (for instance, `user_name` becomes `User name`)
253
274
  - `p`: format phone (for instance, `48111222333` becomes `+48 111 222 333`)
254
275
 
276
+ ## Example project
277
+
278
+ [Here](https://github.com/visualitypl/textris/tree/master/example/rails-4.2) you can find a simple example project that demonstrates **textris** usage with Rails 4.2. In order to see how it works or experiment with it, just go to project's directory and invoke:
279
+
280
+ ```
281
+ bundle install
282
+ rake db:migrate
283
+ rails server
284
+ ```
285
+
286
+ Open [application page](http://localhost:3000/) and fill in some user information. Sample texter will be invoked and you'll see an output similar to following in your server log:
287
+
288
+ ```
289
+ [ActiveJob] Enqueued Textris::Delay::ActiveJob::Job (Job ID: 71ed54f7-02e8-4205-9093-6f2a0ff7f483) to Inline(textris) with arguments: "UserTexter", "welcome", [#<User id: 1, name: "Mr Jones", phone: "48666777888", created_at: "2015-02-20 17:17:16", updated_at: "2015-02-20 17:17:16">]
290
+ [ActiveJob] User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]]
291
+ [ActiveJob] [Textris::Delay::ActiveJob::Job] [71ed54f7-02e8-4205-9093-6f2a0ff7f483] Performing Textris::Delay::ActiveJob::Job from Inline(textris) with arguments: "UserTexter", "welcome", [#<User id: 1, name: "Mr Jones", phone: "48666777888", created_at: "2015-02-20 17:17:16", updated_at: "2015-02-20 17:17:16">]
292
+ [ActiveJob] [Textris::Delay::ActiveJob::Job] [71ed54f7-02e8-4205-9093-6f2a0ff7f483] Sent text to +48 666 777 888
293
+ [ActiveJob] [Textris::Delay::ActiveJob::Job] [71ed54f7-02e8-4205-9093-6f2a0ff7f483] Texter: User#welcome
294
+ [ActiveJob] [Textris::Delay::ActiveJob::Job] [71ed54f7-02e8-4205-9093-6f2a0ff7f483] Date: 2015-02-20 18:17:16 +0100
295
+ [ActiveJob] [Textris::Delay::ActiveJob::Job] [71ed54f7-02e8-4205-9093-6f2a0ff7f483] From: Our Team <+48 666 777 888>
296
+ [ActiveJob] [Textris::Delay::ActiveJob::Job] [71ed54f7-02e8-4205-9093-6f2a0ff7f483] To: +48 666 777 888
297
+ [ActiveJob] [Textris::Delay::ActiveJob::Job] [71ed54f7-02e8-4205-9093-6f2a0ff7f483] Rendered user_texter/welcome.text.erb (0.4ms)
298
+ [ActiveJob] [Textris::Delay::ActiveJob::Job] [71ed54f7-02e8-4205-9093-6f2a0ff7f483] Content: Welcome to our system, Mr Jones!
299
+ [ActiveJob] [Textris::Delay::ActiveJob::Job] [71ed54f7-02e8-4205-9093-6f2a0ff7f483] Performed Textris::Delay::ActiveJob::Job from Inline(textris) in 9.98ms
300
+ ```
301
+
302
+ Example project may serve as a convenient sandbox for [developing custom delivery methods](#custom-delivery-methods).
303
+
255
304
  ## Contributing
256
305
 
257
306
  1. Fork it (https://github.com/visualitypl/textris/fork)
@@ -1,5 +1,6 @@
1
1
  require 'action_controller'
2
2
  require 'action_mailer'
3
+ require 'active_support'
3
4
  require 'phony'
4
5
 
5
6
  begin
@@ -46,12 +46,14 @@ module Textris
46
46
  def render_content
47
47
  set_instance_variables_for_rendering
48
48
 
49
- render(:template => template_name, :formats => ['text'])
49
+ render(:template => template_name, :formats => ['text'], :locale => @locale)
50
50
  end
51
51
 
52
52
  protected
53
53
 
54
54
  def text(options = {})
55
+ @locale = options[:locale] || I18n.locale
56
+
55
57
  options = self.class.with_defaults(options)
56
58
  options.merge!(
57
59
  :texter => self.class,
@@ -8,13 +8,7 @@ module Textris
8
8
  def deliver_later(options = {})
9
9
  job = Textris::Delay::ActiveJob::Job
10
10
 
11
- [:wait, :wait_until, :queue].each do |option|
12
- if options.has_key?(option)
13
- job.set(option => options[option])
14
- end
15
- end
16
-
17
- job.perform_later(texter(:raw => true).to_s, action.to_s, args)
11
+ job.new(texter(:raw => true).to_s, action.to_s, args).enqueue(options)
18
12
  end
19
13
  end
20
14
  end
@@ -3,13 +3,18 @@ module Textris
3
3
  class Twilio < Textris::Delivery::Base
4
4
  def deliver(to)
5
5
  client.messages.create(
6
- :from => message.from_phone,
7
- :to => to,
6
+ :from => phone_with_plus(message.from_phone),
7
+ :to => phone_with_plus(to),
8
8
  :body => message.content)
9
9
  end
10
10
 
11
11
  private
12
12
 
13
+ # Twillo requires phone numbers starting with a '+' sign
14
+ def phone_with_plus(phone)
15
+ phone.to_s.start_with?('+') ? phone : "+#{phone}"
16
+ end
17
+
13
18
  def client
14
19
  @client ||= ::Twilio::REST::Client.new
15
20
  end
@@ -3,24 +3,9 @@ module Textris
3
3
  attr_reader :content, :from_name, :from_phone, :to, :texter, :action, :args
4
4
 
5
5
  def initialize(options = {})
6
- @to = parse_to options[:to]
7
- @content = parse_content options[:content] if options[:content].present?
8
- @renderer = options[:renderer]
9
-
10
- if options.has_key?(:from)
11
- @from_name, @from_phone = parse_from options[:from]
12
- else
13
- @from_name = options[:from_name]
14
- @from_phone = options[:from_phone]
15
- end
16
-
17
- unless @content.present? || @renderer.respond_to?(:render)
18
- raise(ArgumentError, "Content must be provided")
19
- end
20
-
21
- unless @to.present?
22
- raise(ArgumentError, "Recipients must be provided and E.164 compilant")
23
- end
6
+ initialize_content(options)
7
+ initialize_author(options)
8
+ initialize_recipients(options)
24
9
 
25
10
  @texter = options[:texter]
26
11
  @action = options[:action]
@@ -57,11 +42,38 @@ module Textris
57
42
  end
58
43
 
59
44
  def content
60
- @content ||= @renderer.render_content
45
+ @content ||= parse_content(@renderer.render_content)
61
46
  end
62
47
 
63
48
  private
64
49
 
50
+ def initialize_content(options)
51
+ if options[:content].present?
52
+ @content = parse_content options[:content]
53
+ elsif options[:renderer].present?
54
+ @renderer = options[:renderer]
55
+ else
56
+ raise(ArgumentError, "Content must be provided")
57
+ end
58
+ end
59
+
60
+ def initialize_author(options)
61
+ if options.has_key?(:from)
62
+ @from_name, @from_phone = parse_from options[:from]
63
+ else
64
+ @from_name = options[:from_name]
65
+ @from_phone = options[:from_phone]
66
+ end
67
+ end
68
+
69
+ def initialize_recipients(options)
70
+ @to = parse_to options[:to]
71
+
72
+ unless @to.present?
73
+ raise(ArgumentError, "Recipients must be provided and E.164 compilant")
74
+ end
75
+ end
76
+
65
77
  def parse_from(from)
66
78
  parse_from_dual(from) || parse_from_singular(from)
67
79
  end
@@ -1,3 +1,3 @@
1
1
  module Textris
2
- VERSION = '0.4.0'
2
+ VERSION = '0.4.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.4.0
4
+ version: 0.4.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: 2015-02-20 00:00:00.000000000 Z
11
+ date: 2016-02-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -164,6 +164,20 @@ dependencies:
164
164
  - - "~>"
165
165
  - !ruby/object:Gem::Version
166
166
  version: '4.2'
167
+ - !ruby/object:Gem::Dependency
168
+ name: activesupport
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - "~>"
172
+ - !ruby/object:Gem::Version
173
+ version: '4.2'
174
+ type: :runtime
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - "~>"
179
+ - !ruby/object:Gem::Version
180
+ version: '4.2'
167
181
  - !ruby/object:Gem::Dependency
168
182
  name: phony
169
183
  requirement: !ruby/object:Gem::Requirement
@@ -241,7 +255,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
241
255
  version: '0'
242
256
  requirements: []
243
257
  rubyforge_project:
244
- rubygems_version: 2.2.2
258
+ rubygems_version: 2.4.6
245
259
  signing_key:
246
260
  specification_version: 4
247
261
  summary: Simple SMS messaging gem for Rails based on concepts and conventions similar