eventflit-client 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,181 @@
1
+ require 'spec_helper'
2
+
3
+ describe "A EventflitClient::Channels collection" do
4
+ before do
5
+ @channels = EventflitClient::Channels.new
6
+ end
7
+
8
+ it "should initialize empty" do
9
+ expect(@channels).to be_empty
10
+ expect(@channels.size).to eq(0)
11
+ end
12
+
13
+ it "should instantiate new channels added to it by name" do
14
+ @channels << 'TestChannel'
15
+ expect(@channels.find('TestChannel').class).to eq(EventflitClient::Channel)
16
+ end
17
+
18
+ it "should allow removal of channels by name" do
19
+ @channels << 'TestChannel'
20
+ expect(@channels['TestChannel'].class).to eq(EventflitClient::Channel)
21
+ @channels.remove('TestChannel')
22
+ expect(@channels).to be_empty
23
+ end
24
+
25
+ it "should not allow two channels of the same name" do
26
+ @channels << 'TestChannel'
27
+ @channels << 'TestChannel'
28
+ expect(@channels.size).to eq(1)
29
+ end
30
+
31
+ end
32
+
33
+ describe "A EventflitClient::Channel" do
34
+ before do
35
+ @channels = EventflitClient::Channels.new
36
+ @channel = @channels << "TestChannel"
37
+ end
38
+
39
+ it 'should not be subscribed by default' do
40
+ expect(@channel.subscribed).to be_falsey
41
+ end
42
+
43
+ it 'should not be global by default' do
44
+ expect(@channel.global).to be_falsey
45
+ end
46
+
47
+ it 'can have procs bound to an event' do
48
+ @channel.bind('TestEvent') {}
49
+ expect(@channel.callbacks.size).to eq(1)
50
+ end
51
+
52
+ it 'should run callbacks when an event is dispatched' do
53
+
54
+ @channel.bind('TestEvent') do
55
+ EventflitClient.logger.test "Local callback running"
56
+ end
57
+
58
+ @channel.dispatch('TestEvent', {})
59
+ expect(EventflitClient.logger.test_messages).to include("Local callback running")
60
+ end
61
+
62
+ end
63
+
64
+ describe "A EventflitClient::Socket" do
65
+ before do
66
+ @socket = EventflitClient::Socket.new(TEST_APP_KEY, :secret => 'secret')
67
+ end
68
+
69
+ it 'should not connect when instantiated' do
70
+ expect(@socket.connected).to be_falsey
71
+ end
72
+
73
+ it 'should raise ArgumentError if TEST_APP_KEY is an empty string' do
74
+ expect {
75
+ @broken_socket = EventflitClient::Socket.new('')
76
+ }.to raise_error(ArgumentError)
77
+ expect {
78
+ @broken_socket = EventflitClient::Socket.new(nil)
79
+ }.to raise_error(ArgumentError)
80
+ end
81
+
82
+ describe "...when connected" do
83
+ before do
84
+ @socket.connect
85
+ end
86
+
87
+ it 'should know its connected' do
88
+ expect(@socket.connected).to be_truthy
89
+ end
90
+
91
+ it 'should know its socket_id' do
92
+ expect(@socket.socket_id).to eq('123abc')
93
+ end
94
+
95
+ it 'should not be subscribed to its global channel' do
96
+ expect(@socket.global_channel.subscribed).to be_falsey
97
+ end
98
+
99
+ it 'should subscribe to a channel' do
100
+ @channel = @socket.subscribe('testchannel')
101
+ expect(@socket.channels['testchannel']).to eq(@channel)
102
+ expect(@channel.subscribed).to be_truthy
103
+ end
104
+
105
+ it 'should unsubscribe from a channel' do
106
+ @socket.subscribe('testchannel')
107
+ @socket.unsubscribe('testchannel')
108
+ expect(EventflitClient.logger.test_messages.last).to include('eventflit:unsubscribe')
109
+ expect(@socket.channels['testchannel']).to be_nil
110
+ end
111
+
112
+ it 'should subscribe to a private channel' do
113
+ @channel = @socket.subscribe('private-testchannel')
114
+ expect(@socket.channels['private-testchannel']).to eq(@channel)
115
+ expect(@channel.subscribed).to be_truthy
116
+ end
117
+
118
+ it 'should subscribe to a presence channel with user_id' do
119
+ @channel = @socket.subscribe('presence-testchannel', '123')
120
+ expect(@socket.channels['presence-testchannel']).to eq(@channel)
121
+ expect(@channel.user_data).to eq('{"user_id":"123"}')
122
+ expect(@channel.subscribed).to be_truthy
123
+ end
124
+
125
+ it 'should subscribe to a presence channel with custom channel_data' do
126
+ @channel = @socket.subscribe('presence-testchannel', :user_id => '123', :user_name => 'john')
127
+ expect(@socket.channels['presence-testchannel']).to eq(@channel)
128
+ expect(@channel.user_data).to eq('{"user_id":"123","user_name":"john"}')
129
+ expect(@channel.subscribed).to be_truthy
130
+ end
131
+
132
+ it 'should allow binding of global events' do
133
+ @socket.bind('testevent') { |data| EventflitClient.logger.test("testchannel received #{data}") }
134
+ expect(@socket.global_channel.callbacks.has_key?('testevent')).to be_truthy
135
+ end
136
+
137
+ it 'should trigger callbacks for global events' do
138
+ @socket.bind('globalevent') { |data| EventflitClient.logger.test("Global event!") }
139
+ expect(@socket.global_channel.callbacks.has_key?('globalevent')).to be_truthy
140
+
141
+ @socket.simulate_received('globalevent', 'some data', '')
142
+ expect(EventflitClient.logger.test_messages.last).to include('Global event!')
143
+ end
144
+
145
+ it 'should kill the connection thread when disconnect is called' do
146
+ @socket.disconnect
147
+ expect(Thread.list.size).to eq(1)
148
+ end
149
+
150
+ it 'should not be connected after disconnecting' do
151
+ @socket.disconnect
152
+ expect(@socket.connected).to be_falsey
153
+ end
154
+
155
+ describe "when subscribed to a channel" do
156
+ before do
157
+ @channel = @socket.subscribe('testchannel')
158
+ end
159
+
160
+ it 'should allow binding of callbacks for the subscribed channel' do
161
+ @socket['testchannel'].bind('testevent') { |data| EventflitClient.logger.test(data) }
162
+ expect(@socket['testchannel'].callbacks.has_key?('testevent')).to be_truthy
163
+ end
164
+
165
+ it "should trigger channel callbacks when a message is received" do
166
+ # Bind 2 events for the channel
167
+ @socket['testchannel'].bind('coming') { |data| EventflitClient.logger.test(data) }
168
+ @socket['testchannel'].bind('going') { |data| EventflitClient.logger.test(data) }
169
+
170
+ # Simulate the first event
171
+ @socket.simulate_received('coming', 'Hello!', 'testchannel')
172
+ expect(EventflitClient.logger.test_messages.last).to include('Hello!')
173
+
174
+ # Simulate the second event
175
+ @socket.simulate_received('going', 'Goodbye!', 'testchannel')
176
+ expect(EventflitClient.logger.test_messages.last).to include('Goodbye!')
177
+ end
178
+
179
+ end
180
+ end
181
+ end
@@ -0,0 +1,51 @@
1
+ $:.unshift File.expand_path('../lib', __FILE__)
2
+
3
+ require 'eventflit-client'
4
+
5
+ require 'logger'
6
+
7
+ TEST_APP_KEY = "TEST_APP_KEY"
8
+
9
+ module EventflitClient
10
+ class TestLogger < Logger
11
+ attr_reader :test_messages
12
+
13
+ def initialize(logdev, shift_age = 0, shift_size = 1048576)
14
+ @test_messages = []
15
+ super
16
+ end
17
+ def test(msg)
18
+ @test_messages << msg
19
+ debug msg
20
+ end
21
+ end
22
+
23
+ class Socket
24
+ # Simulate a connection being established
25
+ def connect(async = false)
26
+ @connection_thread = Thread.new do
27
+ @connection = TestConnection.new
28
+ @global_channel.dispatch('eventflit:connection_established', JSON.dump({'socket_id' => '123abc'}))
29
+ end
30
+ @connection_thread.run
31
+ @connection_thread.join unless async
32
+ return self
33
+ end
34
+
35
+ def simulate_received(event_name, event_data, channel_name)
36
+ send_local_event(event_name, event_data, channel_name)
37
+ end
38
+ end
39
+
40
+ class TestConnection
41
+ def send(payload)
42
+ EventflitClient.logger.test("SEND: #{payload}")
43
+ end
44
+
45
+ def close
46
+ end
47
+ end
48
+
49
+ EventflitClient.logger = TestLogger.new('test.log')
50
+
51
+ end
metadata ADDED
@@ -0,0 +1,139 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: eventflit-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Eventflit
8
+ - Logan Koester
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2018-05-04 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: websocket
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '1.0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '1.0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: json
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rspec
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: rake
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: bundler
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ description: Client for consuming WebSockets from http://eventflit.com
85
+ email:
86
+ - support@eventflit.com
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files:
90
+ - LICENSE.txt
91
+ - README.md
92
+ files:
93
+ - ".document"
94
+ - ".gitignore"
95
+ - ".travis.yml"
96
+ - CHANGELOG.md
97
+ - Gemfile
98
+ - LICENSE.txt
99
+ - README.md
100
+ - Rakefile
101
+ - certs/cacert.pem
102
+ - eventflit-client.gemspec
103
+ - examples/hello_eventflit.rb
104
+ - examples/hello_eventflit_async.rb
105
+ - examples/hello_eventflit_ssl.rb
106
+ - examples/subscribe_private.rb
107
+ - lib/eventflit-client.rb
108
+ - lib/eventflit-client/channel.rb
109
+ - lib/eventflit-client/channels.rb
110
+ - lib/eventflit-client/socket.rb
111
+ - lib/eventflit-client/version.rb
112
+ - lib/eventflit-client/websocket.rb
113
+ - spec/eventflitclient_spec.rb
114
+ - spec/spec_helper.rb
115
+ homepage: http://github.com/eventflit/eventflit-websocket-ruby
116
+ licenses:
117
+ - MIT
118
+ metadata: {}
119
+ post_install_message:
120
+ rdoc_options: []
121
+ require_paths:
122
+ - lib
123
+ required_ruby_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ required_rubygems_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ requirements: []
134
+ rubyforge_project:
135
+ rubygems_version: 2.5.2.1
136
+ signing_key:
137
+ specification_version: 4
138
+ summary: Client for consuming WebSockets from http://eventflit.com
139
+ test_files: []