websockethook 0.1.03 → 0.2.01
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/websockethook.rb +38 -78
- data/spec/websockethook_spec.rb +27 -25
- metadata +2 -17
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 345a7a56cda55cd3db3bd9b0b26599d2ee5f5b56
|
4
|
+
data.tar.gz: 2061804507bc06c370df8288fdbce5b34c5a4d10
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ec81675cce79fb343906c0493a50e21396f672ddceffeba9d904e1a2310a832972271d7f53eed69dcef0a3577238ba7490e58427579ea1c1916a8033d08f831b
|
7
|
+
data.tar.gz: 24d0253c0549891b91619e44ce441dcd78cae0a41ce47d6adaec7d423bca272243b44cc965c5d4cd4b74a2549639772bfad93d103ea30d456e0024816e1edc3e
|
data/lib/websockethook.rb
CHANGED
@@ -1,9 +1,10 @@
|
|
1
|
-
require '
|
1
|
+
require 'websocket-client-simple'
|
2
2
|
require 'json'
|
3
|
-
require 'eventmachine'
|
4
3
|
|
5
4
|
class WebSocketHook
|
6
|
-
|
5
|
+
attr_reader :host
|
6
|
+
|
7
|
+
DEFAULT_HOST = 'wss://websockethook.io'
|
7
8
|
DEFAULT_SLEEP = 0.1
|
8
9
|
DEFAULT_KEEP_ALIVE = true
|
9
10
|
DEFAULT_PING = 20
|
@@ -11,97 +12,56 @@ class WebSocketHook
|
|
11
12
|
def initialize(options = {})
|
12
13
|
@stopping = false
|
13
14
|
initialize_host options
|
14
|
-
|
15
|
-
initialize_keep_alive options
|
16
|
-
initialize_ping options
|
15
|
+
# initialize_keep_alive options
|
17
16
|
end
|
18
17
|
|
19
|
-
def listen(id=nil, &
|
20
|
-
|
21
|
-
begin
|
22
|
-
@stopping = false
|
23
|
-
listener(id, &block)
|
24
|
-
restart = @keep_alive && !@stopping
|
25
|
-
if restart
|
26
|
-
block.call type: 'restart', message: 'restarting connection since it was lost'
|
27
|
-
sleep 5
|
28
|
-
end
|
29
|
-
end while restart
|
30
|
-
end
|
18
|
+
def listen(id=nil, &callback)
|
19
|
+
@ws = WebSocket::Client::Simple.connect(@host)
|
31
20
|
|
32
|
-
|
33
|
-
@
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
private
|
38
|
-
|
39
|
-
def initialize_host(options = {})
|
40
|
-
@host = options[:host] || DEFAULT_HOST
|
41
|
-
fail 'Host (:host) must be a URL' unless @host.is_a?(String)
|
42
|
-
end
|
21
|
+
this = self
|
22
|
+
@ws.on(:open) { this.on_open(id, &callback) }
|
23
|
+
@ws.on(:message) {|message| this.on_message(message, &callback) }
|
24
|
+
@ws.on(:close) { callback.call :closed }
|
25
|
+
@ws.on(:error) { |er| callback.call :error, er }
|
43
26
|
|
44
|
-
|
45
|
-
@pause = options[:sleep] || DEFAULT_SLEEP
|
46
|
-
fail 'Pause (:pause) must be a float or integer' unless @pause.is_a?(Float) || @pause.is_a?(Integer)
|
27
|
+
block(&callback)
|
47
28
|
end
|
48
29
|
|
49
|
-
def
|
50
|
-
|
51
|
-
|
52
|
-
end
|
53
|
-
|
54
|
-
def initialize_ping(options = {})
|
55
|
-
@ping = options[:ping] || DEFAULT_PING
|
56
|
-
fail 'Ping (:ping) must be an integer' unless @ping.is_a?(Integer)
|
30
|
+
def stop
|
31
|
+
unblock
|
32
|
+
@ws.close
|
57
33
|
end
|
58
34
|
|
59
|
-
def
|
60
|
-
|
61
|
-
|
35
|
+
def on_open(id=nil, &callback)
|
36
|
+
callback.call :open
|
37
|
+
@ws.send({type:'register', id: id}.to_json) if id
|
62
38
|
end
|
63
39
|
|
64
|
-
def
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
end
|
70
|
-
ws.on :message do |message|
|
71
|
-
data = nil
|
72
|
-
begin
|
73
|
-
data = JSON.parse(message.data, symbolize_names: true)
|
74
|
-
rescue => ex
|
75
|
-
block.call type: 'error', message: "Message received, but couldn't parse: #{ex.message}"
|
40
|
+
def on_message(message, &callback)
|
41
|
+
if message && message.data
|
42
|
+
data = JSON.parse(message.data)
|
43
|
+
if data['type'] == 'registered'
|
44
|
+
callback.call :registered, data['data']
|
76
45
|
end
|
77
|
-
|
78
|
-
data[:data][:url] = "#{@host.sub('ws://','http://')}#{data[:data][:path]}" if data[:type]=='registered' && data[:data] && data[:data][:path]
|
79
|
-
block.call(content) if data && block
|
46
|
+
callback.call :hook, {'id'=>data['id'], 'data' => data['data']} if data['type'] == 'hook'
|
80
47
|
end
|
48
|
+
end
|
81
49
|
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
end
|
50
|
+
def block(&callback)
|
51
|
+
begin
|
52
|
+
sleep 0.1
|
53
|
+
end while !@stopping
|
54
|
+
callback.call :stopped
|
55
|
+
@stopping = false
|
86
56
|
end
|
87
57
|
|
88
|
-
def
|
89
|
-
|
90
|
-
ws_settings = { ping: @ping }
|
91
|
-
ws = Faye::WebSocket::Client.new(@host, nil, ws_settings)
|
92
|
-
socket_block.call(ws)
|
93
|
-
end
|
94
|
-
callback_block.call(type: 'stopped')
|
95
|
-
rescue => ex
|
96
|
-
callback_block.call type: 'error', message: ex.message
|
97
|
-
rescue Interrupt
|
98
|
-
stop
|
99
|
-
callback_block.call(type: 'stopped')
|
58
|
+
def unblock
|
59
|
+
@stopping = true
|
100
60
|
end
|
101
61
|
|
102
|
-
def
|
103
|
-
|
104
|
-
|
105
|
-
end
|
62
|
+
def initialize_host(options = {})
|
63
|
+
@host = options[:host] || DEFAULT_HOST
|
64
|
+
fail 'Host (:host) must be a URL' unless @host.is_a?(String)
|
106
65
|
end
|
66
|
+
|
107
67
|
end
|
data/spec/websockethook_spec.rb
CHANGED
@@ -10,49 +10,51 @@ describe WebSocketHook do
|
|
10
10
|
end
|
11
11
|
|
12
12
|
it 'can open and close' do
|
13
|
-
expect(@logger).to receive(:log).with(
|
14
|
-
expect(@logger).to receive(:log).with(
|
15
|
-
expect(@logger).to receive(:log).with(
|
13
|
+
expect(@logger).to receive(:log).with(:open)
|
14
|
+
expect(@logger).to receive(:log).with(:closed)
|
15
|
+
expect(@logger).to receive(:log).with(:stopped)
|
16
16
|
|
17
|
-
@ws.listen
|
17
|
+
@ws.listen do |msg|
|
18
18
|
@logger.log(msg)
|
19
19
|
@ws.stop
|
20
20
|
end
|
21
|
-
|
22
21
|
end
|
23
22
|
|
24
23
|
it 'can trigger a hook with data' do
|
25
|
-
expect(@logger).to receive(:log).with(
|
26
|
-
expect(@logger).to receive(:log).with(
|
27
|
-
expect(@logger).to receive(:log).with(
|
28
|
-
expect(@logger).to receive(:log).with(
|
29
|
-
expect(@logger).to receive(:log).with(
|
30
|
-
|
31
|
-
@ws.listen do |msg|
|
32
|
-
@logger.log(msg)
|
24
|
+
expect(@logger).to receive(:log).with(:open,nil)
|
25
|
+
expect(@logger).to receive(:log).with(:registered, {'id'=>anything(),'path'=>anything()})
|
26
|
+
expect(@logger).to receive(:log).with(:hook, {'id'=> anything(), 'data' => {'this_is_a' => 'test'}})
|
27
|
+
expect(@logger).to receive(:log).with(:closed,nil)
|
28
|
+
expect(@logger).to receive(:log).with(:stopped,nil)
|
29
|
+
|
30
|
+
@ws.listen do |type,msg|
|
31
|
+
@logger.log(type,msg)
|
33
32
|
|
34
|
-
if
|
35
|
-
|
36
|
-
|
33
|
+
if type == :registered
|
34
|
+
host = @ws.host.sub(/^ws/,'http')
|
35
|
+
url = "#{host}#{msg['path']}"
|
36
|
+
RestClient.post(url,this_is_a:'test')
|
37
37
|
end
|
38
38
|
|
39
|
-
|
39
|
+
if type == :hook
|
40
|
+
@ws.stop
|
41
|
+
end
|
40
42
|
end
|
41
43
|
end
|
42
44
|
|
43
45
|
it 'can register a custom id' do
|
44
46
|
id = "test_#{SecureRandom.hex(4)}"
|
45
47
|
|
46
|
-
expect(@logger).to receive(:log).with(
|
47
|
-
expect(@logger).to receive(:log).with(
|
48
|
-
expect(@logger).to receive(:log).with(
|
49
|
-
expect(@logger).to receive(:log).with(
|
50
|
-
expect(@logger).to receive(:log).with(
|
48
|
+
expect(@logger).to receive(:log).with(:open, nil)
|
49
|
+
expect(@logger).to receive(:log).with(:registered, {'id'=>anything(),'path'=>anything()})
|
50
|
+
expect(@logger).to receive(:log).with(:registered, {'id'=>id, 'path'=>"/hook/#{id}"})
|
51
|
+
expect(@logger).to receive(:log).with(:closed, nil)
|
52
|
+
expect(@logger).to receive(:log).with(:stopped, nil)
|
51
53
|
|
52
|
-
@ws.listen id do |msg|
|
53
|
-
@logger.log(msg)
|
54
|
+
@ws.listen id do |type,msg|
|
55
|
+
@logger.log(type,msg)
|
54
56
|
|
55
|
-
if
|
57
|
+
if type == :registered && msg['id'] == id
|
56
58
|
@ws.stop
|
57
59
|
end
|
58
60
|
end
|
metadata
CHANGED
@@ -1,29 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: websockethook
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.01
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Maciej Skierkowski
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-05-
|
11
|
+
date: 2016-05-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
-
- !ruby/object:Gem::Dependency
|
14
|
-
name: faye-websocket
|
15
|
-
requirement: !ruby/object:Gem::Requirement
|
16
|
-
requirements:
|
17
|
-
- - "~>"
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: 0.10.3
|
20
|
-
type: :runtime
|
21
|
-
prerelease: false
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
23
|
-
requirements:
|
24
|
-
- - "~>"
|
25
|
-
- !ruby/object:Gem::Version
|
26
|
-
version: 0.10.3
|
27
13
|
- !ruby/object:Gem::Dependency
|
28
14
|
name: rspec
|
29
15
|
requirement: !ruby/object:Gem::Requirement
|
@@ -117,4 +103,3 @@ summary: A library for use the free web.sockethook.io service
|
|
117
103
|
test_files:
|
118
104
|
- "./spec/spec_helper.rb"
|
119
105
|
- "./spec/websockethook_spec.rb"
|
120
|
-
has_rdoc:
|