mattermost-api4-ruby 0.0.3 → 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/lib/mattermost/client.rb +12 -0
- data/lib/mattermost/websocket_client.rb +94 -0
- data/mattermost-api4-ruby.gemspec +2 -1
- metadata +17 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 19d64e50d31233c3c823ca05b2bde709d004e2b4
|
4
|
+
data.tar.gz: 8ecbb00dff5bfcdbdebeca57d9d56b294e7f7076
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7d85a810c0a898da5c6fb26f59b48e59cf32603f52f867310394799456ae78ffdd87e16569242fcafeb3b63e414d0511a3377144ec2ebf8ffe3145b80dc6c930
|
7
|
+
data.tar.gz: 5fd7a8abae791675cb01a738d5c89897ef8685fc9b649ec5494345b651d79c6ed6f496a40637333d1bb276fa05fac4235e57c2040182f0a7e6a5aa58a0b7ac58
|
data/lib/mattermost/client.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require 'httparty'
|
2
2
|
require_relative 'endpoint'
|
3
3
|
require_relative 'request'
|
4
|
+
require_relative 'websocket_client'
|
4
5
|
|
5
6
|
module Mattermost
|
6
7
|
|
@@ -36,6 +37,17 @@ module Mattermost
|
|
36
37
|
getMe().success?
|
37
38
|
end
|
38
39
|
|
40
|
+
def connect_websocket
|
41
|
+
# TODO raise exception then connected? == false
|
42
|
+
@ws_client = WebSocketClient.new "#{base_uri}/websocket", token, {:headers => self.class.headers}
|
43
|
+
yield @ws_client if block_given?
|
44
|
+
@ws_client
|
45
|
+
end
|
46
|
+
|
47
|
+
def ws_client
|
48
|
+
@ws_client
|
49
|
+
end
|
50
|
+
|
39
51
|
def get(path, options = {}, &block)
|
40
52
|
self.class.get(path, options, &block)
|
41
53
|
end
|
@@ -0,0 +1,94 @@
|
|
1
|
+
require 'websocket-client-simple'
|
2
|
+
require 'event_emitter'
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
module Mattermost
|
6
|
+
class WebSocketClient
|
7
|
+
include EventEmitter
|
8
|
+
|
9
|
+
def initialize(url, token, option = {})
|
10
|
+
@token = token
|
11
|
+
@url = url
|
12
|
+
@seq_mutex = Mutex.new
|
13
|
+
@seq = 0
|
14
|
+
@connected = false
|
15
|
+
mm_ws = self
|
16
|
+
@client = WebSocket::Client::Simple.connect(url, option) do |ws|
|
17
|
+
ws.on :open do
|
18
|
+
mm_ws.on_open
|
19
|
+
end
|
20
|
+
ws.on :message do |msg|
|
21
|
+
mm_ws.on_message msg.data
|
22
|
+
end
|
23
|
+
ws.on :close do |e|
|
24
|
+
mm_ws.on_close e
|
25
|
+
end
|
26
|
+
ws.on :error do |e|
|
27
|
+
mm_ws.on_error e
|
28
|
+
end
|
29
|
+
end
|
30
|
+
yield self if block_given?
|
31
|
+
end
|
32
|
+
|
33
|
+
def on_open
|
34
|
+
emit :open
|
35
|
+
end
|
36
|
+
|
37
|
+
def on_message(data)
|
38
|
+
json = JSON.parse data
|
39
|
+
event = json["event"]
|
40
|
+
#puts "on_message json:#{json}, event:#{event}"
|
41
|
+
seq_up json
|
42
|
+
case event
|
43
|
+
when "hello"
|
44
|
+
@connected = true
|
45
|
+
else
|
46
|
+
emit event.to_sym, json
|
47
|
+
emit :message, json
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def on_close(msg)
|
52
|
+
@connected = false
|
53
|
+
emit :close, msg
|
54
|
+
end
|
55
|
+
|
56
|
+
def on_error(err)
|
57
|
+
emit :error, err
|
58
|
+
end
|
59
|
+
|
60
|
+
def send_msg(action, data)
|
61
|
+
payload = {
|
62
|
+
:seq => next_seq,
|
63
|
+
:action => action,
|
64
|
+
:data => data
|
65
|
+
}.to_json
|
66
|
+
@client.send payload
|
67
|
+
end
|
68
|
+
|
69
|
+
def connected?
|
70
|
+
@connected && @client.connected?
|
71
|
+
end
|
72
|
+
|
73
|
+
def close
|
74
|
+
@client.close
|
75
|
+
end
|
76
|
+
|
77
|
+
private
|
78
|
+
|
79
|
+
def seq_up(event)
|
80
|
+
@seq_mutex.synchronize do
|
81
|
+
return if !event.key? "seq"
|
82
|
+
new_seq = event["seq"]
|
83
|
+
@seq = new_seq if @seq < new_seq
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
def next_seq
|
88
|
+
@seq_mutex.synchronize do
|
89
|
+
@seq = @seq + 1
|
90
|
+
@seq
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |spec|
|
4
4
|
spec.name = "mattermost-api4-ruby"
|
5
|
-
spec.version = "0.0.
|
5
|
+
spec.version = "0.0.4"
|
6
6
|
spec.authors = ["Takayuki Maruyama"]
|
7
7
|
spec.email = ["bis5.wsys@gmail.com"]
|
8
8
|
|
@@ -22,5 +22,6 @@ Gem::Specification.new do |spec|
|
|
22
22
|
spec.add_development_dependency "rspec", "~> 3.0"
|
23
23
|
|
24
24
|
spec.add_dependency "httparty", "~> 0.15"
|
25
|
+
spec.add_dependency "websocket-client-simple", "~> 0.3"
|
25
26
|
|
26
27
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mattermost-api4-ruby
|
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
|
- Takayuki Maruyama
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-01-
|
11
|
+
date: 2018-01-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -66,6 +66,20 @@ dependencies:
|
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0.15'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: websocket-client-simple
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0.3'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0.3'
|
69
83
|
description:
|
70
84
|
email:
|
71
85
|
- bis5.wsys@gmail.com
|
@@ -109,6 +123,7 @@ files:
|
|
109
123
|
- lib/mattermost/endpoint/users.rb
|
110
124
|
- lib/mattermost/endpoint/webhooks.rb
|
111
125
|
- lib/mattermost/request.rb
|
126
|
+
- lib/mattermost/websocket_client.rb
|
112
127
|
- mattermost-api4-ruby.gemspec
|
113
128
|
homepage: https://github.com/maruTA-bis5/mattermost-api4-ruby
|
114
129
|
licenses:
|