action_short_message 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.rspec +2 -0
- data/.travis.yml +7 -0
- data/Gemfile +8 -0
- data/LICENSE.txt +21 -0
- data/README.md +101 -0
- data/Rakefile +6 -0
- data/action_short_message.gemspec +30 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/action_short_message/base.rb +145 -0
- data/lib/action_short_message/delivery_job.rb +29 -0
- data/lib/action_short_message/interceptor.rb +35 -0
- data/lib/action_short_message/log_subscriber.rb +25 -0
- data/lib/action_short_message/message_delivery.rb +37 -0
- data/lib/action_short_message/railtie.rb +21 -0
- data/lib/action_short_message/short_message.rb +49 -0
- data/lib/action_short_message/sms_providers/base.rb +25 -0
- data/lib/action_short_message/sms_providers/test.rb +18 -0
- data/lib/action_short_message/sms_providers.rb +52 -0
- data/lib/action_short_message/version.rb +3 -0
- data/lib/action_short_message.rb +40 -0
- data/lib/rails/generators/message/USAGE +12 -0
- data/lib/rails/generators/message/message_generator.rb +20 -0
- data/lib/rails/generators/message/templates/messageer.rb +9 -0
- metadata +182 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: d1e42103d5c90cbd96e69d117d2d7599afcc2496db9a3bd7dbc4349a0857ec9a
|
4
|
+
data.tar.gz: '08ffe2747d100918ddb76ebc19930693fdc95529c469cb8812463c911bbb9742'
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9b477d57ea7778eb8caed59d5205d21006e06110e37f649c9b09ad01cdefe1e35f2e63da9e44f690b15c0a20d68c7775391914188a23b7e876cd9e4b79f16d83
|
7
|
+
data.tar.gz: 8cafc62b55e5d476a2559c9d63cc854fefe7cf91b5bc97b80708303ca3601b17ef1f16a4ba666dec195511ba9a0feff491293c7241a6b624b118e81cb2f446db
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Diego Ballona
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
# ActionShortMessage
|
2
|
+
|
3
|
+
ActionShortMessage reference to [dballona/actionmessage](https://github.com/dballona/actionmessage), is heavily-inspired on ActionMailer.
|
4
|
+
It's a gem for sending SMS/Text messages like we do for sending e-mails on ActionMailer.
|
5
|
+
|
6
|
+
## Setup
|
7
|
+
|
8
|
+
Install it using bundler:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
# Gemfile
|
12
|
+
gem 'action_short_message'
|
13
|
+
```
|
14
|
+
|
15
|
+
## Configuration
|
16
|
+
Place this on your environment file or application.rb
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
|
20
|
+
# Setting logger, default is Rails.logger
|
21
|
+
config.action_short_message.logger = Rails.logger
|
22
|
+
|
23
|
+
# Setting short message service provider, default is setting.
|
24
|
+
#
|
25
|
+
# Highly recommended place this on the environment files, and set
|
26
|
+
# development sms_provider to :test it can save your money
|
27
|
+
config.action_short_message.sms_provider = :test
|
28
|
+
|
29
|
+
|
30
|
+
```
|
31
|
+
|
32
|
+
### SMS Provider
|
33
|
+
Default provider is `test`, I also made a provider, use Mitake(山竹簡訊) api.
|
34
|
+
You can add [mitake_sms](http://) in Taiwan 🇹🇼
|
35
|
+
|
36
|
+
you also can create your provider, by implement `ActionShortMessage::SMSProviders::Base` and add provider follow by `ActionShortMessage::Base.add_provider(symbol, YourProvider, provider_setting)`
|
37
|
+
|
38
|
+
## Usage
|
39
|
+
|
40
|
+
In order to generate your message class, you can either place this code
|
41
|
+
under app/messengers/welcome_message.rb or just use generators by running
|
42
|
+
the following command: `rails g messenger Welcome send_welcome_sms`
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
class WelcomeMessenger < ActionShortMessage::Base
|
46
|
+
def send_welcome_sms(name, phone_number_to_send_message)
|
47
|
+
@name = name
|
48
|
+
sms(to: phone_number_to_send_message)
|
49
|
+
end
|
50
|
+
|
51
|
+
# Inline message example, body parameter has preference compared
|
52
|
+
# to a text.erb template.
|
53
|
+
def welcome_with_inline_body(name, phone_number_to_send_message)
|
54
|
+
@name = name
|
55
|
+
sms(to: phone_number_to_send_message, message: 'Inline message')
|
56
|
+
end
|
57
|
+
end
|
58
|
+
```
|
59
|
+
|
60
|
+
### Views
|
61
|
+
|
62
|
+
If you want to use template, add this file to `app/view/messenger/{your_messenger_name}/{method_name}.erb`
|
63
|
+
|
64
|
+
```html
|
65
|
+
Welcome, <%= @name %>!
|
66
|
+
```
|
67
|
+
|
68
|
+
### Callbacks
|
69
|
+
You can specify callbacks using `before_action` and `after_action` for configuring your messages.
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
class WelcomeMessenger
|
73
|
+
before_action :set_default_message
|
74
|
+
after_action :log_message
|
75
|
+
|
76
|
+
private
|
77
|
+
|
78
|
+
def set_default_message
|
79
|
+
...
|
80
|
+
end
|
81
|
+
|
82
|
+
def log_message
|
83
|
+
...
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
```
|
88
|
+
|
89
|
+
And to send is really simple!
|
90
|
+
|
91
|
+
```ruby
|
92
|
+
name = 'John Doe'
|
93
|
+
phone = '0987654321'
|
94
|
+
|
95
|
+
# To send right away:
|
96
|
+
WelcomeMessage.send_welcome_sms(name, phone).deliver_now
|
97
|
+
|
98
|
+
# To send through a background job
|
99
|
+
WelcomeMessage.send_welcome_sms(name, phone).deliver_later
|
100
|
+
|
101
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
lib = File.expand_path('lib', __dir__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require 'action_short_message/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'action_short_message'
|
7
|
+
spec.version = ActionShortMessage::VERSION
|
8
|
+
spec.authors = ['David']
|
9
|
+
spec.email = ['z22919456@gmail.com']
|
10
|
+
|
11
|
+
spec.summary = 'Refenrence dballona/action_short_message. SMS/Text Messages gem, just like ActionShortMessage'
|
12
|
+
spec.description = 'Refenrence dballona/action_short_message. SMS/Text Messages gem, just like ActionShortMessage'
|
13
|
+
spec.homepage = 'http://github.com/z22919456/action_short_message'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = 'exe'
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
|
21
|
+
spec.add_dependency 'actionpack', '>= 5.0.0'
|
22
|
+
spec.add_dependency 'actionview', '>= 5.0.0'
|
23
|
+
spec.add_dependency 'activejob', '>= 5.0.0'
|
24
|
+
|
25
|
+
spec.add_development_dependency 'bundler', '~> 2.3'
|
26
|
+
spec.add_development_dependency 'codecov', '~> 0.4.3'
|
27
|
+
spec.add_development_dependency 'rake', '~> 11.0'
|
28
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
29
|
+
spec.add_development_dependency 'webmock', '~> 3.14'
|
30
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'action_short_message'
|
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
|
data/bin/setup
ADDED
@@ -0,0 +1,145 @@
|
|
1
|
+
require 'active_support/core_ext/string/inflections'
|
2
|
+
require 'active_support/core_ext/hash/except'
|
3
|
+
require 'active_support/core_ext/module/anonymous'
|
4
|
+
require 'action_short_message/log_subscriber'
|
5
|
+
|
6
|
+
module ActionShortMessage
|
7
|
+
class Base < AbstractController::Base
|
8
|
+
include SMSProviders
|
9
|
+
|
10
|
+
abstract!
|
11
|
+
|
12
|
+
include AbstractController::Rendering
|
13
|
+
include AbstractController::Logger
|
14
|
+
include AbstractController::Callbacks
|
15
|
+
|
16
|
+
include ActionView::Layouts
|
17
|
+
|
18
|
+
class << self
|
19
|
+
def register_observers(*observers)
|
20
|
+
observers.flaten.compact.each { |observer| register_observer(observer) }
|
21
|
+
end
|
22
|
+
|
23
|
+
def unrgister_observers(*observers)
|
24
|
+
observers.flatten.compact.each { |observer| unregister_observer(observer) }
|
25
|
+
end
|
26
|
+
|
27
|
+
def register_observer(observer)
|
28
|
+
Message.register_observer(observer_class_for(observer))
|
29
|
+
end
|
30
|
+
|
31
|
+
class_attribute :default_params
|
32
|
+
self.default_params = {
|
33
|
+
adapter: {
|
34
|
+
name: :test,
|
35
|
+
credentials: {}
|
36
|
+
}
|
37
|
+
}
|
38
|
+
|
39
|
+
# Sets the defaults through app configuration:
|
40
|
+
# config.action_short_message.default()
|
41
|
+
#
|
42
|
+
# Aliased by ::default_options=
|
43
|
+
#
|
44
|
+
def default(value = nil)
|
45
|
+
self.default_params = default_params.merge(value).freeze if value
|
46
|
+
default_params
|
47
|
+
end
|
48
|
+
# Allows to set defaults through app configuration:
|
49
|
+
# config.action_short_message = { charset: 'ISO-8859-1' }
|
50
|
+
#
|
51
|
+
alias default_options= default
|
52
|
+
|
53
|
+
def base_paths
|
54
|
+
%w[
|
55
|
+
app/views
|
56
|
+
app/views/messages
|
57
|
+
app/views/mailers
|
58
|
+
app/views/application
|
59
|
+
app/views/layouts
|
60
|
+
].freeze
|
61
|
+
end
|
62
|
+
|
63
|
+
protected
|
64
|
+
|
65
|
+
def method_missing(method_name, *args) # :nodoc:
|
66
|
+
if action_methods.include? method_name.to_s
|
67
|
+
MessageDelivery.new(self, method_name, *args)
|
68
|
+
else
|
69
|
+
super
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
attr_internal :short_message
|
75
|
+
attr_accessor :template_name, :template_path
|
76
|
+
|
77
|
+
def initialize
|
78
|
+
super
|
79
|
+
@_short_message_was_called = false
|
80
|
+
@_short_message = ShortMessage.new
|
81
|
+
end
|
82
|
+
|
83
|
+
def payload
|
84
|
+
default_options
|
85
|
+
end
|
86
|
+
|
87
|
+
def sms(params = {}, &block)
|
88
|
+
raise ArgumentError, 'You need to provide at least a receipient' if params[:to].blank?
|
89
|
+
return short_message if @_short_message_was_called && !block
|
90
|
+
|
91
|
+
self.template_name = params[:template_name].presence || template_name
|
92
|
+
self.template_path = params[:template_path].presence || template_path
|
93
|
+
|
94
|
+
@_short_message_was_called = true
|
95
|
+
wrap_sms_provider_behavior!
|
96
|
+
|
97
|
+
# elookup_context.view_paths = (lookup_context.view_paths.to_a + self.class.base_paths).flatten.uniq
|
98
|
+
|
99
|
+
short_message.to = params[:to]
|
100
|
+
short_message.debug = params[:debug]
|
101
|
+
short_message.message = params[:message] || render(full_template_path)
|
102
|
+
short_message.options = params.reject! { |p| %i[to debug message].include?(p) }
|
103
|
+
short_message
|
104
|
+
end
|
105
|
+
|
106
|
+
def process(method_name, *args)
|
107
|
+
payload = {
|
108
|
+
messenger: self.class.name,
|
109
|
+
action: method_name
|
110
|
+
}
|
111
|
+
|
112
|
+
self.template_path ||= self.class.name.underscore
|
113
|
+
self.template_name ||= method_name
|
114
|
+
|
115
|
+
ActiveSupport::Notifications.instrument('process.action_short_message', payload) do
|
116
|
+
super
|
117
|
+
@_short_message = NullMessage.new unless @_short_message_was_called
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
def full_template_path
|
122
|
+
[template_path, template_name].join('/')
|
123
|
+
end
|
124
|
+
|
125
|
+
class NullMessage # :nodoc:
|
126
|
+
def body
|
127
|
+
''
|
128
|
+
end
|
129
|
+
|
130
|
+
def header
|
131
|
+
{}
|
132
|
+
end
|
133
|
+
|
134
|
+
def respond_to?(_string, _include_all = false)
|
135
|
+
true
|
136
|
+
end
|
137
|
+
|
138
|
+
def method_missing(*_args)
|
139
|
+
nil
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
ActiveSupport.run_load_hooks(:action_short_message, self)
|
144
|
+
end
|
145
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'active_job'
|
2
|
+
|
3
|
+
module ActionShortMessage
|
4
|
+
class DeliveryJob < ActiveJob::Base
|
5
|
+
queue_as { ActionShortMessage::Base.deliver_later_queue_name }
|
6
|
+
|
7
|
+
rescue_from StandardError, with: :handle_exception_with_message_class
|
8
|
+
|
9
|
+
def perform(message, message_method, delivery_method, *args) # :nodoc:
|
10
|
+
message.constantize.public_send(message_method, *args).send(delivery_method)
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def message_class
|
16
|
+
if message = Array(@serialized_arguments).first || Array(arguments).first
|
17
|
+
message.constantize
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def handle_exception_with_message_class(exception)
|
22
|
+
if klass = message_class
|
23
|
+
klass.handle_exception exception
|
24
|
+
else
|
25
|
+
raise exception
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module ActionShortMessage
|
2
|
+
class Interceptor
|
3
|
+
cattr_accessor :blacklist
|
4
|
+
self.blacklist = {}
|
5
|
+
|
6
|
+
class << self
|
7
|
+
def register(conditions = {})
|
8
|
+
raise TypeError, 'Invalid type. Please provide a hash object' unless conditions.methods.include?(:key)
|
9
|
+
|
10
|
+
conditions.each do |attribute, condition|
|
11
|
+
@@blacklist[attribute.to_sym] ||= []
|
12
|
+
@@blacklist[attribute.to_sym].push(condition)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def registered_for?(message)
|
17
|
+
@@blacklist.each do |attribute, conditions|
|
18
|
+
value = message.send(attribute.to_sym)
|
19
|
+
|
20
|
+
conditions.each do |condition|
|
21
|
+
return true if value.send(match_method_for(condition), condition)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
false
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def match_method_for(condition)
|
31
|
+
condition.is_a?(Regexp) ? :=~ : :==
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'active_support/log_subscriber'
|
2
|
+
|
3
|
+
module ActionShortMessage
|
4
|
+
class LogSubscriber < ActiveSupport::LogSubscriber
|
5
|
+
def deliver(_event)
|
6
|
+
info do
|
7
|
+
# preform_adapter = event.payload[:preform_deliveries]
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def process(event)
|
12
|
+
debug do
|
13
|
+
messenger = event.payload[:messenger]
|
14
|
+
action = event.payload[:action]
|
15
|
+
"#{messenger}##{action}: processed SMS in #{event.duration.round(1)}ms"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def logger
|
20
|
+
ActionShortMessage::Base.logger || Rails.logger
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
ActionShortMessage::LogSubscriber.attach_to :action_short_message
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module ActionShortMessage
|
2
|
+
class MessageDelivery
|
3
|
+
attr_reader :messenger_class, :action, :args
|
4
|
+
|
5
|
+
def initialize(messenger_class, action, *args)
|
6
|
+
@messenger_class = messenger_class
|
7
|
+
@action = action
|
8
|
+
@args = args
|
9
|
+
end
|
10
|
+
|
11
|
+
def messenger_instance
|
12
|
+
processed_messenger.send(action, *@args)
|
13
|
+
end
|
14
|
+
|
15
|
+
def deliver_now
|
16
|
+
processed_messenger.send(action, *@args).deliver
|
17
|
+
end
|
18
|
+
|
19
|
+
def deliver_later(options = {})
|
20
|
+
enqueue_delivery :deliver_now, options
|
21
|
+
end
|
22
|
+
|
23
|
+
protected
|
24
|
+
|
25
|
+
def processed_messenger
|
26
|
+
# message_delivery with template and all messenger need
|
27
|
+
@processed_messenger ||= @messenger_class.new.tap do |messenger_instance|
|
28
|
+
messenger_instance.process @action, *@args
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def enqueue_delivery(delivery_method, options = {})
|
33
|
+
args = @messenger_class.name, @action.to_s, delivery_method.to_s, *@args
|
34
|
+
::ActionShortMessage::DeliveryJob.set(options).perform_later(*args)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'active_job/railtie'
|
2
|
+
require 'action_short_message'
|
3
|
+
require 'rails'
|
4
|
+
|
5
|
+
module ActionShortMessage
|
6
|
+
class Railtie < Rails::Railtie # :nodoc:
|
7
|
+
config.action_short_message = ActiveSupport::OrderedOptions.new
|
8
|
+
config.eager_load_namespaces << ActionShortMessage
|
9
|
+
|
10
|
+
initializer 'action_short_message.logger' do
|
11
|
+
ActiveSupport.on_load(:action_short_message) { self.logger ||= Rails.logger }
|
12
|
+
end
|
13
|
+
|
14
|
+
config.after_initialize do |app|
|
15
|
+
ActiveSupport.on_load(:action_short_message) do
|
16
|
+
options = app.config.action_short_message
|
17
|
+
options.each { |k, v| send("#{k}=", v) }
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
module ActionShortMessage
|
2
|
+
# include ActionShortMessage::Adapters
|
3
|
+
|
4
|
+
class ShortMessage
|
5
|
+
attr_accessor :action, :message, :to, :debug, :options, :raise_delivery_errors
|
6
|
+
attr_reader :sms_provider
|
7
|
+
|
8
|
+
def sms_provider(method = nil, settings = {})
|
9
|
+
@sms_provider = method.new(settings)
|
10
|
+
end
|
11
|
+
|
12
|
+
# Initialize the observers and interceptors arrays
|
13
|
+
@@delivery_notification_observers = []
|
14
|
+
|
15
|
+
# You can register an object to be informed of every short message that is sent through
|
16
|
+
# this method.
|
17
|
+
#
|
18
|
+
# Your object needs to respond to a single method #delivered_message(message)
|
19
|
+
# which receives the short message that is sent.
|
20
|
+
def self.register_observer(observer)
|
21
|
+
@@delivery_notification_observers << observer unless @@delivery_notification_observers.include?(observer)
|
22
|
+
end
|
23
|
+
|
24
|
+
# Unregister the given observer, allowing short message to resume operations
|
25
|
+
# without it.
|
26
|
+
def self.unregister_observer(observer)
|
27
|
+
@@delivery_notification_observers.delete(observer)
|
28
|
+
end
|
29
|
+
|
30
|
+
def inform_observers
|
31
|
+
@@delivery_notification_observers.each do |observer|
|
32
|
+
observer.delivered_message(self)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.delivery_adapters; end
|
37
|
+
|
38
|
+
def debug?
|
39
|
+
!!@debug
|
40
|
+
end
|
41
|
+
|
42
|
+
def deliver
|
43
|
+
ActiveSupport::Notifications.instrument('deliver.action_short_message', { messagage: message, to: to }) do
|
44
|
+
@sms_provider.send_message(message, options.merge(to: to))
|
45
|
+
end
|
46
|
+
inform_observers
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module ActionShortMessage
|
2
|
+
module SMSProviders
|
3
|
+
class Base
|
4
|
+
def initialize(params = {})
|
5
|
+
@params = params
|
6
|
+
end
|
7
|
+
|
8
|
+
def send_message(_message, _params = {})
|
9
|
+
# raise ArgumentError, 'You should provide at least one phone for sending messages' unless senders.present?
|
10
|
+
|
11
|
+
# @from = pick_sender
|
12
|
+
end
|
13
|
+
|
14
|
+
def senders
|
15
|
+
@senders ||= @params[:from].present? ? @params[:from] : ActionShortMessage::Base.default_params[:from]
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def pick_sender
|
21
|
+
senders.is_a?(String) ? senders : senders.shuffle.sample
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module ActionShortMessage
|
2
|
+
module SMSProviders
|
3
|
+
class Test < Base
|
4
|
+
def initialize(params = {})
|
5
|
+
super(params)
|
6
|
+
end
|
7
|
+
|
8
|
+
def send_message(message, params = {})
|
9
|
+
puts "SMS TEST: send short message to #{params[:to]}, context:"
|
10
|
+
puts 'options'
|
11
|
+
puts params
|
12
|
+
puts message
|
13
|
+
puts 'Completed!'
|
14
|
+
super(message, params)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'action_short_message/sms_providers/base'
|
2
|
+
require 'action_short_message/sms_providers/test'
|
3
|
+
|
4
|
+
module ActionShortMessage
|
5
|
+
module SMSProviders
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
included do
|
9
|
+
cattr_accessor :raise_delivery_errors
|
10
|
+
cattr_accessor :perform_deliveries, default: true
|
11
|
+
cattr_accessor :deliver_later_queue_name, default: :shore_messengers
|
12
|
+
|
13
|
+
class_attribute :sms_providers, default: {}.freeze
|
14
|
+
class_attribute :sms_provider, default: :test
|
15
|
+
|
16
|
+
add_sms_provider :test, SMSProviders::Test
|
17
|
+
end
|
18
|
+
|
19
|
+
module ClassMethods
|
20
|
+
delegate :sms_providers, :sms_providers=, to: SMSProviders::Test
|
21
|
+
|
22
|
+
def add_sms_provider(symbol, klass, default_options = {})
|
23
|
+
class_attribute(:"#{symbol}_settings") unless respond_to?(:"#{symbol}_settings")
|
24
|
+
public_send(:"#{symbol}_settings=", default_options)
|
25
|
+
self.sms_providers = sms_providers.merge(symbol.to_sym => klass).freeze
|
26
|
+
end
|
27
|
+
|
28
|
+
def wrap_sms_provider_behavior(short_message) # :nodoc:
|
29
|
+
raise 'SMS provider cannot be nil' if sms_provider.nil?
|
30
|
+
|
31
|
+
case sms_provider
|
32
|
+
when NilClass
|
33
|
+
raise 'Delivery method cannot be nil'
|
34
|
+
when Symbol
|
35
|
+
if klass = sms_providers[sms_provider]
|
36
|
+
short_message.sms_provider(klass, send(:"#{sms_provider}_settings") || {})
|
37
|
+
else
|
38
|
+
raise "Invalid delivery method #{sms_provider.inspect}"
|
39
|
+
end
|
40
|
+
else
|
41
|
+
short_message.sms_provider(method)
|
42
|
+
end
|
43
|
+
# short_message.perform_deliveries = perform_deliveries
|
44
|
+
short_message.raise_delivery_errors = raise_delivery_errors
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def wrap_sms_provider_behavior!
|
49
|
+
self.class.wrap_sms_provider_behavior(short_message)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'abstract_controller'
|
2
|
+
|
3
|
+
require 'action_short_message/sms_providers'
|
4
|
+
require 'action_short_message/base'
|
5
|
+
require 'action_short_message/delivery_job'
|
6
|
+
require 'action_short_message/interceptor'
|
7
|
+
require 'action_short_message/short_message'
|
8
|
+
require 'action_short_message/message_delivery'
|
9
|
+
require 'action_short_message/version'
|
10
|
+
require 'action_short_message/railtie'
|
11
|
+
|
12
|
+
require 'active_support'
|
13
|
+
require 'active_support/rails'
|
14
|
+
require 'active_support/core_ext/class'
|
15
|
+
require 'active_support/core_ext/module/attr_internal'
|
16
|
+
require 'active_support/core_ext/string/inflections'
|
17
|
+
require 'active_support/lazy_load_hooks'
|
18
|
+
|
19
|
+
module ActionShortMessage
|
20
|
+
extend ::ActiveSupport::Autoload
|
21
|
+
|
22
|
+
autoload :Base
|
23
|
+
autoload :DeliveryJob
|
24
|
+
autoload :MessageDelivery
|
25
|
+
autoload :Messeng
|
26
|
+
|
27
|
+
def self.eager_load!
|
28
|
+
super
|
29
|
+
Base.descendants.each do |messenger|
|
30
|
+
messenger.eager_load! unless messenger.abstract?
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
autoload :Mime, 'action_dispatch/http/mime_type'
|
36
|
+
|
37
|
+
ActiveSupport.on_load(:action_view) do
|
38
|
+
ActionView::Base.default_formats ||= Mime::SET.symbols
|
39
|
+
ActionView::Template::Types.delegate_to Mime
|
40
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
Description:
|
2
|
+
============
|
3
|
+
Stubs out a new message class. Passes the message name, either
|
4
|
+
CamelCased or under_scored, and an optional list of emails as arguments.
|
5
|
+
|
6
|
+
This generates a message class in app/messages.
|
7
|
+
|
8
|
+
Example:
|
9
|
+
========
|
10
|
+
rails generate message Authentication signup forgot_password lock
|
11
|
+
creates a Authentication message class under app/messages/authentication_message.rb
|
12
|
+
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Rails
|
2
|
+
module Generators
|
3
|
+
class MessageGenerator < NamedBase
|
4
|
+
source_root File.expand_path("../templates", __FILE__)
|
5
|
+
|
6
|
+
argument :actions, type: :array, default: [], banner: "method method"
|
7
|
+
|
8
|
+
check_class_collision suffix: "Messenger"
|
9
|
+
|
10
|
+
def create_message_file
|
11
|
+
template "message.rb", File.join("app/messenger", class_path, "#{file_name}_messenger.rb")
|
12
|
+
end
|
13
|
+
|
14
|
+
protected
|
15
|
+
def file_name
|
16
|
+
@_file_name ||= super.gsub(/_messag/i, '')
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
metadata
ADDED
@@ -0,0 +1,182 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: action_short_message
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- David
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-05-24 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: actionpack
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 5.0.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 5.0.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: actionview
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 5.0.0
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 5.0.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: activejob
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 5.0.0
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 5.0.0
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: bundler
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '2.3'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '2.3'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: codecov
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 0.4.3
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 0.4.3
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rake
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '11.0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '11.0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rspec
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '3.0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '3.0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: webmock
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '3.14'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '3.14'
|
125
|
+
description: Refenrence dballona/action_short_message. SMS/Text Messages gem, just
|
126
|
+
like ActionShortMessage
|
127
|
+
email:
|
128
|
+
- z22919456@gmail.com
|
129
|
+
executables: []
|
130
|
+
extensions: []
|
131
|
+
extra_rdoc_files: []
|
132
|
+
files:
|
133
|
+
- ".gitignore"
|
134
|
+
- ".rspec"
|
135
|
+
- ".travis.yml"
|
136
|
+
- Gemfile
|
137
|
+
- LICENSE.txt
|
138
|
+
- README.md
|
139
|
+
- Rakefile
|
140
|
+
- action_short_message.gemspec
|
141
|
+
- bin/console
|
142
|
+
- bin/setup
|
143
|
+
- lib/action_short_message.rb
|
144
|
+
- lib/action_short_message/base.rb
|
145
|
+
- lib/action_short_message/delivery_job.rb
|
146
|
+
- lib/action_short_message/interceptor.rb
|
147
|
+
- lib/action_short_message/log_subscriber.rb
|
148
|
+
- lib/action_short_message/message_delivery.rb
|
149
|
+
- lib/action_short_message/railtie.rb
|
150
|
+
- lib/action_short_message/short_message.rb
|
151
|
+
- lib/action_short_message/sms_providers.rb
|
152
|
+
- lib/action_short_message/sms_providers/base.rb
|
153
|
+
- lib/action_short_message/sms_providers/test.rb
|
154
|
+
- lib/action_short_message/version.rb
|
155
|
+
- lib/rails/generators/message/USAGE
|
156
|
+
- lib/rails/generators/message/message_generator.rb
|
157
|
+
- lib/rails/generators/message/templates/messageer.rb
|
158
|
+
homepage: http://github.com/z22919456/action_short_message
|
159
|
+
licenses:
|
160
|
+
- MIT
|
161
|
+
metadata: {}
|
162
|
+
post_install_message:
|
163
|
+
rdoc_options: []
|
164
|
+
require_paths:
|
165
|
+
- lib
|
166
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
167
|
+
requirements:
|
168
|
+
- - ">="
|
169
|
+
- !ruby/object:Gem::Version
|
170
|
+
version: '0'
|
171
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
172
|
+
requirements:
|
173
|
+
- - ">="
|
174
|
+
- !ruby/object:Gem::Version
|
175
|
+
version: '0'
|
176
|
+
requirements: []
|
177
|
+
rubygems_version: 3.1.6
|
178
|
+
signing_key:
|
179
|
+
specification_version: 4
|
180
|
+
summary: Refenrence dballona/action_short_message. SMS/Text Messages gem, just like
|
181
|
+
ActionShortMessage
|
182
|
+
test_files: []
|