layabout 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +2 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/Gemfile +8 -0
- data/Gemfile.lock +63 -0
- data/LICENSE +22 -0
- data/README.md +18 -0
- data/layabout.gemspec +26 -0
- data/lib/layabout/auth_test.rb +35 -0
- data/lib/layabout/channels.rb +82 -0
- data/lib/layabout/chat.rb +67 -0
- data/lib/layabout/delete_chat.rb +48 -0
- data/lib/layabout/file_list.rb +58 -0
- data/lib/layabout/file_upload.rb +73 -0
- data/lib/layabout/helpers.rb +21 -0
- data/lib/layabout/incoming_webhook.rb +41 -0
- data/lib/layabout/slack/channel.rb +32 -0
- data/lib/layabout/slack/file.rb +56 -0
- data/lib/layabout/slack_response.rb +37 -0
- data/lib/layabout/version.rb +3 -0
- data/lib/layabout.rb +29 -0
- data/spec/fixtures/upload_test.txt +1 -0
- data/spec/fixtures/vcr_cassettes/Layabout/_channels/returns_a_list_of_channels.yml +125 -0
- data/spec/fixtures/vcr_cassettes/Layabout/_say/posts_a_message.yml +54 -0
- data/spec/fixtures/vcr_cassettes/Layabout/_upload/uploads_a_file.yml +65 -0
- data/spec/fixtures/vcr_cassettes/Layabout_AuthTest/_get/tests_auth.yml +146 -0
- data/spec/fixtures/vcr_cassettes/Layabout_Channels/_info/returns_a_Layabout_Slack_Channel.yml +96 -0
- data/spec/fixtures/vcr_cassettes/Layabout_Channels/_join/returns_a_success_response.yml +95 -0
- data/spec/fixtures/vcr_cassettes/Layabout_Channels/_leave/returns_a_success_response.yml +95 -0
- data/spec/fixtures/vcr_cassettes/Layabout_Channels/_list/returns_a_list_of_Layabout_Slack_Channel.yml +105 -0
- data/spec/fixtures/vcr_cassettes/Layabout_Chat/_post/submits_a_message.yml +95 -0
- data/spec/fixtures/vcr_cassettes/Layabout_DeleteChat/_delete/deletes_the_message.yml +146 -0
- data/spec/fixtures/vcr_cassettes/Layabout_FileList/_list/returns_a_list_of_Layabout_Slack_File.yml +57 -0
- data/spec/fixtures/vcr_cassettes/Layabout_FileUpload/_upload/uploads_a_file.yml +65 -0
- data/spec/fixtures/vcr_cassettes/Layabout_IncomingWebhook/_post/submits_a_message.yml +44 -0
- data/spec/layabout/auth_test_spec.rb +12 -0
- data/spec/layabout/channels_spec.rb +29 -0
- data/spec/layabout/chat_spec.rb +12 -0
- data/spec/layabout/configuration_spec.rb +15 -0
- data/spec/layabout/delete_chat_spec.rb +13 -0
- data/spec/layabout/file_list_spec.rb +11 -0
- data/spec/layabout/file_upload_spec.rb +12 -0
- data/spec/layabout/incoming_webhook_spec.rb +18 -0
- data/spec/layabout/layabout_spec.rb +23 -0
- data/spec/spec_helper.rb +95 -0
- metadata +217 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 616e412288a6c1cc8efb6adcf371f23d636ae4b2
|
4
|
+
data.tar.gz: 42c701ea50fb420e3e72b427cbcc6340d919683f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 55db5b1f883b75f510f29f81869411d074daeefad222952d8dd2c265ca1f31d9578f3888cc94be2d422597749a745a3fcf0341afdc642ad71910f8cbd75a22b8
|
7
|
+
data.tar.gz: 0f43ac974b215764863f5eb0e74300ebd61fd5aa4d254f936105f0a4cbbbd9a99233410e445d7b0c3c02173c60e958096990de0aed62f64f1c88c48bcc62189b
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
layabout (0.0.1)
|
5
|
+
httpi (~> 2.1.1)
|
6
|
+
multipart-post (~> 1.1.0)
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
addressable (2.3.6)
|
12
|
+
coderay (1.1.0)
|
13
|
+
crack (0.4.2)
|
14
|
+
safe_yaml (~> 1.0.0)
|
15
|
+
diff-lcs (1.2.5)
|
16
|
+
docile (1.1.5)
|
17
|
+
httpi (2.1.1)
|
18
|
+
rack
|
19
|
+
rubyntlm (~> 0.3.2)
|
20
|
+
method_source (0.8.2)
|
21
|
+
multi_json (1.10.1)
|
22
|
+
multipart-post (1.1.5)
|
23
|
+
pry (0.10.1)
|
24
|
+
coderay (~> 1.1.0)
|
25
|
+
method_source (~> 0.8.1)
|
26
|
+
slop (~> 3.4)
|
27
|
+
rack (1.5.2)
|
28
|
+
rspec (3.1.0)
|
29
|
+
rspec-core (~> 3.1.0)
|
30
|
+
rspec-expectations (~> 3.1.0)
|
31
|
+
rspec-mocks (~> 3.1.0)
|
32
|
+
rspec-core (3.1.4)
|
33
|
+
rspec-support (~> 3.1.0)
|
34
|
+
rspec-expectations (3.1.1)
|
35
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
36
|
+
rspec-support (~> 3.1.0)
|
37
|
+
rspec-mocks (3.1.1)
|
38
|
+
rspec-support (~> 3.1.0)
|
39
|
+
rspec-support (3.1.0)
|
40
|
+
rubyntlm (0.3.4)
|
41
|
+
safe_yaml (1.0.3)
|
42
|
+
simplecov (0.9.0)
|
43
|
+
docile (~> 1.1.0)
|
44
|
+
multi_json
|
45
|
+
simplecov-html (~> 0.8.0)
|
46
|
+
simplecov-html (0.8.0)
|
47
|
+
slop (3.6.0)
|
48
|
+
vcr (2.9.3)
|
49
|
+
webmock (1.18.0)
|
50
|
+
addressable (>= 2.3.6)
|
51
|
+
crack (>= 0.3.2)
|
52
|
+
|
53
|
+
PLATFORMS
|
54
|
+
ruby
|
55
|
+
|
56
|
+
DEPENDENCIES
|
57
|
+
bundler (~> 1.3)
|
58
|
+
layabout!
|
59
|
+
pry
|
60
|
+
rspec (~> 3.1.0, >= 3.1.0)
|
61
|
+
simplecov
|
62
|
+
vcr (~> 2.9.0, >= 2.9.0)
|
63
|
+
webmock (~> 1.18.0, >= 1.18.0)
|
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 James Cook
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# Layabout - Slack API Toolbelt
|
2
|
+
|
3
|
+
|
4
|
+
### Configuration
|
5
|
+
```
|
6
|
+
Layabout.configure do |config|
|
7
|
+
config.team = 'isotope11'
|
8
|
+
config.token = 'your-api-token'
|
9
|
+
end
|
10
|
+
```
|
11
|
+
|
12
|
+
### Usage
|
13
|
+
|
14
|
+
```
|
15
|
+
Layabout.say('hello world', '#random') # Posts a message
|
16
|
+
Layabout.upload('/path/to/a/silly.gif', 'C234546') # You must specify the channel ID ...
|
17
|
+
Layabout.channels # Return a list of channels
|
18
|
+
```
|
data/layabout.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
lib = File.expand_path('../lib', __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require 'layabout/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "layabout"
|
7
|
+
spec.version = Layabout::VERSION
|
8
|
+
spec.authors = ["James Cook"]
|
9
|
+
spec.email = ["jcook.rubyist@gmail.com"]
|
10
|
+
spec.description = %q{slack.com API toolbelt}
|
11
|
+
spec.summary = %q{Chat, upload files, set channel topics, send direct messages, and more via the Slack API}
|
12
|
+
spec.homepage = "https://github.com/jamescook/layabout"
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files`.split($/)
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(spec)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_dependency "httpi", "~> 2.1.1"
|
21
|
+
spec.add_dependency "multipart-post", "~> 1.1.0"
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
23
|
+
spec.add_development_dependency "rspec", "~> 3.1.0", ">= 3.1.0"
|
24
|
+
spec.add_development_dependency "vcr", "~> 2.9.0", ">= 2.9.0"
|
25
|
+
spec.add_development_dependency "webmock", "~> 1.18.0", ">= 1.18.0"
|
26
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'uri'
|
2
|
+
require 'net/http'
|
3
|
+
require 'openssl'
|
4
|
+
require 'forwardable'
|
5
|
+
|
6
|
+
module Layabout
|
7
|
+
class AuthTest
|
8
|
+
extend Forwardable
|
9
|
+
|
10
|
+
def_delegators :@configuration, :domain, :team, :token
|
11
|
+
|
12
|
+
def initialize(options={})
|
13
|
+
@configuration = ::Layabout.configuration
|
14
|
+
end
|
15
|
+
|
16
|
+
def get
|
17
|
+
SlackResponse.new(HTTPI.get(http_request))
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def http_request
|
23
|
+
HTTPI::Request.new(endpoint.to_s).tap do |httpi|
|
24
|
+
httpi.auth.ssl.verify_mode = :peer
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def endpoint
|
29
|
+
domain.tap do |uri|
|
30
|
+
uri.path = '/api/auth.test'
|
31
|
+
uri.query = "token=#{token}"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
require 'uri'
|
2
|
+
require 'httpi'
|
3
|
+
require 'forwardable'
|
4
|
+
require_relative '../layabout/slack/channel.rb'
|
5
|
+
|
6
|
+
# TODO This code was originally using Net::HTTP instead of HTTPI
|
7
|
+
# Needs some refactoring
|
8
|
+
module Layabout
|
9
|
+
|
10
|
+
class Channels
|
11
|
+
extend Forwardable
|
12
|
+
|
13
|
+
def_delegators :@configuration, :domain, :team, :token
|
14
|
+
|
15
|
+
def initialize(options={})
|
16
|
+
@configuration = ::Layabout.configuration
|
17
|
+
end
|
18
|
+
|
19
|
+
def list
|
20
|
+
process 'channels', SlackResponse.new(HTTPI.get(http_request(list_endpoint)))
|
21
|
+
end
|
22
|
+
|
23
|
+
def info(channel_id)
|
24
|
+
process('channel', SlackResponse.new(HTTPI.get(http_request(info_endpoint(channel_id)))))
|
25
|
+
end
|
26
|
+
|
27
|
+
def leave(channel_id)
|
28
|
+
SlackResponse.new(HTTPI.get(http_request(leave_endpoint(channel_id))))
|
29
|
+
end
|
30
|
+
|
31
|
+
def join(channel_name)
|
32
|
+
SlackResponse.new(HTTPI.get(http_request(join_endpoint(channel_name))))
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def process(key, slack_response)
|
38
|
+
data = slack_response.body.fetch(key)
|
39
|
+
if data.kind_of?(Array)
|
40
|
+
return data.map{|json| ::Layabout::Slack::Channel.new(json) }
|
41
|
+
else
|
42
|
+
return ::Layabout::Slack::Channel.new(data)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def http_request(endpoint)
|
47
|
+
@http_request ||= HTTPI::Request.new(endpoint.to_s).tap do |httpi|
|
48
|
+
httpi.auth.ssl.verify_mode = :peer
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def join_endpoint(channel_name)
|
53
|
+
build_channel_endpoint('join').tap do |uri|
|
54
|
+
channel_name.sub!('#', '%23')
|
55
|
+
uri.query = uri.query + "&name=#{channel_name}"
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def leave_endpoint(channel_id)
|
60
|
+
build_channel_endpoint('leave').tap do |uri|
|
61
|
+
uri.query = uri.query + "&channel=#{channel_id}"
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def list_endpoint
|
66
|
+
build_channel_endpoint 'list'
|
67
|
+
end
|
68
|
+
|
69
|
+
def info_endpoint(channel_id)
|
70
|
+
build_channel_endpoint('info').tap do |uri|
|
71
|
+
uri.query = uri.query + "&channel=#{channel_id}"
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def build_channel_endpoint(action)
|
76
|
+
domain.tap do |uri|
|
77
|
+
uri.path = "/api/channels.#{action}"
|
78
|
+
uri.query = "token=#{token}"
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'uri'
|
2
|
+
require 'httpi'
|
3
|
+
require 'forwardable'
|
4
|
+
require 'json'
|
5
|
+
|
6
|
+
module Layabout
|
7
|
+
class Chat
|
8
|
+
extend Forwardable
|
9
|
+
|
10
|
+
def_delegators :@configuration, :domain, :team, :token
|
11
|
+
|
12
|
+
attr_reader :text, :channel
|
13
|
+
|
14
|
+
def initialize(options={})
|
15
|
+
@text = options.fetch(:text)
|
16
|
+
@channel = options.fetch(:channel)
|
17
|
+
@username = options[:username]
|
18
|
+
@parse = options[:parse]
|
19
|
+
@link_names = options[:link_names]
|
20
|
+
@attachments = options[:attachments]
|
21
|
+
@unfurl_links = options[:unfurl_links]
|
22
|
+
@icon_url = options[:icon_url]
|
23
|
+
@icon_emoji = options[:icon_emoji]
|
24
|
+
@configuration = ::Layabout.configuration
|
25
|
+
end
|
26
|
+
|
27
|
+
def post_message
|
28
|
+
http_request.query = build_query
|
29
|
+
|
30
|
+
SlackResponse.new(HTTPI.get(http_request))
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def http_request
|
36
|
+
@http_request ||= HTTPI::Request.new(endpoint.to_s).tap do |httpi|
|
37
|
+
httpi.auth.ssl.verify_mode = :peer
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def build_query
|
42
|
+
{
|
43
|
+
'channel' => channel,
|
44
|
+
'text' => text,
|
45
|
+
'token' => token
|
46
|
+
}.merge(optional_params)
|
47
|
+
end
|
48
|
+
|
49
|
+
def optional_params
|
50
|
+
{}.tap do |hash|
|
51
|
+
hash['username'] = @username if @username
|
52
|
+
hash['parse'] = @parse if @parse
|
53
|
+
hash['link_names'] = @link_names if @link_names
|
54
|
+
hash['attachments'] = @attachments if @attachments
|
55
|
+
hash['unfurl_links'] = @unfurl_links if @unfurl_links
|
56
|
+
hash['icon_url'] = @icon_url if @icon_url
|
57
|
+
hash['icon_emoji'] = @icon_emoji if @icon_emoji
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def endpoint
|
62
|
+
domain.tap do |uri|
|
63
|
+
uri.path = '/api/chat.postMessage'
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'uri'
|
2
|
+
require 'httpi'
|
3
|
+
require 'forwardable'
|
4
|
+
require 'json'
|
5
|
+
|
6
|
+
module Layabout
|
7
|
+
class DeleteChat
|
8
|
+
extend Forwardable
|
9
|
+
|
10
|
+
def_delegators :@configuration, :domain, :team, :token
|
11
|
+
|
12
|
+
attr_reader :timestamp, :channel
|
13
|
+
|
14
|
+
def initialize(options={})
|
15
|
+
@channel = options.fetch(:channel)
|
16
|
+
@timestamp = options.fetch(:timestamp)
|
17
|
+
@configuration = ::Layabout.configuration
|
18
|
+
end
|
19
|
+
|
20
|
+
def delete
|
21
|
+
http_request.query = build_query
|
22
|
+
|
23
|
+
SlackResponse.new(HTTPI.get(http_request))
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def http_request
|
29
|
+
@http_request ||= HTTPI::Request.new(endpoint.to_s).tap do |httpi|
|
30
|
+
httpi.auth.ssl.verify_mode = :peer
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def build_query
|
35
|
+
{
|
36
|
+
'channel' => channel,
|
37
|
+
'ts' => timestamp,
|
38
|
+
'token' => token
|
39
|
+
}
|
40
|
+
end
|
41
|
+
|
42
|
+
def endpoint
|
43
|
+
domain.tap do |uri|
|
44
|
+
uri.path = '/api/chat.delete'
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'uri'
|
2
|
+
require 'httpi'
|
3
|
+
require 'forwardable'
|
4
|
+
require_relative '../layabout/slack/file.rb'
|
5
|
+
|
6
|
+
module Layabout
|
7
|
+
class FileList
|
8
|
+
extend Forwardable
|
9
|
+
|
10
|
+
def_delegators :@configuration, :domain, :team, :token
|
11
|
+
|
12
|
+
def initialize(options={})
|
13
|
+
@configuration = ::Layabout.configuration
|
14
|
+
@user = options[:user]
|
15
|
+
@ts_from = options[:ts_from]
|
16
|
+
@ts_to = options[:ts_to]
|
17
|
+
@types = options[:types]
|
18
|
+
@count = options[:count]
|
19
|
+
@page = options[:page]
|
20
|
+
end
|
21
|
+
|
22
|
+
def list
|
23
|
+
process 'files', SlackResponse.new(HTTPI.get(http_request(endpoint)))
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def process(key, slack_response)
|
29
|
+
data = slack_response.body.fetch(key)
|
30
|
+
# TODO Wrap this in an object that can be paginated
|
31
|
+
return data.map{|json| ::Layabout::Slack::File.new(json) }
|
32
|
+
end
|
33
|
+
|
34
|
+
def http_request(endpoint)
|
35
|
+
@http_request ||= HTTPI::Request.new(endpoint.to_s).tap do |httpi|
|
36
|
+
httpi.auth.ssl.verify_mode = :peer
|
37
|
+
httpi.query = build_query.merge('token' => token)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def build_query
|
42
|
+
{}.tap do |hash|
|
43
|
+
hash['user'] = @user if @user
|
44
|
+
hash['ts_from'] = @ts_from if @ts_from
|
45
|
+
hash['ts_to'] = @ts_to if @ts_to
|
46
|
+
hash['types'] = @types if @types
|
47
|
+
hash['count'] = @count if @count
|
48
|
+
hash['page'] = @page if @page
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def endpoint
|
53
|
+
domain.tap do |uri|
|
54
|
+
uri.path = "/api/files.list"
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
require 'net/http/post/multipart'
|
2
|
+
require 'ostruct'
|
3
|
+
|
4
|
+
module Layabout
|
5
|
+
class FileUpload
|
6
|
+
extend Forwardable
|
7
|
+
|
8
|
+
def_delegators :@configuration, :domain, :token
|
9
|
+
|
10
|
+
attr_reader :filepath, :channels
|
11
|
+
|
12
|
+
def initialize(options={})
|
13
|
+
@configuration = ::Layabout.configuration
|
14
|
+
@filepath = options.fetch(:filepath)
|
15
|
+
@channels = options.fetch(:channels)
|
16
|
+
@filetype = options[:filetype]
|
17
|
+
@filename = options[:filename]
|
18
|
+
@title = options[:title]
|
19
|
+
@initial_comment = options[:initial_comment]
|
20
|
+
end
|
21
|
+
|
22
|
+
def upload
|
23
|
+
SlackResponse.new(wrap(http_response))
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
# HTTPI doesn't support multipart posts. We can work around it by using another gem
|
29
|
+
# and handing off something that looks like a HTTPI response
|
30
|
+
def wrap(http_response)
|
31
|
+
OpenStruct.new.tap do |obj|
|
32
|
+
obj.code = http_response.code.to_i
|
33
|
+
obj.body = http_response.body
|
34
|
+
obj.headers = {}
|
35
|
+
http_response.each_header{|k,v| obj.headers[k] = v }
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def http_response
|
40
|
+
req = Net::HTTP::Post::Multipart.new(endpoint.path, params)
|
41
|
+
res = Net::HTTP.new(endpoint.host, endpoint.port)
|
42
|
+
res.use_ssl = true
|
43
|
+
res.request(req)
|
44
|
+
end
|
45
|
+
|
46
|
+
def required_params
|
47
|
+
{
|
48
|
+
"file" => UploadIO.new(File.new(filepath), "text/plain", "Gemfile"),
|
49
|
+
"token" => token,
|
50
|
+
"channels" => channels
|
51
|
+
}
|
52
|
+
end
|
53
|
+
|
54
|
+
def optional_params
|
55
|
+
{}.tap do |hash|
|
56
|
+
hash['filetype'] = @filetype if @filetype
|
57
|
+
hash['filename'] = @filename if @filename
|
58
|
+
hash['title'] = @title if @title
|
59
|
+
hash['initial_comment'] = @initial_comment if @initial_comment
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def params
|
64
|
+
required_params.merge(optional_params)
|
65
|
+
end
|
66
|
+
|
67
|
+
def endpoint
|
68
|
+
domain.tap do |uri|
|
69
|
+
uri.path = '/api/files.upload'
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require_relative './chat.rb'
|
2
|
+
require_relative './channels.rb'
|
3
|
+
require_relative './file_upload.rb'
|
4
|
+
|
5
|
+
module Layabout
|
6
|
+
module Helpers
|
7
|
+
def say(message, channel, options={})
|
8
|
+
options = options.dup.merge(text: message, channel: channel)
|
9
|
+
Chat.new(options).post_message
|
10
|
+
end
|
11
|
+
|
12
|
+
def channels(options={})
|
13
|
+
Channels.new.list
|
14
|
+
end
|
15
|
+
|
16
|
+
def upload(filepath, channels, options={})
|
17
|
+
options = options.dup.merge(filepath: filepath, channels: channels)
|
18
|
+
FileUpload.new(options).upload
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'uri'
|
2
|
+
require 'httpi'
|
3
|
+
require 'forwardable'
|
4
|
+
require 'json'
|
5
|
+
|
6
|
+
module Layabout
|
7
|
+
class IncomingWebhook
|
8
|
+
extend Forwardable
|
9
|
+
|
10
|
+
def_delegators :@configuration, :domain, :team
|
11
|
+
|
12
|
+
attr_reader :token, :message
|
13
|
+
|
14
|
+
def initialize(options={})
|
15
|
+
@token = options.fetch(:token) # The token for Webhooks is not the same as your API token
|
16
|
+
@message = options.fetch(:message)
|
17
|
+
@configuration = ::Layabout.configuration
|
18
|
+
end
|
19
|
+
|
20
|
+
def post
|
21
|
+
http_request.body = {'text' => message}.to_json.to_s
|
22
|
+
|
23
|
+
SlackResponse.new(HTTPI.post(http_request))
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def http_request
|
29
|
+
@http_request ||= HTTPI::Request.new(endpoint.to_s).tap do |httpi|
|
30
|
+
httpi.auth.ssl.verify_mode = :peer
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def endpoint
|
35
|
+
domain.tap do |uri|
|
36
|
+
uri.path = '/services/hooks/incoming-webhook'
|
37
|
+
uri.query = "token=#{token}"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'forwardable'
|
2
|
+
require 'ostruct'
|
3
|
+
|
4
|
+
module Layabout
|
5
|
+
module Slack
|
6
|
+
class Channel
|
7
|
+
extend Forwardable
|
8
|
+
|
9
|
+
def_delegators :@attributes, :id,
|
10
|
+
:name,
|
11
|
+
:created,
|
12
|
+
:creator,
|
13
|
+
:is_archived,
|
14
|
+
:is_member,
|
15
|
+
:num_members,
|
16
|
+
:topic,
|
17
|
+
:purpose,
|
18
|
+
:members,
|
19
|
+
:last_read,
|
20
|
+
:latest,
|
21
|
+
:unread_count
|
22
|
+
|
23
|
+
def initialize(attributes={})
|
24
|
+
@attributes = OpenStruct.new(attributes)
|
25
|
+
end
|
26
|
+
|
27
|
+
def inspect
|
28
|
+
"<Layabout::Slack::Channel id='#{id}' name='#{name}'>"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'forwardable'
|
2
|
+
require 'ostruct'
|
3
|
+
|
4
|
+
module Layabout
|
5
|
+
module Slack
|
6
|
+
class File
|
7
|
+
extend Forwardable
|
8
|
+
|
9
|
+
def_delegators :@attributes,
|
10
|
+
:id,
|
11
|
+
:timestamp,
|
12
|
+
:name,
|
13
|
+
:title,
|
14
|
+
:mimetype,
|
15
|
+
:filetype,
|
16
|
+
:pretty_type,
|
17
|
+
:user,
|
18
|
+
:mode,
|
19
|
+
:editable,
|
20
|
+
:is_external,
|
21
|
+
:external_type,
|
22
|
+
:size,
|
23
|
+
:url,
|
24
|
+
:url_download,
|
25
|
+
:url_private,
|
26
|
+
:url_private_download,
|
27
|
+
:thumb_64,
|
28
|
+
:thumb_80,
|
29
|
+
:thumb_360,
|
30
|
+
:thumb_360_gif,
|
31
|
+
:thumb_360_w,
|
32
|
+
:thumb_360_h,
|
33
|
+
:permalink,
|
34
|
+
:edit_link,
|
35
|
+
:preview,
|
36
|
+
:preview_highlight,
|
37
|
+
:lines,
|
38
|
+
:lines_more,
|
39
|
+
:is_public,
|
40
|
+
:public_url_shared,
|
41
|
+
:channels,
|
42
|
+
:groups,
|
43
|
+
:initial_comment,
|
44
|
+
:num_stars,
|
45
|
+
:is_starred
|
46
|
+
|
47
|
+
def initialize(attributes={})
|
48
|
+
@attributes = OpenStruct.new(attributes)
|
49
|
+
end
|
50
|
+
|
51
|
+
def inspect
|
52
|
+
"<Layabout::Slack::File id='#{id}' name='#{name}'>"
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|