action_messenger 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +5 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +104 -0
- data/LICENSE.txt +21 -0
- data/README.md +110 -0
- data/Rakefile +6 -0
- data/action_messenger.gemspec +31 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/action_messenger/base.rb +100 -0
- data/lib/action_messenger/config.rb +20 -0
- data/lib/action_messenger/log_subscriber.rb +31 -0
- data/lib/action_messenger/message_delivery.rb +37 -0
- data/lib/action_messenger/message_delivery_job.rb +12 -0
- data/lib/action_messenger/providers/slack.rb +53 -0
- data/lib/action_messenger/railtie.rb +9 -0
- data/lib/action_messenger/rescuable.rb +14 -0
- data/lib/action_messenger/version.rb +3 -0
- data/lib/action_messenger.rb +22 -0
- data/lib/generators/rails/messenger/USAGE +15 -0
- data/lib/generators/rails/messenger/messenger_generator.rb +44 -0
- data/lib/generators/rails/messenger/templates/application_messenger.rb.tt +4 -0
- data/lib/generators/rails/messenger/templates/messenger.rb.tt +11 -0
- data/lib/generators/rails/messenger/templates/messenger_template.rb.tt +0 -0
- metadata +182 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 2f04f2f8befdb14f95951bc527b43883a21825b556726e2e6366293c057a9dcf
|
4
|
+
data.tar.gz: b73e9bdfce191d46db51fd9cfef0dcd68b640e19f48227bd39fe5155511cb2ef
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 32c71d201b64e43f8bcc55f585820be1b4b2ba64bf148d423398d018c25278661512e08b025c7b0cd41aec5c158ef025728d4df22175aa93dc377a57a4243325
|
7
|
+
data.tar.gz: daeb79b147a66337db42e4b580be012f653d54a27f417f07b82b3b7b9eaf32fa9cca87b12dd4a97eb06e36797305c3354856e1b32e69b27139395434101043af
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
action_messenger (0.0.1)
|
5
|
+
actionpack
|
6
|
+
actionview
|
7
|
+
activejob
|
8
|
+
activesupport
|
9
|
+
slack-ruby-client
|
10
|
+
|
11
|
+
GEM
|
12
|
+
remote: https://rubygems.org/
|
13
|
+
specs:
|
14
|
+
actionpack (5.2.0)
|
15
|
+
actionview (= 5.2.0)
|
16
|
+
activesupport (= 5.2.0)
|
17
|
+
rack (~> 2.0)
|
18
|
+
rack-test (>= 0.6.3)
|
19
|
+
rails-dom-testing (~> 2.0)
|
20
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.2)
|
21
|
+
actionview (5.2.0)
|
22
|
+
activesupport (= 5.2.0)
|
23
|
+
builder (~> 3.1)
|
24
|
+
erubi (~> 1.4)
|
25
|
+
rails-dom-testing (~> 2.0)
|
26
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.3)
|
27
|
+
activejob (5.2.0)
|
28
|
+
activesupport (= 5.2.0)
|
29
|
+
globalid (>= 0.3.6)
|
30
|
+
activesupport (5.2.0)
|
31
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
32
|
+
i18n (>= 0.7, < 2)
|
33
|
+
minitest (~> 5.1)
|
34
|
+
tzinfo (~> 1.1)
|
35
|
+
builder (3.2.3)
|
36
|
+
concurrent-ruby (1.0.5)
|
37
|
+
crass (1.0.4)
|
38
|
+
diff-lcs (1.3)
|
39
|
+
erubi (1.7.1)
|
40
|
+
faraday (0.15.2)
|
41
|
+
multipart-post (>= 1.2, < 3)
|
42
|
+
faraday_middleware (0.12.2)
|
43
|
+
faraday (>= 0.7.4, < 1.0)
|
44
|
+
gli (2.17.1)
|
45
|
+
globalid (0.4.1)
|
46
|
+
activesupport (>= 4.2.0)
|
47
|
+
hashie (3.5.7)
|
48
|
+
i18n (1.0.1)
|
49
|
+
concurrent-ruby (~> 1.0)
|
50
|
+
loofah (2.2.2)
|
51
|
+
crass (~> 1.0.2)
|
52
|
+
nokogiri (>= 1.5.9)
|
53
|
+
mini_portile2 (2.3.0)
|
54
|
+
minitest (5.11.3)
|
55
|
+
multipart-post (2.0.0)
|
56
|
+
nokogiri (1.8.4)
|
57
|
+
mini_portile2 (~> 2.3.0)
|
58
|
+
rack (2.0.5)
|
59
|
+
rack-test (1.0.0)
|
60
|
+
rack (>= 1.0, < 3)
|
61
|
+
rails-dom-testing (2.0.3)
|
62
|
+
activesupport (>= 4.2.0)
|
63
|
+
nokogiri (>= 1.6)
|
64
|
+
rails-html-sanitizer (1.0.4)
|
65
|
+
loofah (~> 2.2, >= 2.2.2)
|
66
|
+
rake (10.5.0)
|
67
|
+
rspec (3.7.0)
|
68
|
+
rspec-core (~> 3.7.0)
|
69
|
+
rspec-expectations (~> 3.7.0)
|
70
|
+
rspec-mocks (~> 3.7.0)
|
71
|
+
rspec-core (3.7.1)
|
72
|
+
rspec-support (~> 3.7.0)
|
73
|
+
rspec-expectations (3.7.0)
|
74
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
75
|
+
rspec-support (~> 3.7.0)
|
76
|
+
rspec-mocks (3.7.0)
|
77
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
78
|
+
rspec-support (~> 3.7.0)
|
79
|
+
rspec-support (3.7.1)
|
80
|
+
slack-ruby-client (0.11.1)
|
81
|
+
activesupport
|
82
|
+
faraday (>= 0.9)
|
83
|
+
faraday_middleware
|
84
|
+
gli
|
85
|
+
hashie
|
86
|
+
websocket-driver
|
87
|
+
thread_safe (0.3.6)
|
88
|
+
tzinfo (1.2.5)
|
89
|
+
thread_safe (~> 0.1)
|
90
|
+
websocket-driver (0.7.0)
|
91
|
+
websocket-extensions (>= 0.1.0)
|
92
|
+
websocket-extensions (0.1.3)
|
93
|
+
|
94
|
+
PLATFORMS
|
95
|
+
ruby
|
96
|
+
|
97
|
+
DEPENDENCIES
|
98
|
+
action_messenger!
|
99
|
+
bundler (~> 1.16)
|
100
|
+
rake (~> 10.0)
|
101
|
+
rspec (~> 3.0)
|
102
|
+
|
103
|
+
BUNDLED WITH
|
104
|
+
1.16.2
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 shun.hikita
|
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,110 @@
|
|
1
|
+
# ActionMessenger
|
2
|
+
|
3
|
+
Framework for delivering messages to Messenger. Currently only slack is supported.
|
4
|
+
|
5
|
+
:warning: **This is highly experimental project. Current version is 0.0.1**
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'action_messenger'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install action_messenger
|
22
|
+
|
23
|
+
## Configuration
|
24
|
+
|
25
|
+
```rb
|
26
|
+
ActionMessenger.configure do |config|
|
27
|
+
config.slack_api_token = ENV['SLACK_API_TOKEN'] # for slack
|
28
|
+
config.views_path = "/app/views" #default: Rails.root.join("app","views")
|
29
|
+
config.logger = Logger.new("your_log_path") # default: Rails.logger
|
30
|
+
end
|
31
|
+
```
|
32
|
+
|
33
|
+
## Generation
|
34
|
+
|
35
|
+
### Rails
|
36
|
+
|
37
|
+
```
|
38
|
+
bundle exec rails generate messenger account registerd_notify removed_notify
|
39
|
+
|
40
|
+
# create app/messengers/account_messenger.rb
|
41
|
+
# create app/messengers/application_messenger.rb
|
42
|
+
# create app/views/account_messengers/registerd_notify.text.erb
|
43
|
+
# create app/views/account_messengers/removed_notify.text.erb
|
44
|
+
```
|
45
|
+
|
46
|
+
|
47
|
+
## Usage
|
48
|
+
|
49
|
+
```rb
|
50
|
+
class AccountMessenger < ApplicationMessenger
|
51
|
+
|
52
|
+
def registered_notify(registered_accout_id)
|
53
|
+
@account = Account.find(registered_account)
|
54
|
+
message_to_slack(channel: "#account-registerd-notice")
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
AccountMessenger.registerd_notify(@account.id).deliver_now!
|
60
|
+
# or
|
61
|
+
AccountMessenger.registerd_notify(@account.id).deliver_later! # for active job
|
62
|
+
```
|
63
|
+
|
64
|
+
### Handle exception
|
65
|
+
|
66
|
+
```rb
|
67
|
+
class AccountMessenger < ApplicationMessenger
|
68
|
+
|
69
|
+
rescue_from Exception, with: -> { }
|
70
|
+
rescue_from HogeError, with: -> { }
|
71
|
+
|
72
|
+
end
|
73
|
+
```
|
74
|
+
|
75
|
+
## Send messages
|
76
|
+
|
77
|
+
In the instance method of the class inheriting ActionMessenger::Base, the following methods can be used.
|
78
|
+
|
79
|
+
### Message to Slack
|
80
|
+
|
81
|
+
```rb
|
82
|
+
# When the text option is specified, it becomes a message, and if not specified, the contents of the corresponding View template becomes a message.
|
83
|
+
message_to_slack(channel: '#sample', options: {text: 'hogehoge'})
|
84
|
+
# You can also specify attachments.
|
85
|
+
message_to_slack(channel: '#sample', options: {attachments: [{"pretext": "pre-hello", "text": "text-world"}]})
|
86
|
+
# And other options.
|
87
|
+
# -> https://api.slack.com/methods/chat.postMessage
|
88
|
+
```
|
89
|
+
|
90
|
+
### File upload to Slack
|
91
|
+
|
92
|
+
```rb
|
93
|
+
upload_file_to_slack(channels: '#general',file: Faraday::UploadIO.new('/path/to/sample.jpg', 'image/jpg'), options: {})
|
94
|
+
# And other options.
|
95
|
+
# -> https://api.slack.com/methods/files.upload
|
96
|
+
```
|
97
|
+
|
98
|
+
## Development
|
99
|
+
|
100
|
+
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.
|
101
|
+
|
102
|
+
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).
|
103
|
+
|
104
|
+
## Contributing
|
105
|
+
|
106
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/action_messenger.
|
107
|
+
|
108
|
+
## License
|
109
|
+
|
110
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "action_messenger/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "action_messenger"
|
8
|
+
spec.version = ActionMessenger::VERSION
|
9
|
+
spec.authors = ["h1kita"]
|
10
|
+
|
11
|
+
spec.summary = "Framework for delivering messages to Messenger (ex. slack)"
|
12
|
+
spec.description = "delivering messages to Messenger (ex. slack) using the familiar controller/view pattern."
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
16
|
+
f.match(%r{^(test|spec|features)/})
|
17
|
+
end
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
25
|
+
|
26
|
+
spec.add_dependency 'slack-ruby-client'
|
27
|
+
spec.add_dependency 'actionpack'
|
28
|
+
spec.add_dependency 'actionview'
|
29
|
+
spec.add_dependency 'activesupport'
|
30
|
+
spec.add_dependency 'activejob'
|
31
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "action_messenger"
|
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(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
require 'action_messenger/rescuable'
|
2
|
+
require 'action_messenger/log_subscriber'
|
3
|
+
|
4
|
+
module ActionMessenger
|
5
|
+
class Base < AbstractController::Base
|
6
|
+
|
7
|
+
include AbstractController::Rendering
|
8
|
+
include AbstractController::Logger
|
9
|
+
include AbstractController::Helpers
|
10
|
+
include ActionView::Layouts
|
11
|
+
include ActionMessenger::Rescuable
|
12
|
+
|
13
|
+
class << self
|
14
|
+
|
15
|
+
def logger
|
16
|
+
ActionMessenger.config.logger || (defined?(Rails) ? Rails.logger : nil)
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def method_missing(method_name, *args)
|
22
|
+
if action_methods.include?(method_name.to_s)
|
23
|
+
ActionMessenger::MessageDelivery.new(self, method_name, *args)
|
24
|
+
else
|
25
|
+
super
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def respond_to_missing?(method, include_all = false)
|
30
|
+
action_methods.include?(method.to_s) || super
|
31
|
+
end
|
32
|
+
|
33
|
+
end # end class << self
|
34
|
+
|
35
|
+
|
36
|
+
attr_reader :caller_method_name
|
37
|
+
|
38
|
+
# message to slack
|
39
|
+
#
|
40
|
+
# @param [String] channel slack channel.
|
41
|
+
# ex. #general
|
42
|
+
# @param [Hash] options Slack API Request Options
|
43
|
+
# ex. https://api.slack.com/methods/chat.postMessage
|
44
|
+
#
|
45
|
+
# ex. message_to_slack(channel: '#general', options: {text: 'sample'})
|
46
|
+
def message_to_slack(channel:, options: {})
|
47
|
+
@caller_method_name = caller[0][/`([^']*)'/, 1]
|
48
|
+
options = apply_defaults(options)
|
49
|
+
message = nil
|
50
|
+
ActiveSupport::Notifications.instrument('message_to_slack.action_messenger', channel: channel, body: options[:text]) do
|
51
|
+
message = slack_client.message(channel, options)
|
52
|
+
end
|
53
|
+
message
|
54
|
+
end
|
55
|
+
|
56
|
+
# upload file to slack
|
57
|
+
#
|
58
|
+
# @param [String] channels slack channel
|
59
|
+
# ex. #general, #hoge
|
60
|
+
# @param [Faraday::UploadIO] file upload file
|
61
|
+
# ex. Faraday::UploadIO.new('/path/to/sample.jpg', 'image/jpg')
|
62
|
+
# @param [Hash] options
|
63
|
+
def upload_file_to_slack(channels: ,file: ,options: {})
|
64
|
+
upload_file = nil
|
65
|
+
ActiveSupport::Notifications.instrument('upload_file_to_slack.action_messenger', channels: channels) do
|
66
|
+
upload_file = slack_client.upload_file(channels, file, options)
|
67
|
+
end
|
68
|
+
upload_file
|
69
|
+
end
|
70
|
+
|
71
|
+
|
72
|
+
private
|
73
|
+
|
74
|
+
def apply_defaults(options)
|
75
|
+
options[:text] = render_template_text if options[:text].blank?
|
76
|
+
options
|
77
|
+
end
|
78
|
+
|
79
|
+
def render_template_text
|
80
|
+
return '' if views_path.blank?
|
81
|
+
|
82
|
+
lookup_context = ActionView::LookupContext.new(views_path)
|
83
|
+
lookup_context.cache = false
|
84
|
+
|
85
|
+
view_context = ActionView::Base.new(lookup_context)
|
86
|
+
instance_variables.each {|name| view_context.assign(name.to_s.gsub('@', '').to_sym => instance_variable_get(name))}
|
87
|
+
|
88
|
+
view_context.render(template: caller_method_name.to_s, prefixes: self.class.name.underscore.pluralize)
|
89
|
+
end
|
90
|
+
|
91
|
+
def views_path
|
92
|
+
ActionMessenger.config.views_path || (defined?(Rails) ? Rails.root.join('app', 'views') : nil)
|
93
|
+
end
|
94
|
+
|
95
|
+
def slack_client
|
96
|
+
ActionMessenger::Providers::Slack.new
|
97
|
+
end
|
98
|
+
|
99
|
+
end
|
100
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module ActionMessenger
|
2
|
+
|
3
|
+
class << self
|
4
|
+
def configure
|
5
|
+
yield config
|
6
|
+
end
|
7
|
+
|
8
|
+
def config
|
9
|
+
@config ||= ActionMessenger::Config.new
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class Config
|
14
|
+
include ActiveSupport::Configurable
|
15
|
+
config_accessor :slack_api_token
|
16
|
+
config_accessor :views_path
|
17
|
+
config_accessor :logger
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'active_support/log_subscriber'
|
2
|
+
|
3
|
+
module ActionMessenger
|
4
|
+
class LogSubscriber < ActiveSupport::LogSubscriber
|
5
|
+
|
6
|
+
def deliver_now!(event)
|
7
|
+
info do
|
8
|
+
"#{Time.current} Sent Message (#{event.duration.round(1)}ms) #{event.payload.as_json}"
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def message_to_slack(event)
|
13
|
+
info do
|
14
|
+
"#{Time.current} Sent Message to Slack (#{event.duration.round(1)}ms) #{event.payload.as_json}"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def upload_file_to_slack(event)
|
19
|
+
info do
|
20
|
+
"#{Time.current} Upload File to Slack (#{event.duration.round(1)}ms) #{event.payload.as_json}"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def logger
|
25
|
+
ActionMessenger::Base.logger
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
ActionMessenger::LogSubscriber.attach_to :action_messenger
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'action_messenger/log_subscriber'
|
2
|
+
|
3
|
+
module ActionMessenger
|
4
|
+
class MessageDelivery
|
5
|
+
|
6
|
+
attr_reader :messenger_class, :method_name, :args
|
7
|
+
|
8
|
+
def initialize(messenger_class, method_name, *args)
|
9
|
+
@messenger_class = messenger_class
|
10
|
+
@method_name = method_name
|
11
|
+
@args = args
|
12
|
+
end
|
13
|
+
|
14
|
+
# send a message now
|
15
|
+
def deliver_now!
|
16
|
+
messenger.handle_exceptions do
|
17
|
+
ActiveSupport::Notifications.instrument('deliver_now!.action_messenger', method_name: method_name, args: args) do
|
18
|
+
if args.present?
|
19
|
+
messenger.public_send(method_name, *args)
|
20
|
+
else
|
21
|
+
messenger.public_send(method_name)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
# send a message asynchronously
|
28
|
+
def deliver_later!
|
29
|
+
ActionMessenger::MessageDeliveryJob.perform_later(self.class.name, 'deliver_now!', messenger_class.to_s, method_name.to_s, *args)
|
30
|
+
end
|
31
|
+
|
32
|
+
def messenger
|
33
|
+
@messenger ||= messenger_class.new
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'active_job'
|
2
|
+
|
3
|
+
module ActionMessenger
|
4
|
+
class MessageDeliveryJob < ActiveJob::Base
|
5
|
+
queue_as :messengers
|
6
|
+
|
7
|
+
def perform(delivery_class_name, delivery_action, messenger_class, method_name, *args)
|
8
|
+
delivery_class_name.constantize.new(messenger_class.constantize, method_name.to_sym, *args).public_send(delivery_action.to_sym)
|
9
|
+
end
|
10
|
+
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'slack-ruby-client'
|
2
|
+
|
3
|
+
module ActionMessenger
|
4
|
+
module Providers
|
5
|
+
class Slack
|
6
|
+
|
7
|
+
TokenError = Class.new(StandardError)
|
8
|
+
|
9
|
+
attr_reader :client
|
10
|
+
|
11
|
+
def initialize
|
12
|
+
raise(TokenError.new('slack api token is blank.')) if ActionMessenger.config.slack_api_token.blank?
|
13
|
+
|
14
|
+
@client = ::Slack::Web::Client.new.tap do |client|
|
15
|
+
client.token = ActionMessenger.config.slack_api_token
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
# message to slack
|
20
|
+
# @param [String] channel
|
21
|
+
# @param [Hash] options
|
22
|
+
def message(channel, options)
|
23
|
+
options = {channel: channel}.merge(message_option_to_api_hash(options))
|
24
|
+
client.chat_postMessage(options)
|
25
|
+
end
|
26
|
+
|
27
|
+
# upload file to slack
|
28
|
+
# @param [String] channels
|
29
|
+
# ex. #sample, #hoge
|
30
|
+
# @param [Faraday::UploadIO] file
|
31
|
+
# @param [Hash] options
|
32
|
+
# ex. https://api.slack.com/methods/files.upload
|
33
|
+
def upload_file(channels, file, options)
|
34
|
+
options = {channels: channels, file: file}.merge(upload_file_option_to_api_hash(options))
|
35
|
+
client.files_upload(options)
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
# @param options [Hash] Slack API Request Options
|
41
|
+
# ex. https://api.slack.com/methods/chat.postMessage
|
42
|
+
def message_option_to_api_hash(options)
|
43
|
+
raise ArgumentError('text is blank.') if options[:text].blank? && options[:attachments].blank?
|
44
|
+
|
45
|
+
options.delete_if { |_, v| v.nil? }
|
46
|
+
end
|
47
|
+
|
48
|
+
def upload_file_option_to_api_hash(options)
|
49
|
+
options.delete_if { |_, v| v.nil? }
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module ActionMessenger
|
2
|
+
module Rescuable
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
include ActiveSupport::Rescuable
|
5
|
+
|
6
|
+
# call rescue_from when an exception occurs
|
7
|
+
def handle_exceptions
|
8
|
+
yield
|
9
|
+
rescue => e
|
10
|
+
rescue_with_handler(e) || raise(e)
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'action_messenger/version'
|
2
|
+
|
3
|
+
require 'active_support'
|
4
|
+
require 'abstract_controller'
|
5
|
+
require 'action_view'
|
6
|
+
require 'action_messenger/config'
|
7
|
+
|
8
|
+
if defined?(Rails)
|
9
|
+
require 'action_messenger/railtie'
|
10
|
+
end
|
11
|
+
|
12
|
+
module ActionMessenger
|
13
|
+
extend ::ActiveSupport::Autoload
|
14
|
+
|
15
|
+
autoload :Base
|
16
|
+
autoload :MessageDelivery
|
17
|
+
autoload :MessageDeliveryJob
|
18
|
+
|
19
|
+
module Providers
|
20
|
+
require 'action_messenger/providers/slack'
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
Description:
|
2
|
+
============
|
3
|
+
Stubs out a new messenger and its views. Passes the messenger name,
|
4
|
+
either CamelCased or under_scored, and an optional list of messengers as arguments.
|
5
|
+
|
6
|
+
This generates a messenger class in app/messengers and invokes your template
|
7
|
+
engine and test framework generators.
|
8
|
+
|
9
|
+
Example:
|
10
|
+
========
|
11
|
+
rails generate messenger Account registerd_notify
|
12
|
+
|
13
|
+
creates a Account messenger class, views, and test:
|
14
|
+
Messenger: app/messengers/account_messenger.rb
|
15
|
+
Views: app/views/account_messengers/registerd_notify.text.erb
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'rails'
|
2
|
+
|
3
|
+
module Rails
|
4
|
+
module Generators
|
5
|
+
class MessengerGenerator < NamedBase
|
6
|
+
source_root File.expand_path("templates", __dir__)
|
7
|
+
|
8
|
+
argument :actions, type: :array, default: [], banner: 'method method'
|
9
|
+
|
10
|
+
check_class_collision suffix: 'Messenger'
|
11
|
+
|
12
|
+
def create_messenger_file
|
13
|
+
template 'messenger.rb', File.join('app/messengers', class_path, "#{file_name}_messenger.rb")
|
14
|
+
|
15
|
+
in_root do
|
16
|
+
if behavior == :invoke && !File.exist?(application_messenger_file_name)
|
17
|
+
template 'application_messenger.rb', application_messenger_file_name
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def create_messenger_view_file
|
23
|
+
actions.each do |action|
|
24
|
+
template 'messenger_template.rb', File.join('app/views', class_path,"#{file_name}_messengers", "#{action}.text.erb")
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def file_name
|
31
|
+
@_file_name ||= super.sub(/_messenger\z/i, '')
|
32
|
+
end
|
33
|
+
|
34
|
+
def application_messenger_file_name
|
35
|
+
@_application_messenger_file_name ||= if mountable_engine?
|
36
|
+
"app/messengers/#{namespaced_path}/application_messenger.rb"
|
37
|
+
else
|
38
|
+
'app/messengers/application_messenger.rb'
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
File without changes
|
metadata
ADDED
@@ -0,0 +1,182 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: action_messenger
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- h1kita
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-07-21 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.16'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.16'
|
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: slack-ruby-client
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: actionpack
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: actionview
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: activesupport
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :runtime
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: activejob
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :runtime
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
description: delivering messages to Messenger (ex. slack) using the familiar controller/view
|
126
|
+
pattern.
|
127
|
+
email:
|
128
|
+
executables: []
|
129
|
+
extensions: []
|
130
|
+
extra_rdoc_files: []
|
131
|
+
files:
|
132
|
+
- ".gitignore"
|
133
|
+
- ".rspec"
|
134
|
+
- ".travis.yml"
|
135
|
+
- Gemfile
|
136
|
+
- Gemfile.lock
|
137
|
+
- LICENSE.txt
|
138
|
+
- README.md
|
139
|
+
- Rakefile
|
140
|
+
- action_messenger.gemspec
|
141
|
+
- bin/console
|
142
|
+
- bin/setup
|
143
|
+
- lib/action_messenger.rb
|
144
|
+
- lib/action_messenger/base.rb
|
145
|
+
- lib/action_messenger/config.rb
|
146
|
+
- lib/action_messenger/log_subscriber.rb
|
147
|
+
- lib/action_messenger/message_delivery.rb
|
148
|
+
- lib/action_messenger/message_delivery_job.rb
|
149
|
+
- lib/action_messenger/providers/slack.rb
|
150
|
+
- lib/action_messenger/railtie.rb
|
151
|
+
- lib/action_messenger/rescuable.rb
|
152
|
+
- lib/action_messenger/version.rb
|
153
|
+
- lib/generators/rails/messenger/USAGE
|
154
|
+
- lib/generators/rails/messenger/messenger_generator.rb
|
155
|
+
- lib/generators/rails/messenger/templates/application_messenger.rb.tt
|
156
|
+
- lib/generators/rails/messenger/templates/messenger.rb.tt
|
157
|
+
- lib/generators/rails/messenger/templates/messenger_template.rb.tt
|
158
|
+
homepage:
|
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
|
+
rubyforge_project:
|
178
|
+
rubygems_version: 2.7.6
|
179
|
+
signing_key:
|
180
|
+
specification_version: 4
|
181
|
+
summary: Framework for delivering messages to Messenger (ex. slack)
|
182
|
+
test_files: []
|