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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f12e791fa2429ff43a0fe5834535487685cb8ee7
4
- data.tar.gz: 8ab40acc4243e3709e58949a9afe5b3030c7bd58
3
+ metadata.gz: 345a7a56cda55cd3db3bd9b0b26599d2ee5f5b56
4
+ data.tar.gz: 2061804507bc06c370df8288fdbce5b34c5a4d10
5
5
  SHA512:
6
- metadata.gz: 9bdffa58da91db7c2c0857879466dda153b736518b19e0c6ef76f6e04f50ecf299c3a877a5451382a572a23235ddfb4b082b56e53b37c584c95f2546a686d33f
7
- data.tar.gz: c0387e1e207b2b01b6cdac83398c41e3c9db94f37b2924a6e83f9a0ab878e559ee316c7dd186fba9946cd01c16686ebac86338df2089df2acc5ae4347d7d23ad
6
+ metadata.gz: ec81675cce79fb343906c0493a50e21396f672ddceffeba9d904e1a2310a832972271d7f53eed69dcef0a3577238ba7490e58427579ea1c1916a8033d08f831b
7
+ data.tar.gz: 24d0253c0549891b91619e44ce441dcd78cae0a41ce47d6adaec7d423bca272243b44cc965c5d4cd4b74a2549639772bfad93d103ea30d456e0024816e1edc3e
data/lib/websockethook.rb CHANGED
@@ -1,9 +1,10 @@
1
- require 'faye/websocket'
1
+ require 'websocket-client-simple'
2
2
  require 'json'
3
- require 'eventmachine'
4
3
 
5
4
  class WebSocketHook
6
- DEFAULT_HOST = 'ws://websockethook.io'
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
- initialize_pause options
15
- initialize_keep_alive options
16
- initialize_ping options
15
+ # initialize_keep_alive options
17
16
  end
18
17
 
19
- def listen(id=nil, &block)
20
- fail 'Block must be provided' unless block && block.is_a?(Proc)
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
- def stop
33
- @stopping = true
34
- stop_em
35
- end
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
- def initialize_pause(options = {})
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 initialize_keep_alive(options = {})
50
- @keep_alive = options[:keep_alive] || DEFAULT_KEEP_ALIVE
51
- fail 'Keep Alive (:keep_alive) must be a boolean (true/false)' unless @keep_alive == true || @keep_alive == false
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 stop_em
60
- EM.stop
61
- rescue
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 listener(id, &block)
65
- websocket(block) do |ws|
66
- ws.on :open do
67
- block.call type: 'open'
68
- ws.send({type:'register',id:id}.to_json)
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
- content = data.inject({}) { |memo, (k, v)| memo[k.to_sym] = v; memo }
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
- handle_ws(ws, :error, block) { @hooks.each { |hook| ws.send({ type: 'register', id: hook }.to_json) } }
83
- handle_ws(ws, :close, block) { stop_em }
84
- handle_ws(ws, :error, block) { stop_em }
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 websocket(callback_block, &socket_block)
89
- EM.run do
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 handle_ws(ws, action, callback_block, &_block)
103
- ws.on action do
104
- callback_block.call(type: action.to_s)
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
@@ -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({type: 'open'})
14
- expect(@logger).to receive(:log).with({type: 'close'})
15
- expect(@logger).to receive(:log).with({type: 'stopped'})
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 'test' do |msg|
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({type: 'open'})
26
- expect(@logger).to receive(:log).with({type: 'registered', data: anything()})
27
- expect(@logger).to receive(:log).with({type: 'hook', id: anything(), data: {this_is_a:'test'}})
28
- expect(@logger).to receive(:log).with({type: 'close'})
29
- expect(@logger).to receive(:log).with({type: 'stopped'})
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 msg[:type] == 'registered'
35
- hook_url = msg[:data][:url]
36
- RestClient.post(hook_url,this_is_a:'test')
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
- @ws.stop if msg[:type] == 'hook'
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({type: 'open'})
47
- expect(@logger).to receive(:log).with({type: 'registered', data: anything()})
48
- expect(@logger).to receive(:log).with({type: 'registered', data: {id:id, path:"/hook/#{id}",url:"http://websockethook.io/hook/#{id}"}})
49
- expect(@logger).to receive(:log).with({type: 'close'})
50
- expect(@logger).to receive(:log).with({type: 'stopped'})
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 msg[:type] == 'registered' && msg[:data][:id] == id
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.1.03
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-13 00:00:00.000000000 Z
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: