ansible-sse 0.0.1
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.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.rvmrc +1 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +33 -0
- data/Rakefile +8 -0
- data/ansible.gemspec +28 -0
- data/lib/ansible.rb +7 -0
- data/lib/ansible/constant_resolver.rb +17 -0
- data/lib/ansible/sse.rb +19 -0
- data/lib/ansible/transmit.rb +72 -0
- data/lib/ansible/version.rb +7 -0
- data/lib/generators/ansible/templates/ansible.yml +5 -0
- data/spec/constant_resolver_spec.rb +11 -0
- data/spec/fixtures/controllers.rb +21 -0
- data/spec/fixtures/fake_rails_app.rb +28 -0
- data/spec/rails_integration_spec.rb +26 -0
- data/spec/spec_helper.rb +14 -0
- data/spec/sse_spec.rb +23 -0
- data/spec/transmit_spec.rb +27 -0
- metadata +169 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d3390748c842d7980b7b0bc3468fe163f9107bac
|
4
|
+
data.tar.gz: d2b641406e0bdd4138c9caaa813dfa69487245da
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6a130fec5f8e7d2ee83c270e0ba5dcc44dfa8e96dd8100d2aa60cae7e9af0371046eff4ee2054a7826c94341f455f69172fd06affaa993450e0dd1380bd81224
|
7
|
+
data.tar.gz: 947927464314aee692199b08911a56a82262c8fdb007c8157d60b7c01fbefe3c96ec3e3d7a03224a0e9b2efebfe831f19be7ac4b3634632036000c134ff2a38f
|
data/.gitignore
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm gemset use ansible
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Derek Parker
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# Ansible
|
2
|
+
|
3
|
+
Ansible provides a conveniant way to declaratively stream Server-Sent Events to the client using Rails 4.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'ansible'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install ansible
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
To use Ansible, simply add this to your controller for any model you want to transmit events for:
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
transmit :model
|
25
|
+
```
|
26
|
+
|
27
|
+
## Contributing
|
28
|
+
|
29
|
+
1. Fork it
|
30
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
31
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
32
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
33
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/ansible.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'ansible/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "ansible-sse"
|
8
|
+
spec.version = Ansible::VERSION
|
9
|
+
spec.authors = ["Derek Parker"]
|
10
|
+
spec.email = ["parkerderek86@gmail.com"]
|
11
|
+
spec.description = "Ansible allows you to stream events to a partial using ActionController::Live"
|
12
|
+
spec.summary = "Real time updates to browser using ActionController::Live"
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rails", "4.0.0.rc1"
|
24
|
+
spec.add_development_dependency "pry"
|
25
|
+
spec.add_development_dependency "rspec-rails"
|
26
|
+
spec.add_development_dependency "rspec-spies"
|
27
|
+
spec.add_development_dependency "pg"
|
28
|
+
end
|
data/lib/ansible.rb
ADDED
data/lib/ansible/sse.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
module Ansible
|
2
|
+
class SSE
|
3
|
+
attr_reader :stream
|
4
|
+
|
5
|
+
def initialize(stream)
|
6
|
+
@stream = stream
|
7
|
+
end
|
8
|
+
|
9
|
+
def write(event, message)
|
10
|
+
stream.write "event: #{event}\n"
|
11
|
+
stream.write "retry: 1000\n"
|
12
|
+
stream.write "data: #{message}\n\n"
|
13
|
+
end
|
14
|
+
|
15
|
+
def close
|
16
|
+
stream.close
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
module Ansible
|
2
|
+
module Transmit
|
3
|
+
def self.extended(base)
|
4
|
+
base.class_eval { include ActionController::Live }
|
5
|
+
end
|
6
|
+
|
7
|
+
def transmit(name)
|
8
|
+
beacon_name = :"#{name}_ansible_beacon"
|
9
|
+
build_route beacon_name
|
10
|
+
model = ConstantResolver.new(name).lookup_constant
|
11
|
+
|
12
|
+
model.extend TransmitModalHelper
|
13
|
+
|
14
|
+
define_method beacon_name, beacon_action
|
15
|
+
name
|
16
|
+
end
|
17
|
+
|
18
|
+
def build_route(beacon_name)
|
19
|
+
controller_name = self.to_s.underscore.gsub('_controller', '')
|
20
|
+
Rails.application.routes.draw do
|
21
|
+
Rails.application.routes.routes.named_routes.each do |path, route|
|
22
|
+
get path, route.defaults
|
23
|
+
end
|
24
|
+
get "/#{controller_name}/#{beacon_name}" => "#{controller_name}##{beacon_name}"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def beacon_action
|
31
|
+
Proc.new do
|
32
|
+
begin
|
33
|
+
beacon = __method__
|
34
|
+
model = ConstantResolver.new(beacon.to_s.split("_").first).lookup_constant
|
35
|
+
|
36
|
+
headers['Content-Type'] = 'text/event-stream'
|
37
|
+
sse = SSE.new(response.stream)
|
38
|
+
|
39
|
+
model.on_new_message do |event, message|
|
40
|
+
sse.write event, message
|
41
|
+
end
|
42
|
+
ensure
|
43
|
+
sse.close
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
module TransmitModalHelper
|
49
|
+
def self.extended(base)
|
50
|
+
base.class_eval do
|
51
|
+
after_commit :notify
|
52
|
+
|
53
|
+
def notify
|
54
|
+
self.class.connection.execute "NOTIFY #{self.class.table_name}, #{self.class.connection.quote attributes}"
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def on_new_message
|
60
|
+
listen
|
61
|
+
|
62
|
+
self.class.connection.wait_for_notify do |event, pid, message|
|
63
|
+
yield event, message
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def listen
|
68
|
+
self.class.connection.execute "LISTEN #{table_name}"
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'fixtures/fake_rails_app'
|
2
|
+
|
3
|
+
class Space < ActiveRecord::Base; end
|
4
|
+
|
5
|
+
class TestsController < ActionController::Base
|
6
|
+
include Rails.application.routes.url_helpers
|
7
|
+
|
8
|
+
transmit :space
|
9
|
+
|
10
|
+
def new
|
11
|
+
render text: "new"
|
12
|
+
end
|
13
|
+
|
14
|
+
def create
|
15
|
+
head :ok
|
16
|
+
end
|
17
|
+
|
18
|
+
def space_params
|
19
|
+
params.require(:space).permit(:expanding)
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'active_support/all'
|
2
|
+
require 'action_controller'
|
3
|
+
require 'action_dispatch'
|
4
|
+
require 'active_model'
|
5
|
+
require 'active_record'
|
6
|
+
require 'rails'
|
7
|
+
require 'ostruct'
|
8
|
+
require 'ansible'
|
9
|
+
|
10
|
+
module Rails
|
11
|
+
class App
|
12
|
+
def env_config; {} end
|
13
|
+
|
14
|
+
def routes
|
15
|
+
return @routes if defined? @routes
|
16
|
+
|
17
|
+
@routes = ActionDispatch::Routing::RouteSet.new.tap do |routes|
|
18
|
+
routes.draw do
|
19
|
+
post '/test' => "tests#create"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.application
|
26
|
+
@app ||= App.new
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'fixtures/controllers'
|
2
|
+
require 'rspec/rails'
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe TestsController, type: :controller do
|
6
|
+
describe 'GET space_ansible_beacon' do
|
7
|
+
before do
|
8
|
+
Space.stub(:on_new_message).and_yield('space', '{"expanding":true}')
|
9
|
+
get :space_ansible_beacon
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'sets the content-type to text/event-stream' do
|
13
|
+
response.headers['Content-Type'].should eq 'text/event-stream'
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'sends the next messages in the que concerning the current modal' do
|
17
|
+
sleep 0.1 until response.stream.closed?
|
18
|
+
response.body.should eq %{event: space\nretry: 1000\ndata: {"expanding":true}\n\n}
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'does not override predefined routes' do
|
22
|
+
post :create
|
23
|
+
response.should be_ok
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'rspec-spies'
|
2
|
+
|
3
|
+
ActiveRecord::Base.establish_connection :adapter => :postgresql,
|
4
|
+
:adapter => 'postgresql',
|
5
|
+
:database => 'ansible_test',
|
6
|
+
:pool => 5,
|
7
|
+
:timeout => 5000,
|
8
|
+
:host => 'localhost'
|
9
|
+
|
10
|
+
RSpec.configure do |config|
|
11
|
+
config.before do
|
12
|
+
Pathname.new(__FILE__).expand_path.join('../').to_s
|
13
|
+
end
|
14
|
+
end
|
data/spec/sse_spec.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'ansible/sse'
|
2
|
+
|
3
|
+
describe Ansible::SSE do
|
4
|
+
let(:io) { stub(write: true) }
|
5
|
+
let(:sse) { Ansible::SSE.new(io) }
|
6
|
+
|
7
|
+
describe '#write' do
|
8
|
+
context 'with an event specified' do
|
9
|
+
it 'sends data accross the pipe in the correct format' do
|
10
|
+
io.should_receive(:write).with("event: message\n")
|
11
|
+
io.should_receive(:write).with(%{data: {\"hello\":\"hello\"}\n\n})
|
12
|
+
sse.write(:message, '{"hello":"hello"}')
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
describe '#close' do
|
18
|
+
it 'closes the IO connection' do
|
19
|
+
io.should_receive :close
|
20
|
+
sse.close
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'action_controller'
|
2
|
+
require 'ansible/transmit'
|
3
|
+
|
4
|
+
class TestsController < ActionController::Base
|
5
|
+
extend Ansible::Transmit
|
6
|
+
|
7
|
+
transmit :space
|
8
|
+
|
9
|
+
def create
|
10
|
+
head :ok
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe Ansible::Transmit do
|
15
|
+
describe '.transmit' do
|
16
|
+
let(:controller) { TestsController.new }
|
17
|
+
let(:action_name) { :space_ansible_beacon }
|
18
|
+
|
19
|
+
it 'defines a method in the format name_ansible_beacon' do
|
20
|
+
controller.should respond_to action_name
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'allows the action to be routable' do
|
24
|
+
controller.hidden_actions.should_not include action_name
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,169 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ansible-sse
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Derek Parker
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-06-06 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rails
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 4.0.0.rc1
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 4.0.0.rc1
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
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: rspec-rails
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
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: rspec-spies
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
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: pg
|
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
|
+
description: Ansible allows you to stream events to a partial using ActionController::Live
|
112
|
+
email:
|
113
|
+
- parkerderek86@gmail.com
|
114
|
+
executables: []
|
115
|
+
extensions: []
|
116
|
+
extra_rdoc_files: []
|
117
|
+
files:
|
118
|
+
- .gitignore
|
119
|
+
- .rvmrc
|
120
|
+
- Gemfile
|
121
|
+
- LICENSE.txt
|
122
|
+
- README.md
|
123
|
+
- Rakefile
|
124
|
+
- ansible.gemspec
|
125
|
+
- lib/ansible.rb
|
126
|
+
- lib/ansible/constant_resolver.rb
|
127
|
+
- lib/ansible/sse.rb
|
128
|
+
- lib/ansible/transmit.rb
|
129
|
+
- lib/ansible/version.rb
|
130
|
+
- lib/generators/ansible/templates/ansible.yml
|
131
|
+
- spec/constant_resolver_spec.rb
|
132
|
+
- spec/fixtures/controllers.rb
|
133
|
+
- spec/fixtures/fake_rails_app.rb
|
134
|
+
- spec/rails_integration_spec.rb
|
135
|
+
- spec/spec_helper.rb
|
136
|
+
- spec/sse_spec.rb
|
137
|
+
- spec/transmit_spec.rb
|
138
|
+
homepage: ''
|
139
|
+
licenses:
|
140
|
+
- MIT
|
141
|
+
metadata: {}
|
142
|
+
post_install_message:
|
143
|
+
rdoc_options: []
|
144
|
+
require_paths:
|
145
|
+
- lib
|
146
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
147
|
+
requirements:
|
148
|
+
- - '>='
|
149
|
+
- !ruby/object:Gem::Version
|
150
|
+
version: '0'
|
151
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
152
|
+
requirements:
|
153
|
+
- - '>='
|
154
|
+
- !ruby/object:Gem::Version
|
155
|
+
version: '0'
|
156
|
+
requirements: []
|
157
|
+
rubyforge_project:
|
158
|
+
rubygems_version: 2.0.3
|
159
|
+
signing_key:
|
160
|
+
specification_version: 4
|
161
|
+
summary: Real time updates to browser using ActionController::Live
|
162
|
+
test_files:
|
163
|
+
- spec/constant_resolver_spec.rb
|
164
|
+
- spec/fixtures/controllers.rb
|
165
|
+
- spec/fixtures/fake_rails_app.rb
|
166
|
+
- spec/rails_integration_spec.rb
|
167
|
+
- spec/spec_helper.rb
|
168
|
+
- spec/sse_spec.rb
|
169
|
+
- spec/transmit_spec.rb
|