transmitter 0.9.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 95fa3f5e66aa623d8d97effbb831019f6cea4293
4
+ data.tar.gz: 755e1f43093a5f4b1331340559f7c4eeed173a48
5
+ SHA512:
6
+ metadata.gz: f8749d4fd1d800a9d6b09d9e56247f0db8d7a02413e4b15d1ea7569c91f9b9f9d2910d7df8530eef0dca9d8d031a008f2bcb34d522074647e6afb6e8b8bbec7c
7
+ data.tar.gz: 9b215a1c21eb831f6897cc1b8ccc1199f31b343016b6b4ca162f75c9f24156bd1bef9ba17434e8b2f378d6835aaac118d7a3171257ddb6f8d92312849f33f176
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm gemset use transmitter
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ansible.gemspec
4
+ gemspec
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,70 @@
1
+ # Transmitter
2
+
3
+ Transmitter provides a conveviance method to wrap the ActionController::Live functionality in Rails 4.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'transmitter'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install transmitter
18
+
19
+ ## Usage
20
+
21
+ To use Transmitter, simply create a routable action in your controller that will represent the event source for transmitting the Server-Sent Events, and then use the `transmit` method to send accross an event and data payload to the client.
22
+
23
+ The first arguement to `transmit` is the event name, the second arguement is amount of time between the client attempting to re-establish connection (in milliseconds), and the last argument is a hash containing the data that should be sent to the client.
24
+
25
+ ```ruby
26
+ FoosController < ActionController::Base
27
+ include ActionController::Live
28
+
29
+ ...
30
+
31
+ def transmit_action
32
+ stream do
33
+ transmit event: 'event', retry: 1000, data: { my: 'really', cool: 'message' }
34
+ end
35
+ end
36
+
37
+ ...
38
+
39
+ end
40
+ ```
41
+
42
+ This will create a properly formatted Server-Sent Event using the functionality of ActionController::Live.
43
+
44
+ To consume this event on the client side, simply use the Javascript EventSource API:
45
+
46
+ ```javascript
47
+ $(document).ready(function(){
48
+ var source = new EventSource('your/eventsource/url');
49
+ source.addEventListener('your_event', function(message){
50
+ // now the data that you send along will be available in
51
+ // message.data, and will come in the form of a JSON document
52
+ });
53
+ });
54
+ ```
55
+
56
+ ## Requirements
57
+
58
+ * Transmitter relies on the ActionController::Live functionality of Rails 4
59
+
60
+ ## Contributing
61
+
62
+ 1. Fork it
63
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
64
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
65
+ 4. Push to the branch (`git push origin my-new-feature`)
66
+ 5. Create new Pull Request
67
+
68
+ ## License
69
+
70
+ MIT
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new :spec do |spec|
5
+ spec.pattern = FileList['spec/**/*_spec.rb']
6
+ end
7
+
8
+ task default: [:spec]
@@ -0,0 +1,5 @@
1
+ development:
2
+ thing: yeah
3
+
4
+ test:
5
+ thing: yeah
@@ -0,0 +1,7 @@
1
+ require 'transmitter/sse'
2
+ require 'transmitter/constant_resolver'
3
+ require 'transmitter/origin'
4
+
5
+ ActiveSupport.on_load(:action_controller) do
6
+ include Transmitter::Origin
7
+ end
@@ -0,0 +1,17 @@
1
+ module Transmitter
2
+ class ConstantResolver
3
+ def initialize(name)
4
+ @name = name
5
+ end
6
+
7
+ def lookup_constant
8
+ name.constantize
9
+ end
10
+
11
+ private
12
+
13
+ def name
14
+ @name.to_s.titleize
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,33 @@
1
+ module Transmitter
2
+ module Origin
3
+ def stream
4
+ begin
5
+ set_headers
6
+ yield
7
+ ensure
8
+ close_connection
9
+ end
10
+ end
11
+
12
+ def transmit(opts)
13
+ sse.build_message(opts).stream unless stream_closed?
14
+ end
15
+
16
+ def close_connection
17
+ sse.close
18
+ end
19
+
20
+ def stream_closed?
21
+ response.stream.closed?
22
+ end
23
+
24
+ def set_headers
25
+ return if headers['Content-Type'] == 'text/event-stream'
26
+ headers['Content-Type'] = 'text/event-stream'
27
+ end
28
+
29
+ def sse
30
+ @sse ||= SSE.new response.stream
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,26 @@
1
+ require 'json'
2
+
3
+ module Transmitter
4
+ class SSE
5
+ attr_reader :response_stream, :message
6
+
7
+ def initialize(response_stream)
8
+ @response_stream = response_stream
9
+ end
10
+
11
+ def stream
12
+ response_stream.write message
13
+ end
14
+
15
+ def close
16
+ response_stream.close
17
+ end
18
+
19
+ def build_message(opts)
20
+ @message = "event: #{opts[:event]}\n"
21
+ @message << "retry: #{opts[:retry]}\n" if opts.has_key? :retry
22
+ @message << "data: #{opts[:data].to_json}\n\n"
23
+ self
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,7 @@
1
+ module Transmitter
2
+ MAJOR = 0
3
+ MINOR = 9
4
+ TINY = 6
5
+
6
+ VERSION = [MAJOR, MINOR, TINY].join "."
7
+ end
@@ -0,0 +1,12 @@
1
+ require 'spec_helper'
2
+ require 'transmitter/constant_resolver'
3
+
4
+ describe Transmitter::ConstantResolver do
5
+ describe '#lookup_constant' do
6
+ before { class Space; end }
7
+
8
+ it 'resolves the constant' do
9
+ described_class.new("Space").lookup_constant.should eq Space
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,20 @@
1
+ require 'fixtures/fake_rails_app'
2
+
3
+ class TestsController < ActionController::Base
4
+ include Rails.application.routes.url_helpers
5
+ include ActionController::Live
6
+
7
+ def new
8
+ render text: "new"
9
+ end
10
+
11
+ def create
12
+ head :ok
13
+ end
14
+
15
+ def space_beacon
16
+ stream do
17
+ transmit event: 'space', retry: 1000, data: { expanding: true }
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,29 @@
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 'transmitter'
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
+ get '/test/space_beacon' => 'tests#space_beacon'
21
+ end
22
+ end
23
+ end
24
+ end
25
+
26
+ def self.application
27
+ @app ||= App.new
28
+ end
29
+ end
@@ -0,0 +1,20 @@
1
+ require 'fixtures/controllers'
2
+ require 'rspec/rails'
3
+ require 'spec_helper'
4
+
5
+ describe TestsController, type: :controller do
6
+ describe 'GET space_beacon' do
7
+ before do
8
+ get :space_beacon
9
+ end
10
+
11
+ it 'sets the content-type to text/event-stream' do
12
+ response.headers['Content-Type'].should eq 'text/event-stream'
13
+ end
14
+
15
+ it 'sends the next messages in the que concerning the current modal' do
16
+ sleep 0.1 until response.stream.closed?
17
+ response.body.should eq %{event: space\nretry: 1000\ndata: {"expanding":true}\n\n}
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,9 @@
1
+ require 'fixtures/fake_rails_app'
2
+ require 'fixtures/controllers'
3
+
4
+ ActiveRecord::Base.establish_connection :adapter => :postgresql,
5
+ :adapter => 'postgresql',
6
+ :database => 'ansible_test',
7
+ :pool => 5,
8
+ :timeout => 5000,
9
+ :host => 'localhost'
data/spec/sse_spec.rb ADDED
@@ -0,0 +1,23 @@
1
+ require 'transmitter/sse'
2
+
3
+ describe Transmitter::SSE do
4
+ let(:io) { double('io', write: true, close: true) }
5
+ let(:sse) { Transmitter::SSE.new(io) }
6
+ let(:expected_message) { "event: message\nretry: 1000\ndata: {\"hello\":\"hello\"}\n\n" }
7
+
8
+ describe '#write' do
9
+ context 'with a retry specified' do
10
+ it 'sends data accross the pipe in the correct format' do
11
+ sse.build_message(event: 'message', retry: 1000, data: { hello: 'hello' }).stream
12
+ io.should have_received(:write).with(expected_message)
13
+ end
14
+ end
15
+ end
16
+
17
+ describe '#close' do
18
+ it 'closes the IO connection' do
19
+ sse.close
20
+ io.should have_received(:close)
21
+ end
22
+ end
23
+ end
@@ -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 'transmitter/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "transmitter"
8
+ spec.version = Transmitter::VERSION
9
+ spec.authors = ["Derek Parker"]
10
+ spec.email = ["parkerderek86@gmail.com"]
11
+ spec.description = "Transmitter provides a conveniance method to your Rails controllers for creating SSEs."
12
+ spec.summary = "Transmitter is a conveniant way to generate Server-Sent Events"
13
+ spec.homepage = "https://www.github.com/dparker1990/transmitter"
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"
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
metadata ADDED
@@ -0,0 +1,168 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: transmitter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.6
5
+ platform: ruby
6
+ authors:
7
+ - Derek Parker
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-07-26 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
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
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: Transmitter provides a conveniance method to your Rails controllers for
112
+ creating SSEs.
113
+ email:
114
+ - parkerderek86@gmail.com
115
+ executables: []
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - .gitignore
120
+ - .rvmrc
121
+ - Gemfile
122
+ - LICENSE.txt
123
+ - README.md
124
+ - Rakefile
125
+ - lib/generators/ansible/templates/ansible.yml
126
+ - lib/transmitter.rb
127
+ - lib/transmitter/constant_resolver.rb
128
+ - lib/transmitter/origin.rb
129
+ - lib/transmitter/sse.rb
130
+ - lib/transmitter/version.rb
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
+ - transmitter.gemspec
138
+ homepage: https://www.github.com/dparker1990/transmitter
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: Transmitter is a conveniant way to generate Server-Sent Events
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