async-slack 0.1.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 315657ffe3859a70c177fcbda5cfd167d8d196de733b8a7120a45e005c0fbd5a
4
+ data.tar.gz: 80201b5872df1dff9d5c8cfa6634ad81ec2be3f10d03361df919de13ddd9d59d
5
+ SHA512:
6
+ metadata.gz: 3c780e82b86cf2af7bf9d01357abcf664fffcd8d83c5222aee2fa7defe259fdffe984d6513d7158ba5129710d7b44f56ee1e64a20d3725b4e6186fe0818c5c87
7
+ data.tar.gz: 1da87de2bdf291fe4983f5be6234609037aa59a5b43e653bc18efb82b45d5765053a252d2539600785fb22a54830e20a1363b0e0aac727c3e9f06917a4cf54bf
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --require spec_helper
@@ -0,0 +1,21 @@
1
+ language: ruby
2
+ dist: xenial
3
+ cache: bundler
4
+ matrix:
5
+ include:
6
+ - rvm: 2.4
7
+ - rvm: 2.5
8
+ - rvm: 2.6
9
+ - rvm: 2.6
10
+ env: COVERAGE=PartialSummary,Coveralls
11
+ - rvm: truffleruby
12
+ - rvm: jruby-head
13
+ env: JRUBY_OPTS="--debug -X+O"
14
+ - rvm: ruby-head
15
+ allow_failures:
16
+ - rvm: truffleruby
17
+ - rvm: ruby-head
18
+ - rvm: jruby-head
19
+ env:
20
+ global:
21
+ secure: RZDLmQ8sPcznMMePAEunRfHflB0+uWipoyP7ylb+F/yrn7iHAfzS5e33ADsXQKwPRmALagZ9HVe5RYUX2VzEMehCPholdyoOkT3xNOfIrV6hJlHYLcu7+PSJgHk9WStKklZqhcnS9HaePwXc93s8Hk1R5qnovDrP62syXMci7MVbwg/61yROx0FNOyTpGqyCe/So6VdVA2mCeldSFS309O6AEKofR7AEJoPIDIPpLH6cftYtYPi6RxEzHd6iD/WA4l5T9dbSGLfwghaO8SKFc7M/eTUx16U4Qyl8kze1aB9x/0xG6tmhTHSdDUrrgToLgRCRHTfFbW/70IXRZMzTjpjwFFaQKV01kbhqIMll7flh1mPCH8J96e+tOyJHFlHryM5Z8q68Vd3lVAYX+B3aJyeqtDLkQcGgDE5zlXComIDcrwhgoCWgXVz/Bmzu9H40X6fQjxFF/ZKcEG25+supYfR0ohi5FEvHXlEWlnH8INoeJMqDZDr8lgBy0iVVr5tHGLr6CU9hIrEYqea8CcbPKaT8hT6q5vS9Jwo52gYPbVwvNnFCRLemkWQAHuZLpV3YiS8o6gHYnXjyENpf9yXu7raHI+GXvVoiHAD71nZbZlM2HTESekaLahV1F+hxO9QuEYFPAYJH0F5iPNK5ByUksM+8DGVh8MlldR7Ni3yGi0M=
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in async-slack.gemspec
4
+ gemspec
@@ -0,0 +1,82 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ async-slack (0.1.0)
5
+ async-rest
6
+ async-websocket
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ ast (2.4.0)
12
+ async (1.19.0)
13
+ console (~> 1.0)
14
+ nio4r (~> 2.3)
15
+ timers (~> 4.1)
16
+ async-http (0.45.8)
17
+ async (~> 1.19)
18
+ async-io (~> 1.18)
19
+ protocol-http (~> 0.8.0)
20
+ protocol-http1 (~> 0.8.0)
21
+ protocol-http2 (~> 0.8.0)
22
+ async-io (1.23.1)
23
+ async (~> 1.14)
24
+ async-rest (0.9.0)
25
+ async-http (~> 0.42)
26
+ protocol-http (~> 0.7)
27
+ async-rspec (1.12.2)
28
+ rspec (~> 3.0)
29
+ async-websocket (0.12.1)
30
+ async-http (~> 0.43)
31
+ async-io (~> 1.23)
32
+ protocol-websocket (~> 0.6.0)
33
+ console (1.3.2)
34
+ covered (0.13.1)
35
+ async-rest
36
+ console (~> 1.0)
37
+ msgpack
38
+ parser
39
+ diff-lcs (1.3)
40
+ msgpack (1.3.0)
41
+ nio4r (2.3.1)
42
+ parser (2.6.3.0)
43
+ ast (~> 2.4.0)
44
+ protocol-hpack (1.4.1)
45
+ protocol-http (0.8.1)
46
+ protocol-http1 (0.8.0)
47
+ protocol-http (~> 0.5)
48
+ protocol-http2 (0.8.2)
49
+ protocol-hpack (~> 1.4)
50
+ protocol-http (~> 0.2)
51
+ protocol-websocket (0.6.1)
52
+ protocol-http (~> 0.2)
53
+ protocol-http1 (~> 0.2)
54
+ rake (10.5.0)
55
+ rspec (3.8.0)
56
+ rspec-core (~> 3.8.0)
57
+ rspec-expectations (~> 3.8.0)
58
+ rspec-mocks (~> 3.8.0)
59
+ rspec-core (3.8.1)
60
+ rspec-support (~> 3.8.0)
61
+ rspec-expectations (3.8.4)
62
+ diff-lcs (>= 1.2.0, < 2.0)
63
+ rspec-support (~> 3.8.0)
64
+ rspec-mocks (3.8.1)
65
+ diff-lcs (>= 1.2.0, < 2.0)
66
+ rspec-support (~> 3.8.0)
67
+ rspec-support (3.8.2)
68
+ timers (4.3.0)
69
+
70
+ PLATFORMS
71
+ ruby
72
+
73
+ DEPENDENCIES
74
+ async-rspec
75
+ async-slack!
76
+ bundler
77
+ covered
78
+ rake (~> 10.0)
79
+ rspec (~> 3.0)
80
+
81
+ BUNDLED WITH
82
+ 2.0.1
@@ -0,0 +1,56 @@
1
+ # Async::Slack
2
+
3
+ A simple asynchronous slack client implementation.
4
+
5
+ [![Build Status](https://secure.travis-ci.org/socketry/async-slack.svg)](http://travis-ci.org/socketry/async-slack)
6
+ [![Coverage Status](https://coveralls.io/repos/socketry/async-slack/badge.svg)](https://coveralls.io/r/socketry/async-slack)
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'async-slack'
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install async-slack
21
+
22
+ ## Usage
23
+
24
+ ...
25
+
26
+ ## Contributing
27
+
28
+ 1. Fork it
29
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
30
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
31
+ 4. Push to the branch (`git push origin my-new-feature`)
32
+ 5. Create new Pull Request
33
+
34
+ ## License
35
+
36
+ Released under the MIT license.
37
+
38
+ Copyright, 2019, by [Samuel G. D. Williams](http://www.codeotaku.com/samuel-williams).
39
+
40
+ Permission is hereby granted, free of charge, to any person obtaining a copy
41
+ of this software and associated documentation files (the "Software"), to deal
42
+ in the Software without restriction, including without limitation the rights
43
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
44
+ copies of the Software, and to permit persons to whom the Software is
45
+ furnished to do so, subject to the following conditions:
46
+
47
+ The above copyright notice and this permission notice shall be included in
48
+ all copies or substantial portions of the Software.
49
+
50
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
51
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
52
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
53
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
54
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
55
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
56
+ THE SOFTWARE.
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,28 @@
1
+
2
+ require_relative 'lib/async/slack/version'
3
+
4
+ Gem::Specification.new do |spec|
5
+ spec.name = "async-slack"
6
+ spec.version = Async::Slack::VERSION
7
+ spec.authors = ["Samuel Williams"]
8
+ spec.email = ["samuel.williams@oriontransfer.co.nz"]
9
+
10
+ spec.summary = "Build Slack bots and use real time messaging."
11
+ spec.homepage = "https://github.com/socketry/async-slack"
12
+ spec.license = "MIT"
13
+
14
+ spec.files = `git ls-files -z`.split("\x0")
15
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
16
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
17
+ spec.require_paths = ["lib"]
18
+
19
+ spec.add_dependency "async-rest"
20
+ spec.add_dependency "async-websocket"
21
+
22
+ spec.add_development_dependency "async-rspec"
23
+
24
+ spec.add_development_dependency "covered"
25
+ spec.add_development_dependency "bundler"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec", "~> 3.0"
28
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literals: true
2
+ #
3
+ # Copyright, 2019, by Samuel G. D. Williams. <http://www.codeotaku.com>
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.
22
+
23
+ require_relative 'slack/client'
24
+ require_relative 'slack/version'
25
+
26
+ module Async
27
+ module Slack
28
+ DEFAULT_URL = "https://slack.com/api"
29
+
30
+ def self.connect(url = DEFAULT_URL, token: nil)
31
+ representation = Client.for(DEFAULT_URL)
32
+
33
+ if token
34
+ representation = representation.authenticated(token)
35
+ end
36
+
37
+ return representation unless block_given?
38
+
39
+ Async do
40
+ begin
41
+ yield representation
42
+ ensure
43
+ representation.close
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,93 @@
1
+ # frozen_string_literals: true
2
+ #
3
+ # Copyright, 2019, by Samuel G. D. Williams. <http://www.codeotaku.com>
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.
22
+
23
+ require_relative 'representation'
24
+
25
+ module Async
26
+ module Slack
27
+ class Message < Representation
28
+ def channel
29
+ value[:channel]
30
+ end
31
+
32
+ def text
33
+ value[:text]
34
+ end
35
+
36
+ def text= text
37
+ value[:text] = text
38
+ end
39
+
40
+ def timestamp
41
+ value[:ts]
42
+ end
43
+
44
+ def username
45
+ value[:username]
46
+ end
47
+
48
+ def call(value)
49
+ if value.nil?
50
+ self.delete if self.timestamp
51
+ elsif value.key?(:ts)
52
+ self.with(path: 'chat.update').post(value)
53
+ else
54
+ self.with(path: 'chat.postMessage').post(value)
55
+ end
56
+ end
57
+
58
+ def assign(value)
59
+ response = self.call(value)
60
+
61
+ if body = response.read
62
+ channel = body[:channel]
63
+
64
+ if message = body[:message]
65
+ message[:channel] ||= channel
66
+
67
+ return message
68
+ end
69
+ end
70
+
71
+ return value
72
+ end
73
+
74
+ def delete
75
+ self.with(path: 'chat.delete').post({ts: self.timestamp, channel: self.channel})
76
+ end
77
+
78
+ def inspect
79
+ "\#<#{self.class} timestamp=#{self.timestamp} text=#{self.text.inspect}>"
80
+ end
81
+ end
82
+
83
+ class Chat < Representation
84
+ def send_message(channel:, **payload)
85
+ message = self.with(Message, parameters: {channel: channel})
86
+
87
+ message.value = payload
88
+
89
+ return message
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literals: true
2
+ #
3
+ # Copyright, 2019, by Samuel G. D. Williams. <http://www.codeotaku.com>
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.
22
+
23
+ require_relative 'representation'
24
+
25
+ require_relative 'real_time'
26
+ require_relative 'chat'
27
+ require_relative 'search'
28
+
29
+ module Async
30
+ module Slack
31
+ class Client < Representation
32
+ def authenticated(token)
33
+ self.with(parameters: {token: token})
34
+ end
35
+
36
+ def real_time
37
+ self.with(RealTime, path: "rtm.connect")
38
+ end
39
+
40
+ def chat(**parameters)
41
+ self.with(Chat, parameters: parameters)
42
+ end
43
+
44
+ def search(**parameters)
45
+ self.with(Search, parameters: parameters)
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literals: true
2
+ #
3
+ # Copyright, 2019, by Samuel G. D. Williams. <http://www.codeotaku.com>
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.
22
+
23
+ require "async/websocket/client"
24
+ require "async/websocket/response"
25
+
26
+ require_relative 'representation'
27
+
28
+ module Async
29
+ module Slack
30
+ class RealTime < Representation
31
+ def connect(&block)
32
+ response = self.post
33
+
34
+ parameters = response.read
35
+ url = parameters[:url]
36
+
37
+ endpoint = Async::HTTP::Endpoint.parse(url)
38
+
39
+ Async::WebSocket::Client.connect(endpoint, &block)
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,67 @@
1
+ # frozen_string_literals: true
2
+ #
3
+ # Copyright, 2019, by Samuel G. D. Williams. <http://www.codeotaku.com>
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.
22
+
23
+ require 'async/rest/representation'
24
+ require 'async/rest/wrapper/json'
25
+ require 'async/rest/wrapper/url_encoded'
26
+
27
+ module Async
28
+ module Slack
29
+ class Wrapper < Async::REST::Wrapper::JSON
30
+ def prepare_request(payload, headers)
31
+ super(nil, headers)
32
+
33
+ if payload
34
+ headers['content-type'] = Async::REST::Wrapper::URLEncoded::APPLICATION_FORM_URLENCODED
35
+
36
+ ::Protocol::HTTP::Body::Buffered.new([
37
+ ::Protocol::HTTP::URL.encode(payload)
38
+ ])
39
+ end
40
+ end
41
+
42
+ class Parser < HTTP::Body::Wrapper
43
+ def join
44
+ body = ::JSON.parse(super, symbolize_names: true)
45
+
46
+ if error = body[:error]
47
+ raise REST::Error, error
48
+ end
49
+
50
+ return body
51
+ end
52
+ end
53
+
54
+ def wrap_response(response)
55
+ if body = response.body
56
+ response.body = Parser.new(body)
57
+ end
58
+ end
59
+ end
60
+
61
+ class Representation < Async::REST::Representation
62
+ def initialize(*args, **options)
63
+ super(*args, wrapper: Wrapper.new, **options)
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright, 2017, by Samuel G. D. Williams. <http://www.codeotaku.com>
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.
22
+
23
+ require 'async/rspec'
24
+ require 'rspec'
25
+
26
+ require_relative '../../slack'
27
+
28
+ module Async
29
+ module Slack
30
+ module RSpec
31
+ module Client
32
+ end
33
+
34
+ ::RSpec.shared_context Client do
35
+ include_context Async::RSpec::Reactor
36
+
37
+ # You must specify these in order for the tests to run.
38
+ let(:token) {ENV['SLACK_TOKEN']}
39
+ let(:channel) {'#testing'}
40
+
41
+ let(:connection) {@connection = Async::Slack.connect(token: token)}
42
+
43
+ after do
44
+ @connection&.close
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,92 @@
1
+ # frozen_string_literals: true
2
+ #
3
+ # Copyright, 2019, by Samuel G. D. Williams. <http://www.codeotaku.com>
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.
22
+
23
+ require_relative 'representation'
24
+
25
+ module Async
26
+ module Slack
27
+ class Pagination < Representation
28
+ include Enumerable
29
+
30
+ def each(page: 1, count: 100, **parameters)
31
+ return to_enum(:each, page: page, count: count, **parameters) unless block_given?
32
+
33
+ collection = self.collection(**parameters)
34
+
35
+ while true
36
+ representation = collection.get(self.class, page: page, count: count)
37
+
38
+ records = representation.records
39
+
40
+ records.each do |value|
41
+ yield represent(representation.metadata, value)
42
+ end
43
+
44
+ # Was this the last page?
45
+ break if records.count < count
46
+
47
+ page += 1
48
+ end
49
+ end
50
+
51
+ def empty?
52
+ self.value.empty?
53
+ end
54
+ end
55
+
56
+ class Messages < Pagination
57
+ class Message < Message
58
+ def channel
59
+ value[:channel][:id]
60
+ end
61
+ end
62
+
63
+ def collection(**parameters)
64
+ @resource.with(path: 'search.messages', parameters: parameters)
65
+ end
66
+
67
+ def representation
68
+ Message
69
+ end
70
+
71
+ def records
72
+ self.value[:messages][:matches]
73
+ end
74
+
75
+ def represent(metadata, attributes)
76
+ representation.new(@resource, metadata: metadata, value: attributes)
77
+ end
78
+ end
79
+
80
+ class Search < Representation
81
+ def messages(**parameters, &block)
82
+ messages = self.with(Messages, parameters: parameters)
83
+
84
+ if block_given?
85
+ messages.each(&block)
86
+ else
87
+ return messages
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,6 @@
1
+
2
+ module Async
3
+ module Slack
4
+ VERSION = "0.1.0"
5
+ end
6
+ end
@@ -0,0 +1,35 @@
1
+ # Copyright, 2019, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ require "async/slack/rspec/client"
22
+
23
+ RSpec.describe Async::Slack::RealTime do
24
+ include_context Async::Slack::RSpec::Client
25
+
26
+ let(:channel) {"CKF0A81D1"}
27
+
28
+ it "can connect to websocket endpoint" do
29
+ Async::Slack.connect(token: token) do |client|
30
+ client.real_time.connect do |connection|
31
+ expect(connection.read).to be == {:type=>"hello"}
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,42 @@
1
+ # Copyright, 2019, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ require "async/slack/rspec/client"
22
+
23
+ RSpec.describe Async::Slack::Client do
24
+ include_context Async::Slack::RSpec::Client
25
+
26
+ it "can send and receive messages" do
27
+ Async::Slack.connect(token: token) do |client|
28
+ 5.times do
29
+ message = client.chat.send_message(channel: channel, text: "The time is #{Time.now}")
30
+ message.value = nil
31
+ end
32
+ end
33
+ end
34
+
35
+ it "can search for messages" do
36
+ Async::Slack.connect(token: token) do |client|
37
+ messages = client.search.messages(query: "Hello World").to_a
38
+
39
+ expect(messages).to_not be_empty
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,13 @@
1
+
2
+ require 'covered/rspec'
3
+
4
+ RSpec.configure do |config|
5
+ config.disable_monkey_patching!
6
+
7
+ # Enable flags like --only-failures and --next-failure
8
+ config.example_status_persistence_file_path = ".rspec_status"
9
+
10
+ config.expect_with :rspec do |c|
11
+ c.syntax = :expect
12
+ end
13
+ end
metadata ADDED
@@ -0,0 +1,163 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: async-slack
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Samuel Williams
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-06-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: async-rest
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: async-websocket
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: async-rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: covered
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
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: bundler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
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: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '10.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '10.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
+ description:
112
+ email:
113
+ - samuel.williams@oriontransfer.co.nz
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - ".travis.yml"
121
+ - Gemfile
122
+ - Gemfile.lock
123
+ - README.md
124
+ - Rakefile
125
+ - async-slack.gemspec
126
+ - lib/async/slack.rb
127
+ - lib/async/slack/chat.rb
128
+ - lib/async/slack/client.rb
129
+ - lib/async/slack/real_time.rb
130
+ - lib/async/slack/representation.rb
131
+ - lib/async/slack/rspec/client.rb
132
+ - lib/async/slack/search.rb
133
+ - lib/async/slack/version.rb
134
+ - spec/async/slack/real_time_spec.rb
135
+ - spec/async/slack_spec.rb
136
+ - spec/spec_helper.rb
137
+ homepage: https://github.com/socketry/async-slack
138
+ licenses:
139
+ - MIT
140
+ metadata: {}
141
+ post_install_message:
142
+ rdoc_options: []
143
+ require_paths:
144
+ - lib
145
+ required_ruby_version: !ruby/object:Gem::Requirement
146
+ requirements:
147
+ - - ">="
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ required_rubygems_version: !ruby/object:Gem::Requirement
151
+ requirements:
152
+ - - ">="
153
+ - !ruby/object:Gem::Version
154
+ version: '0'
155
+ requirements: []
156
+ rubygems_version: 3.0.3
157
+ signing_key:
158
+ specification_version: 4
159
+ summary: Build Slack bots and use real time messaging.
160
+ test_files:
161
+ - spec/async/slack/real_time_spec.rb
162
+ - spec/async/slack_spec.rb
163
+ - spec/spec_helper.rb