thunderer 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +19 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +9 -0
  5. data/Gemfile +16 -0
  6. data/Guardfile +17 -0
  7. data/LICENSE.txt +22 -0
  8. data/README.md +32 -0
  9. data/Rakefile +12 -0
  10. data/app/assets/javascripts/thunderer.js +93 -0
  11. data/app/assets/javascripts/thunderer_angular.js +43 -0
  12. data/lib/generators/thunderer/install_generator.rb +19 -0
  13. data/lib/generators/thunderer/templates/thunderer.ru +10 -0
  14. data/lib/generators/thunderer/templates/thunderer.yml +10 -0
  15. data/lib/thunderer/controller_additions.rb +38 -0
  16. data/lib/thunderer/engine.rb +20 -0
  17. data/lib/thunderer/faye_extension.rb +40 -0
  18. data/lib/thunderer/messanger.rb +50 -0
  19. data/lib/thunderer/parser.rb +22 -0
  20. data/lib/thunderer/publish_changes.rb +51 -0
  21. data/lib/thunderer/railtie.rb +15 -0
  22. data/lib/thunderer/version.rb +3 -0
  23. data/lib/thunderer/view_helpers.rb +22 -0
  24. data/lib/thunderer.rb +66 -0
  25. data/spec/fixtures/thunderer.yml +8 -0
  26. data/spec/javascripts/helpers/.gitkeep +0 -0
  27. data/spec/javascripts/helpers/jasmine_examples/SpecHelper.js +15 -0
  28. data/spec/javascripts/support/jasmine.yml +123 -0
  29. data/spec/javascripts/support/jasmine_helper.rb +11 -0
  30. data/spec/javascripts/thunderer_interceptor_spec.js +0 -0
  31. data/spec/javascripts/thunderer_spec.js +118 -0
  32. data/spec/javascripts/thunderer_subscription_service_spec.js +0 -0
  33. data/spec/spec_helper.rb +29 -0
  34. data/spec/support/controller_macros.rb +5 -0
  35. data/spec/support/matchers/have_filter.rb +6 -0
  36. data/spec/thunderer/controller_aditions_spec.rb +48 -0
  37. data/spec/thunderer/messanger_spec.rb +46 -0
  38. data/spec/thunderer/parser_spec.rb +35 -0
  39. data/spec/thunderer/publish_changes_spec.rb +0 -0
  40. data/spec/thunderer_spec.rb +108 -0
  41. data/thunderer.gemspec +30 -0
  42. metadata +198 -0
@@ -0,0 +1,15 @@
1
+ beforeEach(function () {
2
+ jasmine.addMatchers({
3
+ toBePlaying: function () {
4
+ return {
5
+ compare: function (actual, expected) {
6
+ var player = actual;
7
+
8
+ return {
9
+ pass: player.currentlyPlayingSong === expected && player.isPlaying
10
+ }
11
+ }
12
+ };
13
+ }
14
+ });
15
+ });
@@ -0,0 +1,123 @@
1
+ # src_files
2
+ #
3
+ # Return an array of filepaths relative to src_dir to include before jasmine specs.
4
+ # Default: []
5
+ #
6
+ # EXAMPLE:
7
+ #
8
+ # src_files:
9
+ # - lib/source1.js
10
+ # - lib/source2.js
11
+ # - dist/**/*.js
12
+ #
13
+ src_files:
14
+ - thunderer.js
15
+
16
+ # stylesheets
17
+ #
18
+ # Return an array of stylesheet filepaths relative to src_dir to include before jasmine specs.
19
+ # Default: []
20
+ #
21
+ # EXAMPLE:
22
+ #
23
+ # stylesheets:
24
+ # - css/style.css
25
+ # - stylesheets/*.css
26
+ #
27
+ stylesheets:
28
+
29
+ # helpers
30
+ #
31
+ # Return an array of filepaths relative to spec_dir to include before jasmine specs.
32
+ # Default: ["helpers/**/*.js"]
33
+ #
34
+ # EXAMPLE:
35
+ #
36
+ # helpers:
37
+ # - helpers/**/*.js
38
+ #
39
+ helpers:
40
+ - 'helpers/**/*.js'
41
+
42
+ # spec_files
43
+ #
44
+ # Return an array of filepaths relative to spec_dir to include.
45
+ # Default: ["**/*[sS]pec.js"]
46
+ #
47
+ # EXAMPLE:
48
+ #
49
+ # spec_files:
50
+ # - **/*[sS]pec.js
51
+ #
52
+ spec_files:
53
+ - '**/*[sS]pec.js'
54
+
55
+ # src_dir
56
+ #
57
+ # Source directory path. Your src_files must be returned relative to this path. Will use root if left blank.
58
+ # Default: project root
59
+ #
60
+ # EXAMPLE:
61
+ #
62
+ # src_dir: public
63
+ #
64
+ src_dir: app/assets/javascripts
65
+
66
+
67
+ # spec_dir
68
+ #
69
+ # Spec directory path. Your spec_files must be returned relative to this path.
70
+ # Default: spec/javascripts
71
+ #
72
+ # EXAMPLE:
73
+ #
74
+ # spec_dir: spec/javascripts
75
+ #
76
+ spec_dir: spec/javascripts
77
+
78
+ # spec_helper
79
+ #
80
+ # Ruby file that Jasmine server will require before starting.
81
+ # Returned relative to your root path
82
+ # Default spec/javascripts/support/jasmine_helper.rb
83
+ #
84
+ # EXAMPLE:
85
+ #
86
+ # spec_helper: spec/javascripts/support/jasmine_helper.rb
87
+ #
88
+ spec_helper: spec/javascripts/support/jasmine_helper.rb
89
+
90
+ # boot_dir
91
+ #
92
+ # Boot directory path. Your boot_files must be returned relative to this path.
93
+ # Default: Built in boot file
94
+ #
95
+ # EXAMPLE:
96
+ #
97
+ # boot_dir: spec/javascripts/support/boot
98
+ #
99
+ boot_dir:
100
+
101
+ # boot_files
102
+ #
103
+ # Return an array of filepaths relative to boot_dir to include in order to boot Jasmine
104
+ # Default: Built in boot file
105
+ #
106
+ # EXAMPLE
107
+ #
108
+ # boot_files:
109
+ # - '**/*.js'
110
+ #
111
+ boot_files:
112
+
113
+ # rack_options
114
+ #
115
+ # Extra options to be passed to the rack server
116
+ # by default, Port and AccessLog are passed.
117
+ #
118
+ # This is an advanced options, and left empty by default
119
+ #
120
+ # EXAMPLE
121
+ #
122
+ # rack_options:
123
+ # server: 'thin'
@@ -0,0 +1,11 @@
1
+ #Use this file to set/override Jasmine configuration options
2
+ #You can remove it if you don't need it.
3
+ #This file is loaded *after* jasmine.yml is interpreted.
4
+ #
5
+ #Example: using a different boot file.
6
+ #Jasmine.configure do |config|
7
+ # config.boot_dir = '/absolute/path/to/boot_dir'
8
+ # config.boot_files = lambda { ['/absolute/path/to/boot_dir/file.js'] }
9
+ #end
10
+ #
11
+
File without changes
@@ -0,0 +1,118 @@
1
+ describe('Thunderer', function () {
2
+ var doc;
3
+ beforeEach(function () {
4
+ Faye = {};
5
+ doc = {};
6
+ });
7
+
8
+ it('adds subscription callback', function () {
9
+ Thunderer.subscribe('Hello', 'WorldCallback');
10
+ expect(Thunderer.subscriptionCallbacks['Hello']).toEqual('WorldCallback')
11
+ });
12
+
13
+ it('has fayExtension which add some signature for outgoing message',function () {
14
+ var called = false;
15
+ var message = {channel: "/meta/subscribe", subscription: "hello"}
16
+ Thunderer.subscriptions["hello"] = {signature: "abcd", timestamp: "1234"}
17
+ Thunderer.fayeExtension.outgoing(message, function(message) {
18
+ expect(message.ext.thunderer_signature).toEqual("abcd");
19
+ expect(message.ext.thunderer_timestamp).toEqual("1234");
20
+ called = true;
21
+ });
22
+ expect(called).toBeTruthy();
23
+ });
24
+
25
+ it("adds a faye subscription with response handler when signing", function() {
26
+ var faye = {subscribe: jasmine.createSpy()};
27
+ spyOn(Thunderer, 'faye').and.callFake(function(callback) {
28
+ callback(faye);
29
+ });
30
+ var options = {server: "server", channel: "somechannel"};
31
+ Thunderer.sign(options);
32
+ expect(faye.subscribe).toHaveBeenCalledWith("somechannel", jasmine.any(Function));
33
+ expect(Thunderer.subscriptions.server).toEqual("server");
34
+ expect(Thunderer.subscriptions.somechannel).toEqual(options);
35
+ });
36
+
37
+ it("unsubscribes a channel by name", function(){
38
+ var sub = { cancel: jasmine.createSpy() };
39
+ var faye = {subscribe: function(){ return sub; }};
40
+ spyOn(Thunderer, 'faye').and.callFake(function(callback) {
41
+ callback(faye);
42
+ });
43
+ var options = { server: "server", channel: "somechannel" };
44
+ Thunderer.sign(options);
45
+ expect(Thunderer.subscription("somechannel")).toEqual(sub);
46
+ Thunderer.unsubscribe("somechannel");
47
+ expect(sub.cancel).toHaveBeenCalled();
48
+ expect(Thunderer.subscription("somechannel")).toBeFalsy();
49
+ });
50
+
51
+ it("unsubscribes all channels", function(){
52
+ var created = 0;
53
+ var sub = function() {
54
+ created ++;
55
+ var sub = { cancel: function(){ created --; } };
56
+ return sub;
57
+ };
58
+ var faye = { subscribe: function(){ return sub(); }};
59
+ spyOn(Thunderer, 'faye').and.callFake(function(callback) {
60
+ callback(faye);
61
+ });
62
+ Thunderer.sign({server: "server", channel: "firstchannel"});
63
+ Thunderer.sign({server: "server", channel: "secondchannel"});
64
+ expect(created).toEqual(2);
65
+ expect(Thunderer.subscription("firstchannel")).toBeTruthy();
66
+ expect(Thunderer.subscription("secondchannel")).toBeTruthy();
67
+ Thunderer.unsubscribeAll()
68
+ expect(created).toEqual(0);
69
+ expect(Thunderer.subscription("firstchannel")).toBeFalsy();
70
+ expect(Thunderer.subscription("secondchannel")).toBeFalsy();
71
+ });
72
+
73
+ it("returns the subscription object for a subscribed channel", function(){
74
+ var faye = {subscribe: function(){ return "subscription"; }};
75
+ spyOn(Thunderer, 'faye').and.callFake(function(callback) {
76
+ callback(faye);
77
+ });
78
+ var options = { server: "server", channel: "somechannel" };
79
+ Thunderer.sign(options);
80
+ expect(Thunderer.subscription("somechannel")).toEqual("subscription")
81
+ });
82
+
83
+ it("triggers callback matching message channel in response", function() {
84
+ var handler, callbackSpy, response, faye;
85
+ callbackSpy = jasmine.createSpy();
86
+ response = {channel: 'test', data: 'abcd'};
87
+ faye = {subscribe: function (channel, handleResponse) {
88
+ handler = handleResponse;
89
+ }};
90
+ spyOn(Thunderer, 'faye').and.callFake(function(callback) {
91
+ callback(faye);
92
+ });
93
+ var options = {server: "server", channel: "somechannel"};
94
+ Thunderer.sign(options);
95
+ Thunderer.subscribe("test", callbackSpy);
96
+ handler(response);
97
+
98
+ expect(callbackSpy).toHaveBeenCalledWith('abcd','test')
99
+ });
100
+
101
+ it("takes a callback for subscription object when signing", function(){
102
+ var faye = {subscribe: function(){ return "subscription"; }};
103
+ spyOn(Thunderer, 'faye').and.callFake(function(callback) {
104
+ callback(faye);
105
+ });
106
+ var options = { server: "server", channel: "somechannel" };
107
+ options.subscription = jasmine.createSpy();
108
+ Thunderer.sign(options);
109
+ expect(options.subscription).toHaveBeenCalledWith("subscription");
110
+ });
111
+
112
+ it("adds fayeCallback when client and server aren't available", function() {
113
+ Thunderer.faye("callback");
114
+ expect(Thunderer.fayeCallbacks[0]).toEqual("callback");
115
+ });
116
+
117
+
118
+ });
@@ -0,0 +1,29 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ require 'codeclimate-test-reporter'
8
+ CodeClimate::TestReporter.start
9
+ require 'bundler/setup'
10
+ require 'support/matchers/have_filter'
11
+ require 'json'
12
+ require 'faye'
13
+ require 'support/controller_macros'
14
+ Bundler.setup
15
+
16
+ require 'thunderer'
17
+ RSpec.configure do |config|
18
+ config.treat_symbols_as_metadata_keys_with_true_values = true
19
+ config.run_all_when_everything_filtered = true
20
+ config.filter_run :focus
21
+
22
+ config.include ControllerMacros
23
+
24
+ # Run specs in random order to surface order dependencies. If you find an
25
+ # order dependency and want to debug it, you can fix the order by providing
26
+ # the seed, which is printed after each run.
27
+ # --seed 1234
28
+ config.order = 'random'
29
+ end
@@ -0,0 +1,5 @@
1
+ module ControllerMacros
2
+ def has_before_filters *names
3
+ expect(controller).to have_filters(:before, *names)
4
+ end
5
+ end
@@ -0,0 +1,6 @@
1
+ RSpec::Matchers.define :have_filters do |kind, *names|
2
+ match do |controller|
3
+ filters = controller._process_action_callbacks.select{ |f| f.kind == kind }.map(&:filter)
4
+ names.all?{ |name| filters.include?(name) }
5
+ end
6
+ end
@@ -0,0 +1,48 @@
1
+ require 'spec_helper'
2
+ require 'action_controller'
3
+ require 'thunderer/controller_additions'
4
+
5
+
6
+ describe Thunderer::ControllerAdditions do
7
+ class MyController < ActionController::Base
8
+ include Thunderer::ControllerAdditions
9
+
10
+ def index
11
+ # render text: 'ok'
12
+ end
13
+
14
+ def params; end
15
+ end
16
+
17
+ let(:controller_class) { MyController }
18
+
19
+ describe '#thunderer_channels' do
20
+
21
+ it 'should affect channels class variable' do
22
+ controller_class.thunderer_channels('hello')
23
+ expect(controller_class.channels).to include('hello')
24
+ end
25
+
26
+ it 'should affect interpolation_object of class' do
27
+ controller_class.thunderer_channels(object: 'hello')
28
+ expect(controller_class.interpolation_object).to include('hello')
29
+ end
30
+
31
+ it 'should setup before filter for setting headers' do
32
+ expect(controller_class).to have_filters(:before, :add_channels_header)
33
+ end
34
+
35
+ context 'for instance of controller' do
36
+ subject(:controller) { controller_class.new }
37
+
38
+ # it 'should add special header for index response' do
39
+ # controller_class.thunderer_channels('hello')
40
+ # allow(controller).to receive(:headers)
41
+ # controller.send(:add_channels_header)
42
+ # end
43
+
44
+ end
45
+
46
+ end
47
+
48
+ end
@@ -0,0 +1,46 @@
1
+ require 'spec_helper'
2
+ require 'thunderer/messanger'
3
+
4
+ describe Thunderer::Messanger do
5
+ let(:messanger) { Thunderer::Messanger }
6
+ before { Thunderer::Messanger.reset_config }
7
+
8
+ it 'default have nil uri' do
9
+ expect(messanger.config['uri']).to eq(nil)
10
+ end
11
+
12
+ it 'default have nil use_ssl' do
13
+ expect(messanger.config['use_ssl']).to eq(nil)
14
+ end
15
+
16
+ context 'with default config' do
17
+
18
+ it 'should raise error when you try post message' do
19
+ message = double(:message)
20
+ expect {
21
+ messanger.post(message)
22
+ }.to raise_error Thunderer::Messanger::ConfigurationError
23
+ end
24
+
25
+ end
26
+
27
+ describe '#configure' do
28
+
29
+ it 'parse url and set configuration' do
30
+ messanger.configure('http://google.ru')
31
+ expect(messanger.config).not_to eq({})
32
+ end
33
+
34
+ it 'set use_ssl to false for http' do
35
+ messanger.configure('http://google.ru')
36
+ expect(messanger.config['use_ssl']).to eq(false)
37
+ end
38
+
39
+ it 'set use_ssl to false for https' do
40
+ messanger.configure('https://google.ru')
41
+ expect(messanger.config['use_ssl']).to eq(true)
42
+ end
43
+
44
+ end
45
+
46
+ end
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+ require 'ostruct'
3
+
4
+ describe Thunderer::Parser do
5
+
6
+ let(:object) { OpenStruct.new(first: 1, second: 2) }
7
+
8
+ describe '#interpolate_channel' do
9
+ subject { Thunderer::Parser.interpolate_channel(channel, object) }
10
+
11
+ context 'when channel have no interpolation' do
12
+ let(:channel) { 'without interpolation string' }
13
+
14
+ it { should == channel }
15
+ end
16
+
17
+ context 'when channel have one interpolation sequence' do
18
+ let(:channel) { '/hello/:first' }
19
+
20
+ it { should include('1') }
21
+
22
+ end
23
+
24
+ context 'when channel have two interpolations' do
25
+ let(:channel) { '/hello/:first/world/:second' }
26
+
27
+ it { should include('1') }
28
+
29
+ it { should include('2') }
30
+ end
31
+
32
+
33
+ end
34
+
35
+ end
File without changes
@@ -0,0 +1,108 @@
1
+ require 'spec_helper'
2
+
3
+ describe Thunderer do
4
+ before { Thunderer.reset_config }
5
+
6
+ it "defaults server to nil" do
7
+ Thunderer.config[:server].should be_nil
8
+ end
9
+
10
+ it "defaults signature_expiration to nil" do
11
+ Thunderer.config[:signature_expiration].should be_nil
12
+ end
13
+
14
+ it "defaults subscription timestamp to current time in milliseconds" do
15
+ time = Time.now
16
+ Time.stub(:now).and_return(time)
17
+ Thunderer.subscription[:timestamp].should eq((time.to_f * 1000).round)
18
+ end
19
+
20
+ it "loads a simple configuration file via load_config" do
21
+ Thunderer.load_config("spec/fixtures/thunderer.yml", "production")
22
+ Thunderer.config[:server].should eq("http://example.com/faye")
23
+ Thunderer.config[:secret_token].should eq("PRODUCTION_SECRET_TOKEN")
24
+ Thunderer.config[:signature_expiration].should eq(600)
25
+ end
26
+
27
+ it 'also configure Thunderer::Messanger when load config' do
28
+ Thunderer.load_config("spec/fixtures/thunderer.yml", "production")
29
+ # expect(Thunderer.messanger).to be_kind_of(Thunderer::Messanger)
30
+ expect(Thunderer.messanger.config).not_to be_nil
31
+ end
32
+
33
+ it "raises an exception if an invalid environment is passed to load_config" do
34
+ expect {
35
+ Thunderer.load_config("spec/fixtures/thunderer.yml", :test)
36
+ }.to raise_error ArgumentError
37
+ end
38
+
39
+
40
+ it "includes channel, server, and custom time in subscription" do
41
+ Thunderer.config[:server] = "server"
42
+ subscription = Thunderer.subscription(:timestamp => 123, :channel => "hello")
43
+ subscription[:timestamp].should eq(123)
44
+ subscription[:channel].should eq("hello")
45
+ subscription[:server].should eq("server")
46
+ end
47
+
48
+ it "does a sha1 digest of channel, timestamp, and secret token" do
49
+ Thunderer.config[:secret_token] = "token"
50
+ subscription = Thunderer.subscription(:timestamp => 123, :channel => "channel")
51
+ subscription[:signature].should eq(Digest::SHA1.hexdigest("tokenchannel123"))
52
+ end
53
+
54
+ it "formats a message hash given a channel and a hash" do
55
+ Thunderer.config[:secret_token] = "token"
56
+ Thunderer.message("chan", :foo => "bar").should eq(
57
+ :ext => {:thunderer_secret_token => "token"},
58
+ :channel => "chan",
59
+ :data => {
60
+ :channel => "chan",
61
+ :data => {:foo => "bar"}
62
+ }
63
+ )
64
+ end
65
+
66
+ it "publish message as json to server using Net::HTTP" do
67
+ message = 'foo'
68
+ Thunderer.load_config("spec/fixtures/thunderer.yml", "production")
69
+
70
+ expect(Thunderer::Messanger).to receive(:post)
71
+ Thunderer.publish_message(message)
72
+ end
73
+
74
+
75
+ it "raises an exception if no server is specified when calling publish_message" do
76
+ lambda {
77
+ Thunderer.publish_message("foo")
78
+ }.should raise_error(Thunderer::Error)
79
+ end
80
+
81
+ it "publish_to passes message to publish_message call" do
82
+ Thunderer.should_receive(:message).with("chan", "foo").and_return("message")
83
+ Thunderer.should_receive(:publish_message).with("message").and_return(:result)
84
+ Thunderer.publish_to("chan", "foo").should eq(:result)
85
+ end
86
+
87
+ it "has a Faye rack app instance" do
88
+ Thunderer.faye_app.should be_kind_of(Faye::RackAdapter)
89
+ end
90
+
91
+ it "says signature has expired when time passed in is greater than expiration" do
92
+ Thunderer.config[:signature_expiration] = 30*60
93
+ time = Thunderer.subscription[:timestamp] - 31*60*1000
94
+ Thunderer.signature_expired?(time).should be_true
95
+ end
96
+
97
+ it "says signature has not expired when time passed in is less than expiration" do
98
+ Thunderer.config[:signature_expiration] = 30*60
99
+ time = Thunderer.subscription[:timestamp] - 29*60*1000
100
+ Thunderer.signature_expired?(time).should be_false
101
+ end
102
+
103
+ it "says signature has not expired when expiration is nil" do
104
+ Thunderer.config[:signature_expiration] = nil
105
+ Thunderer.signature_expired?(0).should be_false
106
+ end
107
+
108
+ end
data/thunderer.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'thunderer/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "thunderer"
8
+ spec.version = Thunderer::VERSION
9
+ spec.authors = ["Nick Chubarov"]
10
+ spec.email = ["nick.chubarov@gmail.com"]
11
+ spec.summary = 'pub/sub messaging in Rails application'
12
+ spec.description = 'pub/sub messaging in Rails application thought Faye.'
13
+ spec.homepage = ''
14
+ spec.license = 'MIT'
15
+
16
+ spec.add_dependency 'faye'
17
+
18
+ spec.add_runtime_dependency 'activesupport'
19
+ spec.add_development_dependency 'rails'
20
+ spec.add_development_dependency 'rspec', '~> 2.14.1'
21
+ spec.add_development_dependency 'jasmine'
22
+
23
+ spec.files = `git ls-files -z`.split("\x0")
24
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
25
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
26
+ spec.require_paths = ["lib"]
27
+
28
+ spec.add_development_dependency "bundler", "~> 1.5"
29
+ spec.add_development_dependency "rake"
30
+ end