smser 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: fa4a51837ff0ef6c0b71514210978aa258a2fcdf
4
+ data.tar.gz: 63ed011eabd9eaf6fbd8b6bc2b70c050b942063b
5
+ SHA512:
6
+ metadata.gz: b091f138ef7ed03d02c42c490944543bca26e546da786432132a837d5ce62bebe5bd116e69695d3afbf26b09bf877f488cb002735a9fffa4db24643aeb7e29a9
7
+ data.tar.gz: 74a433e7c6cb6cfcf97c3d9737adf93aa459c01515512f5c3c7f4741cc7b3781abc6173f7f00bb06926cfd9008470623d0e32cb52661d212a3c6b1d8b780a926
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.13.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in smser.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Anton Sozontov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,41 @@
1
+ # Smser
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/smser`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'smser'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install smser
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/smser.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "smser"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,9 @@
1
+ require 'smser/version'
2
+
3
+ require 'smser/base'
4
+ require 'smser/sms_delivery'
5
+ require 'smser/message'
6
+
7
+ module Smser
8
+ mattr_accessor :deliver_method
9
+ end
@@ -0,0 +1,60 @@
1
+ require 'smser/rescuable'
2
+
3
+ module Smser
4
+ class Base < AbstractController::Base
5
+ include Rescuable
6
+
7
+ class_attribute :default_params
8
+ self.default_params = {}.freeze
9
+
10
+ cattr_accessor :deliver_later_queue_name
11
+ self.deliver_later_queue_name = :smsers
12
+
13
+ class << self
14
+ def default(value = nil)
15
+ self.default_params = default_params.merge(value).freeze if value
16
+ default_params
17
+ end
18
+ alias :default_options= :default
19
+
20
+ def sms_path
21
+ @sms_path ||= name.underscore
22
+ end
23
+
24
+ # Allows to set the name of current mailer.
25
+ attr_writer :smser_name
26
+ alias :controller_path :sms_path
27
+
28
+ def method_missing(method_name, *args) # :nodoc:
29
+ if action_methods.include?(method_name.to_s)
30
+ Smser::SmsDelivery.new(self, method_name, *args)
31
+ else
32
+ super
33
+ end
34
+ end
35
+ end
36
+
37
+ attr_internal :message, :action_name
38
+
39
+ def sms(to:, body: nil, from: nil, callback: nil)
40
+ @_message ||= Message.new(
41
+ to: to,
42
+ from: from || default_params[:from],
43
+ body: body || default_i18n_body,
44
+ status_callback: callback || default_params[:status_callback]
45
+ )
46
+ end
47
+
48
+ def process(method_name, *args) #:nodoc:
49
+ @_action_name = method_name.to_s
50
+ process_action(method_name, *args)
51
+ end
52
+
53
+ private
54
+
55
+ def default_i18n_body(interpolations = {})
56
+ mailer_scope = self.class.sms_path.tr('/', '.')
57
+ I18n.t!(action_name, interpolations.merge(scope: [mailer_scope]))
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,34 @@
1
+ require 'active_job'
2
+
3
+ module Smser
4
+ # The <tt>Smser::DeliveryJob</tt> class is used when you
5
+ # want to send emails outside of the request-response cycle.
6
+ #
7
+ # Exceptions are rescued and handled by the smser class.
8
+ class DeliveryJob < ActiveJob::Base # :nodoc:
9
+ queue_as { Smser::Base.deliver_later_queue_name }
10
+
11
+ rescue_from StandardError, with: :handle_exception_with_smser_class
12
+
13
+ def perform(smser, mail_method, delivery_method, *args) #:nodoc:
14
+ smser.constantize.public_send(mail_method, *args).send(delivery_method)
15
+ end
16
+
17
+ private
18
+ # "Deserialize" the smser class name by hand in case another argument
19
+ # (like a Global ID reference) raised DeserializationError.
20
+ def smser_class
21
+ if smser = Array(@serialized_arguments).first || Array(arguments).first
22
+ smser.constantize
23
+ end
24
+ end
25
+
26
+ def handle_exception_with_smser_class(exception)
27
+ if klass = smser_class
28
+ klass.handle_exception exception
29
+ else
30
+ raise exception
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,16 @@
1
+ module Smser
2
+ class Message
3
+ attr_reader :to, :from, :body, :options
4
+
5
+ def initialize(to:, from:, body:, **options)
6
+ @to = to
7
+ @from = from
8
+ @body = body
9
+ @options = options
10
+ end
11
+
12
+ def deliver
13
+ Smser.deliver_method.call(self)
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,27 @@
1
+ module Smser #:nodoc:
2
+ # Provides `rescue_from` for mailers. Wraps mailer action processing,
3
+ # mail job processing, and mail delivery.
4
+ module Rescuable
5
+ extend ActiveSupport::Concern
6
+ include ActiveSupport::Rescuable
7
+
8
+ class_methods do
9
+ def handle_exception(exception) #:nodoc:
10
+ rescue_with_handler(exception) || raise(exception)
11
+ end
12
+ end
13
+
14
+ def handle_exceptions #:nodoc:
15
+ yield
16
+ rescue => exception
17
+ rescue_with_handler(exception) || raise
18
+ end
19
+
20
+ private
21
+ def process(*)
22
+ handle_exceptions do
23
+ super
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,126 @@
1
+ require 'smser/delivery_job'
2
+ require 'delegate'
3
+
4
+ module Smser
5
+ # The <tt>ActionMailer::MessageDelivery</tt> class is used by
6
+ # <tt>ActionMailer::Base</tt> when creating a new mailer.
7
+ # <tt>MessageDelivery</tt> is a wrapper (+Delegator+ subclass) around a lazy
8
+ # created <tt>Mail::Message</tt>. You can get direct access to the
9
+ # <tt>Mail::Message</tt>, deliver the email or schedule the email to be sent
10
+ # through Active Job.
11
+ #
12
+ # Notifier.welcome(User.first) # an ActionMailer::MessageDelivery object
13
+ # Notifier.welcome(User.first).deliver_now # sends the email
14
+ # Notifier.welcome(User.first).deliver_later # enqueue email delivery as a job through Active Job
15
+ # Notifier.welcome(User.first).message # a Mail::Message object
16
+ class SmsDelivery < Delegator
17
+ def initialize(smser_class, action, *args) #:nodoc:
18
+ @smser_class, @action, @args = smser_class, action, args
19
+
20
+ # The sms is only processed if we try to call any methods on it.
21
+ # Typical usage will leave it unloaded and call deliver_later.
22
+ @processed_smser = nil
23
+ @sms_message = nil
24
+ end
25
+
26
+ # Method calls are delegated to the Mail::Message that's ready to deliver.
27
+ def __getobj__ #:nodoc:
28
+ @sms_message ||= processed_smser.message
29
+ end
30
+
31
+ # Unused except for delegator internals (dup, marshaling).
32
+ def __setobj__(sms_message) #:nodoc:
33
+ @sms_message = sms_message
34
+ end
35
+
36
+ # Returns the resulting Mail::Message
37
+ def message
38
+ __getobj__
39
+ end
40
+
41
+ # Was the delegate loaded, causing the smser action to be processed?
42
+ def processed?
43
+ @processed_smser || @sms_message
44
+ end
45
+
46
+ # Enqueues the esms to be delivered through Active Job. When the
47
+ # job runs it will send the esms using +deliver_now!+. That means
48
+ # that the message will be sent bypassing checking +perform_deliveries+
49
+ # and +raise_delivery_errors+, so use with caution.
50
+ #
51
+ # Notifier.welcome(User.first).deliver_later!
52
+ # Notifier.welcome(User.first).deliver_later!(wait: 1.hour)
53
+ # Notifier.welcome(User.first).deliver_later!(wait_until: 10.hours.from_now)
54
+ #
55
+ # Options:
56
+ #
57
+ # * <tt>:wait</tt> - Enqueue the esms to be delivered with a delay
58
+ # * <tt>:wait_until</tt> - Enqueue the esms to be delivered at (after) a specific date / time
59
+ # * <tt>:queue</tt> - Enqueue the esms on the specified queue
60
+ def deliver_later!(options={})
61
+ enqueue_delivery :deliver_now!, options
62
+ end
63
+
64
+ # Enqueues the esms to be delivered through Active Job. When the
65
+ # job runs it will send the esms using +deliver_now+.
66
+ #
67
+ # Notifier.welcome(User.first).deliver_later
68
+ # Notifier.welcome(User.first).deliver_later(wait: 1.hour)
69
+ # Notifier.welcome(User.first).deliver_later(wait_until: 10.hours.from_now)
70
+ #
71
+ # Options:
72
+ #
73
+ # * <tt>:wait</tt> - Enqueue the esms to be delivered with a delay.
74
+ # * <tt>:wait_until</tt> - Enqueue the esms to be delivered at (after) a specific date / time.
75
+ # * <tt>:queue</tt> - Enqueue the esms on the specified queue.
76
+ def deliver_later(options={})
77
+ enqueue_delivery :deliver_now, options
78
+ end
79
+
80
+ # Delivers an esms without checking +perform_deliveries+ and +raise_delivery_errors+,
81
+ # so use with caution.
82
+ #
83
+ # Notifier.welcome(User.first).deliver_now!
84
+ #
85
+ def deliver_now!
86
+ processed_smser.handle_exceptions do
87
+ message.deliver!
88
+ end
89
+ end
90
+
91
+ # Delivers an esms:
92
+ #
93
+ # Notifier.welcome(User.first).deliver_now
94
+ #
95
+ def deliver_now
96
+ processed_smser.handle_exceptions do
97
+ message.deliver
98
+ end
99
+ end
100
+
101
+ private
102
+ # Returns the processed Mailer instance. We keep this instance
103
+ # on hand so we can delegate exception handling to it.
104
+ def processed_smser
105
+ @processed_smser ||= @smser_class.new.tap do |smser|
106
+ smser.process @action, *@args
107
+ end
108
+ end
109
+
110
+ def enqueue_delivery(delivery_method, options={})
111
+ if processed?
112
+ ::Kernel.raise "You've accessed the message before asking to " \
113
+ "deliver it later, so you may have made local changes that would " \
114
+ "be silently lost if we enqueued a job to deliver it. Why? Only " \
115
+ "the smser method *arguments* are passed with the delivery job! " \
116
+ "Do not access the message in any way if you mean to deliver it " \
117
+ "later. Workarounds: 1. don't touch the message before calling " \
118
+ "#deliver_later, 2. only touch the message *within your smser " \
119
+ "method*, or 3. use a custom Active Job instead of #deliver_later."
120
+ else
121
+ args = @smser_class.name, @action.to_s, delivery_method.to_s, *@args
122
+ ::Smser::DeliveryJob.set(options).perform_later(*args)
123
+ end
124
+ end
125
+ end
126
+ end
@@ -0,0 +1,3 @@
1
+ module Smser
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,36 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'smser/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'smser'
8
+ spec.version = Smser::VERSION
9
+ spec.authors = ['Anton Sozontov']
10
+ spec.email = ['a.sozontov@gmail.com']
11
+
12
+ spec.summary = %q{ActionMailer for SMS}
13
+ spec.description = %q{Smser provides simple way to manage your sms messages}
14
+ spec.homepage = 'https://github.com/qwiqer/smser'
15
+ spec.license = 'MIT'
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
21
+ else
22
+ raise 'RubyGems 2.0 or newer is required to protect against public gem pushes.'
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
26
+ f.match(%r{^(test|spec|features)/})
27
+ end
28
+ spec.bindir = 'exe'
29
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ['lib']
31
+
32
+ spec.add_development_dependency 'bundler', '~> 1.13'
33
+ spec.add_development_dependency 'rake', '~> 10.0'
34
+ spec.add_development_dependency 'rspec', '~> 3.0'
35
+ spec.add_dependency 'activesupport', '>=3.2', '<5.1'
36
+ end
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: smser
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Anton Sozontov
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-11-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activesupport
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '3.2'
62
+ - - "<"
63
+ - !ruby/object:Gem::Version
64
+ version: '5.1'
65
+ type: :runtime
66
+ prerelease: false
67
+ version_requirements: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '3.2'
72
+ - - "<"
73
+ - !ruby/object:Gem::Version
74
+ version: '5.1'
75
+ description: Smser provides simple way to manage your sms messages
76
+ email:
77
+ - a.sozontov@gmail.com
78
+ executables: []
79
+ extensions: []
80
+ extra_rdoc_files: []
81
+ files:
82
+ - ".gitignore"
83
+ - ".rspec"
84
+ - ".travis.yml"
85
+ - Gemfile
86
+ - LICENSE.txt
87
+ - README.md
88
+ - Rakefile
89
+ - bin/console
90
+ - bin/setup
91
+ - lib/smser.rb
92
+ - lib/smser/base.rb
93
+ - lib/smser/delivery_job.rb
94
+ - lib/smser/message.rb
95
+ - lib/smser/rescuable.rb
96
+ - lib/smser/sms_delivery.rb
97
+ - lib/smser/version.rb
98
+ - smser.gemspec
99
+ homepage: https://github.com/qwiqer/smser
100
+ licenses:
101
+ - MIT
102
+ metadata:
103
+ allowed_push_host: https://rubygems.org
104
+ post_install_message:
105
+ rdoc_options: []
106
+ require_paths:
107
+ - lib
108
+ required_ruby_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ requirements: []
119
+ rubyforge_project:
120
+ rubygems_version: 2.5.1
121
+ signing_key:
122
+ specification_version: 4
123
+ summary: ActionMailer for SMS
124
+ test_files: []