atr 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,49 @@
1
+ require 'reel'
2
+
3
+ class Atr::Server < Reel::Server::HTTP
4
+ def initialize(host = "127.0.0.1", port = 7777)
5
+
6
+ super(host, port, &method(:on_connection))
7
+ end
8
+
9
+ def on_connection(connection)
10
+ connection.each_request do |request|
11
+ if request.websocket?
12
+ if ::Atr.config.authenticate_with?
13
+ return unless ::Atr.config.authenticate_with.new(request).matches?
14
+ end
15
+
16
+ connection.detach
17
+
18
+ puts "NUMBER OF CONNECTIONS"
19
+ ::ActiveRecord::Base.clear_active_connections!
20
+ puts ::ActiveRecord::Base.connection_pool.instance_variable_get("@connections").size
21
+
22
+ puts "NUMBER OF ACTORS"
23
+ puts ::Celluloid::Actor.all.count
24
+
25
+ if ::Atr.config.scope_with?
26
+ routing_scope = ::Atr.config.scope_with.new(request)
27
+
28
+ return unless routing_scope.valid?
29
+
30
+ ::Atr::Reactor.new(request.websocket, routing_scope.routing_key)
31
+ else
32
+ ::Atr::Reactor.new(request.websocket)
33
+ end
34
+
35
+ return
36
+ else
37
+ handle_request(request)
38
+ end
39
+ end
40
+ end
41
+
42
+ def handle_request(request)
43
+ request.respond :ok, "Nothing to see here"
44
+ end
45
+
46
+ def handle_websocket(socket)
47
+ ::Atr::Reactor.new(socket)
48
+ end
49
+ end
@@ -0,0 +1,3 @@
1
+ module Atr
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ describe ::Atr::Event do
4
+ let(:fake_record) { ::Fake::Blog::Post.new }
5
+ let(:event_name) { "fake.blog.post.created"}
6
+ subject {
7
+ ::Atr::Event.new("fake.blog.post.created", event_name, fake_record)
8
+ }
9
+
10
+ before do
11
+ subject.stub(:id).and_return("something")
12
+ end
13
+
14
+ its(:id) { should eq "something" }
15
+ its(:name) { should eq "fake.blog.post.created" }
16
+ its(:record) { should eq fake_record.attributes }
17
+
18
+ context "serialization" do
19
+ it "should serialize into javascript parseable json" do
20
+ subject.to_json.should include "{\"id\":\"something\",\"name\":\"fake.blog.post.created\","
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,62 @@
1
+ require 'spec_helper'
2
+ require 'atr/publishable'
3
+ describe ::Atr::Publishable do
4
+ ::Fake::Blog::Post.class_eval do
5
+ include ::Atr::Publishable
6
+ publication_scope :user_id
7
+ end
8
+
9
+ before do
10
+ ::Atr::Redis.connect unless ::Atr::Redis.connected?
11
+
12
+ ::Atr.stub(:publish_event)
13
+ end
14
+
15
+ let(:fake_blog_post_attributes) {
16
+ {
17
+ :user_id => 123
18
+ }
19
+ }
20
+
21
+ let(:fake_blog_post) { ::Fake::Blog::Post.new(fake_blog_post_attributes) }
22
+
23
+ subject { fake_blog_post }
24
+
25
+ context "create" do
26
+ it "should publish create event" do
27
+ Atr.should_receive(:publish_event).with(instance_of(::Atr::Event))
28
+ subject.save
29
+ end
30
+ end
31
+
32
+ context "update" do
33
+ it "should publish updated event" do
34
+
35
+ Atr.should_receive(:publish_event).with(instance_of(::Atr::Event))
36
+ subject.save
37
+ subject.update_attributes(:title => "something else")
38
+ end
39
+ end
40
+
41
+
42
+ context "destroy" do
43
+ it "should publish destroy event" do
44
+ Atr.should_receive(:publish_event).with(instance_of(::Atr::Event))
45
+ subject.destroy
46
+ end
47
+ end
48
+
49
+ context "Class Methods" do
50
+ subject{ ::Fake::Blog::Post }
51
+
52
+ its(:publishable_actions) { should be_an(Array) }
53
+
54
+ its(:resource_routing_keys) { should include("fake", "blog", "post")}
55
+
56
+ describe ".build_publication_scope_for_record" do
57
+ it "should build record scoped to user id" do
58
+ subject.build_publication_scope_for_record(fake_blog_post).should eq ["user", 123]
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+
3
+ describe ::Atr::Publisher do
4
+ ::Fake::Blog::Post.class_eval do
5
+ include ::Atr::Publishable
6
+ end
7
+
8
+ before do
9
+ ::Atr::Redis.connect unless ::Atr::Redis.connected?
10
+
11
+ ::Atr::Publisher.supervise_as :atr_publisher
12
+ end
13
+
14
+ let(:fake_record) { ::Fake::Blog::Post.new }
15
+ let(:event_name) { "fake.blog.post.created"}
16
+ let(:routing_key) { "fake.blog.post.created" }
17
+ let(:record_created_routing_key) { "fake.blog.post.created" }
18
+ let(:record_updated_routing_key) { "fake.blog.post.updated" }
19
+ let(:record_destroyed_routing_key) { "fake.blog.post.destroyed" }
20
+
21
+ let(:fake_event) {
22
+ ::Atr::Event.new(routing_key, event_name, fake_record)
23
+ }
24
+
25
+ subject {
26
+ ::Atr::Publisher.supervise_as :atr_publisher
27
+ ::Celluloid::Actor[:atr_publisher]
28
+ }
29
+
30
+ describe "#publish_event" do
31
+ it "should receive publish event with instance of event when publishable record is saved" do
32
+ subject.should_receive(:publish_event).with(instance_of(::Atr::Event))
33
+
34
+ fake_record.save
35
+ end
36
+
37
+ it "should publish via redis connection" do
38
+ ::Atr::Redis.connection.should_receive(:publish).with(routing_key, Marshal.dump(fake_event))
39
+
40
+ subject.publish_event(fake_event)
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,13 @@
1
+ require 'spec_helper'
2
+
3
+ describe ::Atr::Redis do
4
+ subject { described_class }
5
+
6
+ describe ".connect" do
7
+ it "should connect to redis using celluloid driver" do
8
+ ::Redis.should_receive(:new).with(:driver => :celluloid)
9
+
10
+ subject.connect
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+
3
+ describe ::Atr::Registry do
4
+ ::Fake::Blog::Post.class_eval do
5
+ include ::Atr::Publishable
6
+ publication_scope :user_id
7
+ end
8
+
9
+ subject{ described_class }
10
+
11
+ let(:expected_channels) {
12
+ ["fake.blog.post.created", "fake.blog.post.updated", "fake.blog.post.destroyed"]
13
+ }
14
+ let(:user_id) { 1234 }
15
+ let(:scoped_routing_key) { "user.#{user_id}"}
16
+ let(:expected_scoped_channels) {
17
+ expected_channels.map{|channel| "#{scoped_routing_key}.#{channel}"}
18
+ }
19
+
20
+ its(:channels) { should include *expected_channels}
21
+
22
+ describe ".scoped_channels" do
23
+ it "should scope channels by routing key passed in" do
24
+ described_class.scoped_channels(scoped_routing_key).should include *expected_scoped_channels
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+ require 'ostruct'
3
+
4
+ describe ::Atr::RequestAuthenticator do
5
+ let(:fake_request) {
6
+ OpenStruct.new(:url => "user/1234")
7
+ }
8
+
9
+ subject {
10
+ described_class.new(fake_request)
11
+ }
12
+
13
+ describe "#new" do
14
+ its(:request) { should eq fake_request }
15
+ end
16
+
17
+ context "by default" do
18
+ its(:matches?) { should be_false }
19
+ end
20
+ end
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+ require 'ostruct'
3
+
4
+ describe ::Atr::RequestScope do
5
+ let(:fake_request) {
6
+ OpenStruct.new(:query_string => "user=1234")
7
+ }
8
+ let(:expected_params) {
9
+ {
10
+ "user" => "1234"
11
+ }
12
+ }
13
+
14
+ subject {
15
+ described_class.new(fake_request)
16
+ }
17
+
18
+ its(:request) { should eq fake_request }
19
+ its(:matches?) { should be_false }
20
+ its(:params) { should eq expected_params }
21
+ end
@@ -0,0 +1,12 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ require 'simplecov'
4
+ require 'pry'
5
+
6
+ SimpleCov.start do
7
+ add_filter '/spec/'
8
+ end
9
+
10
+ Bundler.require(:default, :development, :test)
11
+
12
+ ::Dir["#{::File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f }
@@ -0,0 +1,21 @@
1
+ require 'active_record'
2
+
3
+ ActiveRecord::Base.establish_connection(
4
+ :adapter => "sqlite3",
5
+ :database => "spec/test.db"
6
+ )
7
+
8
+ ActiveRecord::Base.connection.tables.each do |table|
9
+ ActiveRecord::Base.connection.drop_table(table)
10
+ end
11
+
12
+ ActiveRecord::Schema.define(:version => 1) do
13
+ create_table :posts do |t|
14
+ t.string :body
15
+ t.string :title
16
+ t.string :slug
17
+ t.integer :user_id
18
+
19
+ t.timestamps
20
+ end
21
+ end
@@ -0,0 +1 @@
1
+ require 'support/models/post'
@@ -0,0 +1,8 @@
1
+ require 'active_record'
2
+ module Fake
3
+ module Blog
4
+ class Post < ::ActiveRecord::Base
5
+
6
+ end
7
+ end
8
+ end
Binary file
metadata ADDED
@@ -0,0 +1,287 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: atr
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jason Ayre
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: active_attr
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: reel
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>'
32
+ - !ruby/object:Gem::Version
33
+ version: 0.4.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.4.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: redis
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
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: celluloid-io
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
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: celluloid-redis
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
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: json
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: activerecord
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: sqlite3
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: bundler
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - '>='
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: rake
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - '>='
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - '>='
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: rspec
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - '>='
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - '>='
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ - !ruby/object:Gem::Dependency
168
+ name: rspec-pride
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - '>='
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - '>='
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
181
+ - !ruby/object:Gem::Dependency
182
+ name: pry-nav
183
+ requirement: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - '>='
186
+ - !ruby/object:Gem::Version
187
+ version: '0'
188
+ type: :development
189
+ prerelease: false
190
+ version_requirements: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - '>='
193
+ - !ruby/object:Gem::Version
194
+ version: '0'
195
+ - !ruby/object:Gem::Dependency
196
+ name: simplecov
197
+ requirement: !ruby/object:Gem::Requirement
198
+ requirements:
199
+ - - '>='
200
+ - !ruby/object:Gem::Version
201
+ version: '0'
202
+ type: :development
203
+ prerelease: false
204
+ version_requirements: !ruby/object:Gem::Requirement
205
+ requirements:
206
+ - - '>='
207
+ - !ruby/object:Gem::Version
208
+ version: '0'
209
+ description: Pub sub and websockets
210
+ email:
211
+ - jasonayre@gmail.com
212
+ executables:
213
+ - atr_server
214
+ extensions: []
215
+ extra_rdoc_files: []
216
+ files:
217
+ - .gitignore
218
+ - .rspec
219
+ - Gemfile
220
+ - LICENSE.txt
221
+ - README.md
222
+ - Rakefile
223
+ - atr.gemspec
224
+ - bin/atr_server
225
+ - lib/atr.rb
226
+ - lib/atr/config.rb
227
+ - lib/atr/errors.rb
228
+ - lib/atr/event.rb
229
+ - lib/atr/publishable.rb
230
+ - lib/atr/publisher.rb
231
+ - lib/atr/railtie.rb
232
+ - lib/atr/reactor.rb
233
+ - lib/atr/redis.rb
234
+ - lib/atr/registry.rb
235
+ - lib/atr/request_authenticator.rb
236
+ - lib/atr/request_scope.rb
237
+ - lib/atr/server.rb
238
+ - lib/atr/version.rb
239
+ - spec/atr/event_spec.rb
240
+ - spec/atr/publishable_spec.rb
241
+ - spec/atr/publisher_spec.rb
242
+ - spec/atr/redis_spec.rb
243
+ - spec/atr/registry_spec.rb
244
+ - spec/atr/request_authenticator_spec.rb
245
+ - spec/atr/request_scope_spec.rb
246
+ - spec/spec_helper.rb
247
+ - spec/support/db/setup.rb
248
+ - spec/support/models.rb
249
+ - spec/support/models/post.rb
250
+ - spec/test.db
251
+ homepage: http://github.com/jasonayre/atr
252
+ licenses:
253
+ - MIT
254
+ metadata: {}
255
+ post_install_message:
256
+ rdoc_options: []
257
+ require_paths:
258
+ - lib
259
+ required_ruby_version: !ruby/object:Gem::Requirement
260
+ requirements:
261
+ - - '>='
262
+ - !ruby/object:Gem::Version
263
+ version: '0'
264
+ required_rubygems_version: !ruby/object:Gem::Requirement
265
+ requirements:
266
+ - - '>='
267
+ - !ruby/object:Gem::Version
268
+ version: '0'
269
+ requirements: []
270
+ rubyforge_project:
271
+ rubygems_version: 2.0.6
272
+ signing_key:
273
+ specification_version: 4
274
+ summary: Pub, sub and websocket server
275
+ test_files:
276
+ - spec/atr/event_spec.rb
277
+ - spec/atr/publishable_spec.rb
278
+ - spec/atr/publisher_spec.rb
279
+ - spec/atr/redis_spec.rb
280
+ - spec/atr/registry_spec.rb
281
+ - spec/atr/request_authenticator_spec.rb
282
+ - spec/atr/request_scope_spec.rb
283
+ - spec/spec_helper.rb
284
+ - spec/support/db/setup.rb
285
+ - spec/support/models.rb
286
+ - spec/support/models/post.rb
287
+ - spec/test.db