slack_msgr 0.0.2 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +23 -6
- data/lib/config/constants.rb +1 -1
- data/lib/config/initializer.rb +1 -1
- data/lib/slack_msgr/chat.rb +1 -6
- data/lib/slack_msgr/configuration.rb +31 -14
- data/lib/slack_msgr/slack_method.rb +39 -0
- data/lib/utils/error_handling.rb +2 -2
- metadata +3 -3
- data/lib/slack_msgr/fetcher.rb +0 -22
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a2a6986eb6b25c10e5c937d6f72f7716489ce77e
|
4
|
+
data.tar.gz: 4882906275e2a2781e11c6b07067351ed6b2cc66
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 326e481ccb93330fb19b0d0fbe261dd83212b50ea84b2e0b0612e6cc3a91a98de8e7f48dd6269dbc2b3da61d68d8893416b192fb421f7909d9cc57764d8c0d14
|
7
|
+
data.tar.gz: 6c0ef937c0b433390fb2291c067d689e24469333a47d969a9baa01f92a3378d22ea206192aae92699fdf7c842421d9c38a7a35364fc946d6a4961db5d6651199
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -33,14 +33,30 @@ If you are using this gem with a Rails project, create an initializer file.
|
|
33
33
|
# ./config/initializers/slack_msgr.rb
|
34
34
|
|
35
35
|
SlackMsgr.configure do |config|
|
36
|
-
config.verification_token
|
37
|
-
config.client_secret
|
38
|
-
config.signing_secret
|
39
|
-
config.
|
40
|
-
|
36
|
+
config.verification_token = '1a2b3c4d5e'
|
37
|
+
config.client_secret = 'a1b2c3d4e5'
|
38
|
+
config.signing_secret = 'aa11bb22cc3'
|
39
|
+
config.access_tokens = {
|
40
|
+
bot: 'xoxp-1234567-xxxxxxxxx',
|
41
|
+
me: 'xoxb-1234567-xxxxxxxxx'
|
42
|
+
}
|
41
43
|
end
|
42
44
|
```
|
43
45
|
|
46
|
+
_NOTE_: All access tokens are configured as a hash. The name of the keys are arbitrary, and there is no limit to keys that can be configured.
|
47
|
+
|
48
|
+
Multiple oauth tokens can be used during a single session. Oauth tokens are declared in the method execution. For example, considering the configuration above, to use the token attached to the key `:me` simply pass the parameter `use_token: :me`.
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
SlackMsgr.chat(:post_message,
|
52
|
+
use_token: :me,
|
53
|
+
channel: 'announcements',
|
54
|
+
text: 'Hello world'
|
55
|
+
)
|
56
|
+
```
|
57
|
+
|
58
|
+
SlackMsgr will try to use a default token if a method is called without declaring `use_token:`. SlackMsgr takes the first token in the `access_tokens` hash as a default.
|
59
|
+
|
44
60
|
If you are using this gem outside of a Rails project, follow the configuration pattern above, but be sure to run this code before using any functionality of the gem.
|
45
61
|
|
46
62
|
## Usage
|
@@ -59,6 +75,7 @@ SlackMsgr.chat(:post_message, channel: 'announcements', text: 'Hello world')
|
|
59
75
|
# Sends the text 'Hello world' to the announcements channel
|
60
76
|
|
61
77
|
SlackMsgr.chat(:post_message, {
|
78
|
+
use_token: :bot,
|
62
79
|
channel: "C1H9RESGL",
|
63
80
|
text: "Here's a message for you",
|
64
81
|
as_user: false,
|
@@ -69,7 +86,7 @@ SlackMsgr.chat(:post_message, {
|
|
69
86
|
fallback: "This is an attachment's fallback"
|
70
87
|
}]
|
71
88
|
})
|
72
|
-
# Sends a message with an attachment as a user with the username 'testing'
|
89
|
+
# Sends a message with an attachment using the oauth token configured for :bot as a user with the username 'testing'
|
73
90
|
```
|
74
91
|
|
75
92
|
## License
|
data/lib/config/constants.rb
CHANGED
data/lib/config/initializer.rb
CHANGED
@@ -6,7 +6,7 @@ require_relative './constants'
|
|
6
6
|
|
7
7
|
path = __dir__
|
8
8
|
|
9
|
-
require "#{path}/../slack_msgr/
|
9
|
+
require "#{path}/../slack_msgr/slack_method"
|
10
10
|
require "#{path}/../slack_msgr/chat"
|
11
11
|
require "#{path}/../slack_msgr/configuration"
|
12
12
|
require "#{path}/../utils/error_handling"
|
data/lib/slack_msgr/chat.rb
CHANGED
@@ -29,12 +29,7 @@ module SlackMsgr
|
|
29
29
|
class << self
|
30
30
|
def call(method, opts = {})
|
31
31
|
chat = new(method, opts)
|
32
|
-
|
33
|
-
conn.post do |req|
|
34
|
-
req.url "/api/#{chat.method}"
|
35
|
-
req.headers['Content-Type'] = 'application/json; charset=utf-8'
|
36
|
-
req.body = chat.body
|
37
|
-
end
|
32
|
+
send_post_request_to_slack(chat)
|
38
33
|
end
|
39
34
|
end
|
40
35
|
|
@@ -6,29 +6,46 @@ module SlackMsgr
|
|
6
6
|
attr_accessor :verification_token,
|
7
7
|
:client_secret,
|
8
8
|
:signing_secret,
|
9
|
-
:
|
10
|
-
:
|
9
|
+
:access_tokens,
|
10
|
+
:set_default_token
|
11
11
|
|
12
12
|
def initialize(
|
13
13
|
verification_token: nil,
|
14
14
|
client_secret: nil,
|
15
15
|
signing_secret: nil,
|
16
|
-
|
17
|
-
|
16
|
+
access_tokens: {},
|
17
|
+
set_default_token: nil
|
18
18
|
)
|
19
|
-
@verification_token
|
20
|
-
@client_secret
|
21
|
-
@signing_secret
|
22
|
-
@
|
23
|
-
@
|
19
|
+
@verification_token = verification_token
|
20
|
+
@client_secret = client_secret
|
21
|
+
@signing_secret = signing_secret
|
22
|
+
@access_tokens = access_tokens
|
23
|
+
@set_default_token = set_default_token
|
24
24
|
end
|
25
25
|
|
26
26
|
def clear!
|
27
|
-
@verification_token
|
28
|
-
@client_secret
|
29
|
-
@signing_secret
|
30
|
-
@
|
31
|
-
@
|
27
|
+
@verification_token = nil
|
28
|
+
@client_secret = nil
|
29
|
+
@signing_secret = nil
|
30
|
+
@access_tokens = {}
|
31
|
+
@set_default_token = nil
|
32
|
+
@default_token = nil
|
33
|
+
end
|
34
|
+
|
35
|
+
def default_token
|
36
|
+
@default_token ||= initialize_default_token
|
37
|
+
end
|
38
|
+
|
39
|
+
def initialize_default_token
|
40
|
+
return unless access_tokens&.first
|
41
|
+
|
42
|
+
access_tokens[set_default_token] || access_tokens.first[1]
|
43
|
+
end
|
44
|
+
|
45
|
+
def oauth_access_token(given_token)
|
46
|
+
token = access_tokens[given_token] || default_token
|
47
|
+
ErrorHandling.raise(:configuration_error) unless token
|
48
|
+
token
|
32
49
|
end
|
33
50
|
end
|
34
51
|
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SlackMsgr
|
4
|
+
# Handles all requests with Slack API
|
5
|
+
class SlackMethod
|
6
|
+
class << self
|
7
|
+
private
|
8
|
+
|
9
|
+
attr_reader :conn
|
10
|
+
|
11
|
+
def establish_connection(given_token = nil)
|
12
|
+
@conn ||= Faraday.new(url: SLACK_URL) do |config|
|
13
|
+
config.request :url_encoded # form-encode POST params
|
14
|
+
config.adapter Faraday.default_adapter # make requests with Net::HTTP
|
15
|
+
end
|
16
|
+
conn.authorization :Bearer, SlackMsgr.configuration.oauth_access_token(given_token)
|
17
|
+
end
|
18
|
+
|
19
|
+
def conceal(token)
|
20
|
+
token.split('Bearer ').last[0..-6].gsub(/[a-zA-Z0-9]/, 'X') + token[-5..-1]
|
21
|
+
end
|
22
|
+
|
23
|
+
def add_metadata_to_response(response)
|
24
|
+
JSON.parse(response.body, symbolize_names: true)
|
25
|
+
.merge!(auth_token: conceal(conn.headers['Authorization']))
|
26
|
+
end
|
27
|
+
|
28
|
+
def send_post_request_to_slack(obj)
|
29
|
+
establish_connection(obj.opts[:use_token])
|
30
|
+
response = conn.post do |req|
|
31
|
+
req.url "/api/#{obj.method}"
|
32
|
+
req.headers['Content-Type'] = 'application/json; charset=utf-8'
|
33
|
+
req.body = obj.body
|
34
|
+
end
|
35
|
+
add_metadata_to_response(response)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
data/lib/utils/error_handling.rb
CHANGED
@@ -21,8 +21,8 @@ module SlackMsgr
|
|
21
21
|
def configuration_error(_opts)
|
22
22
|
{
|
23
23
|
exception: ConfigurationError,
|
24
|
-
message: "Error with configruation:
|
25
|
-
'
|
24
|
+
message: "Error with configruation: access_tokens not found\n" \
|
25
|
+
'At least one oauth token must be configured using access_tokens'
|
26
26
|
}
|
27
27
|
end
|
28
28
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: slack_msgr
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryan Workman
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-07-
|
11
|
+
date: 2019-07-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|
@@ -147,7 +147,7 @@ files:
|
|
147
147
|
- lib/slack_msgr.rb
|
148
148
|
- lib/slack_msgr/chat.rb
|
149
149
|
- lib/slack_msgr/configuration.rb
|
150
|
-
- lib/slack_msgr/
|
150
|
+
- lib/slack_msgr/slack_method.rb
|
151
151
|
- lib/utils/error_handling.rb
|
152
152
|
- slack_msgr.gemspec
|
153
153
|
homepage: https://github.com/rdavid1099/slack-msgr
|
data/lib/slack_msgr/fetcher.rb
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module SlackMsgr
|
4
|
-
# Handles all requests with Slack API
|
5
|
-
class SlackMethod
|
6
|
-
class << self
|
7
|
-
private
|
8
|
-
|
9
|
-
def conn
|
10
|
-
oauth_access_token = SlackMsgr.configuration.oauth_access_token
|
11
|
-
ErrorHandling.raise(:configuration_error) unless oauth_access_token
|
12
|
-
|
13
|
-
faraday = Faraday.new(url: SLACK_URL) do |config|
|
14
|
-
config.request :url_encoded # form-encode POST params
|
15
|
-
config.adapter Faraday.default_adapter # make requests with Net::HTTP
|
16
|
-
end
|
17
|
-
faraday.authorization :Bearer, oauth_access_token
|
18
|
-
faraday
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|