celluloid-eventsource 0.0.2

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: 9143242b39f2439340b8c2240189f969370c1f86
4
+ data.tar.gz: fa4f5ec86e33322131670ab0d97c5fba471e4b85
5
+ SHA512:
6
+ metadata.gz: eff65fac28606b27a64166e7793eebb5152afbf9b568a3caf9582782b1737301d07662b4916c9e0e7ff254780929483ee9aa496ccb585a94c33fae28f9c0c97f
7
+ data.tar.gz: 399b6650cb39a9a3e6b42634ae63efea7d03270f1139512b9e078874a8c5c6659cb37a5d5b0c8d97c11eae5257248d2f9724e01ae08473757d10a6cf7aa3de5b
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/.rspec ADDED
@@ -0,0 +1 @@
1
+ --format RSpec::Pride
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in celluloid-eventsource.gemspec
4
+ gemspec
data/LICENSE.md ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Leo Correa
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,62 @@
1
+ # Celluloid::Eventsource
2
+
3
+ #### Under Development!! Use at your own risk :)
4
+
5
+ An EventSource client based off Celluloid::IO.
6
+
7
+ Specification based on [EventSource](http://www.w3.org/TR/2012/CR-eventsource-20121211/)
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ gem 'celluloid-eventsource'
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install celluloid-eventsource
22
+
23
+ then somewhere in your project:
24
+
25
+ require 'celluloid/eventsource'
26
+
27
+ ## Usage
28
+
29
+ Initializing a new `Celluloid::EventSource` object will create the connection:
30
+
31
+ ```ruby
32
+ es = Celluloid::EventSource.new("http://example.com/")
33
+ ```
34
+
35
+ Messages can be received on events such as `on_open`, `on_message` and `on_error`.
36
+
37
+ These can be assigned at initialize time
38
+
39
+ ```ruby
40
+ es = Celluloid::EventSource.new("http://example.com/") do |conn|
41
+ conn.on_message do |message|
42
+ puts "Message: #{message}"
43
+ end
44
+ end
45
+ ```
46
+
47
+ or on the object itself.
48
+
49
+ ```ruby
50
+ es.on_message do |message|
51
+ puts "Message: #{message}"
52
+ end
53
+ ```
54
+
55
+ ## Contributing
56
+
57
+ 1. Fork it
58
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
59
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
60
+ 4. Push to the branch (`git push origin my-new-feature`)
61
+ 5. Create new Pull Request
62
+
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'celluloid/eventsource/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "celluloid-eventsource"
8
+ spec.version = Celluloid::EventSource::VERSION
9
+ spec.authors = ["Leo Correa"]
10
+ spec.email = ["lcorr005@gmail.com"]
11
+ spec.description = %q{Celluloid::IO based library to consume Server-Sent Events.}
12
+ spec.summary = %q{celluloid-eventsource is a gem to consume SSE streaming API.}
13
+ spec.homepage = "https://github.com/Tonkpils/celluloid-eventsource"
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_dependency 'celluloid-io', '~> 0.15'
22
+ spec.add_dependency 'http_parser.rb', '~> 0.5'
23
+
24
+ spec.add_development_dependency "rspec", '~> 2.14'
25
+ spec.add_development_dependency "rspec-pride", '~> 2.2'
26
+ spec.add_development_dependency "bundler", "~> 1.3"
27
+ spec.add_development_dependency "rake", '~> 10.1'
28
+ spec.add_development_dependency "pry", '~> 0.9'
29
+ end
@@ -0,0 +1,49 @@
1
+ require 'http/parser'
2
+
3
+ module Celluloid
4
+
5
+ class EventSource
6
+
7
+ class ResponseParser
8
+ extend Forwardable
9
+
10
+ attr_reader :headers
11
+
12
+ delegate [:status_code, :<<] => :@parser
13
+
14
+ def initialize
15
+ @parser = Http::Parser.new(self)
16
+ @headers = nil
17
+ @chunk = ""
18
+ end
19
+
20
+ def headers?
21
+ !!@headers
22
+ end
23
+
24
+ def status
25
+ @parser.status_code
26
+ end
27
+
28
+ def on_headers_complete(headers)
29
+ @headers = headers
30
+ end
31
+
32
+ def on_body(chunk)
33
+ @chunk << chunk
34
+ end
35
+
36
+ def chunk
37
+ chunk = @chunk
38
+ unless chunk.empty?
39
+ @chunk = ""
40
+ end
41
+
42
+ chunk.to_s
43
+ end
44
+
45
+ end
46
+
47
+ end
48
+
49
+ end
@@ -0,0 +1,5 @@
1
+ module Celluloid
2
+ class EventSource
3
+ VERSION = "0.0.2"
4
+ end
5
+ end
@@ -0,0 +1,123 @@
1
+ require "celluloid/eventsource/version"
2
+ require 'celluloid/io'
3
+ require 'celluloid/eventsource/response_parser'
4
+
5
+ module Celluloid
6
+ class EventSource
7
+ include Celluloid::IO
8
+
9
+ attr_reader :url, :with_credentials
10
+
11
+ CONNECTING = 0
12
+ OPEN = 1
13
+ CLOSED = 2
14
+
15
+ attr_reader :ready_state
16
+
17
+ def initialize(url, options = {})
18
+ options = options.dup
19
+ @url = URI.parse(url)
20
+ @ready_state = CONNECTING
21
+ @with_credentials = options.delete(:with_credentials) { false }
22
+
23
+ @reconnect_timeout = 10
24
+ @last_event_id = String.new
25
+
26
+ @on_open = ->() {}
27
+ @on_error = ->(message) {}
28
+ @on_message = ->(message) {}
29
+
30
+ @socket = Celluloid::IO::TCPSocket.new(@url.host, @url.port)
31
+ @parser = ResponseParser.new
32
+
33
+ yield self if block_given?
34
+
35
+ async.run
36
+ end
37
+
38
+ def connected?
39
+ @ready_state == OPEN
40
+ end
41
+
42
+ def run
43
+ establish_connection
44
+
45
+ @socket.each do |data|
46
+ @parser << data
47
+ handle_stream(@parser.chunk)
48
+ end
49
+ end
50
+
51
+ def establish_connection
52
+ @socket.write(request_string)
53
+
54
+ until @parser.headers?
55
+ @parser << @socket.readline
56
+ end
57
+
58
+ if @parser.status_code != 200
59
+ close
60
+ @on_error.call("Unable to establish connection. Response status #{@parser.status_code}")
61
+ end
62
+
63
+ handle_headers(@parser.headers)
64
+ end
65
+
66
+ def close
67
+ @ready_state = CLOSED
68
+ @socket.close
69
+ end
70
+
71
+ def on_open(&block)
72
+ @on_open = block
73
+ end
74
+
75
+ def on_message(&block)
76
+ @on_message = block
77
+ end
78
+
79
+ def on_error(&block)
80
+ @on_error = block
81
+ end
82
+
83
+ private
84
+
85
+ def handle_stream(stream)
86
+ data = ""
87
+
88
+ stream.split("\n").each do |part|
89
+ case part
90
+ when /^data:(.+)$/
91
+ data = $1
92
+ when /^id:(.+)$/
93
+ @last_event_id = $1
94
+ when /^retry:(.+)$/
95
+ @reconnect_timeout = $1.to_i
96
+ when /^event:(.+)$/
97
+ # TODO
98
+ end
99
+ end
100
+
101
+ return if data.empty?
102
+ data.chomp!("\n")
103
+
104
+ @on_message.call(data)
105
+ end
106
+
107
+ def handle_headers(headers)
108
+ if headers['Content-Type'].include?("text/event-stream")
109
+ @ready_state = OPEN
110
+ @on_open.call
111
+ else
112
+ close
113
+ @on_error.call("Invalid Content-Type #{headers['Content-Type']}. Expected text/event-stream")
114
+ end
115
+ end
116
+
117
+ def request_string
118
+ "GET #{url.request_uri} HTTP/1.1\r\nHost: #{url.host}\r\nAccept: text/event-stream\r\nCache-Control: no-cache\r\n\r\n"
119
+ end
120
+
121
+ end
122
+
123
+ end
@@ -0,0 +1,53 @@
1
+ require 'spec_helper'
2
+
3
+ describe Celluloid::EventSource::ResponseParser do
4
+
5
+ let(:success_headers) {<<-eos
6
+ HTTP/1.1 200 OK
7
+ Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT
8
+ Content-Type: text/html; charset=UTF-8
9
+ Content-Length: 131
10
+ eos
11
+ }
12
+
13
+ let(:error_headers) {<<-eos
14
+ HTTP/1.1 400 OK
15
+ Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT
16
+ Content-Type: text/html; charset=UTF-8
17
+ Content-Length: 131
18
+ eos
19
+ }
20
+
21
+ let(:response_string) { "#{success_headers}\n\n{'hello' : 'world'}\n"}
22
+
23
+ let(:parser) { subject }
24
+
25
+
26
+ def streamed(string)
27
+ stream = StringIO.new(string)
28
+ stream.each do |line|
29
+ yield line
30
+ end
31
+ end
32
+
33
+ it 'parses a complete http response' do
34
+ streamed(response_string) do |line|
35
+ parser << line
36
+ end
37
+
38
+ expect(parser.status).to eq(200)
39
+ expect(parser.headers?).to be_true
40
+ expect(parser.headers['Content-Type']).to eq('text/html; charset=UTF-8')
41
+ expect(parser.headers['Content-Length']).to eq("131")
42
+ end
43
+
44
+ it 'waits until the entire request is found' do
45
+ streamed(success_headers) do |line|
46
+ parser << line
47
+ end
48
+
49
+ expect(parser.status).to eq(200)
50
+ expect(parser.headers?).to be_false
51
+ expect(parser.headers).to be_nil
52
+ end
53
+ end
@@ -0,0 +1,13 @@
1
+ require 'spec_helper'
2
+
3
+ describe Celluloid::EventSource do
4
+
5
+ it 'runs asynchronously on initialize' do
6
+ ces = double(Celluloid::EventSource)
7
+ Celluloid::IO::TCPSocket.stub(:new).with("example.com", 80)
8
+ Celluloid::EventSource.any_instance.should_receive(:async).and_return(ces)
9
+ ces.should_receive(:run)
10
+
11
+ Celluloid::EventSource.new("http://example.com")
12
+ end
13
+ end
@@ -0,0 +1,5 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ require 'celluloid/eventsource'
5
+ require 'celluloid/rspec'
metadata ADDED
@@ -0,0 +1,159 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: celluloid-eventsource
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Leo Correa
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-11-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: celluloid-io
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '0.15'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '0.15'
27
+ - !ruby/object:Gem::Dependency
28
+ name: http_parser.rb
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '0.5'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '0.5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '2.14'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '2.14'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec-pride
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '2.2'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '2.2'
69
+ - !ruby/object:Gem::Dependency
70
+ name: bundler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: '1.3'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: '1.3'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ~>
88
+ - !ruby/object:Gem::Version
89
+ version: '10.1'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ~>
95
+ - !ruby/object:Gem::Version
96
+ version: '10.1'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ~>
102
+ - !ruby/object:Gem::Version
103
+ version: '0.9'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ~>
109
+ - !ruby/object:Gem::Version
110
+ version: '0.9'
111
+ description: Celluloid::IO based library to consume Server-Sent Events.
112
+ email:
113
+ - lcorr005@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - .gitignore
119
+ - .rspec
120
+ - Gemfile
121
+ - LICENSE.md
122
+ - README.md
123
+ - Rakefile
124
+ - celluloid-eventsource.gemspec
125
+ - lib/celluloid/eventsource.rb
126
+ - lib/celluloid/eventsource/response_parser.rb
127
+ - lib/celluloid/eventsource/version.rb
128
+ - spec/celluloid/eventsource/response_parser_spec.rb
129
+ - spec/celluloid/eventsource_spec.rb
130
+ - spec/spec_helper.rb
131
+ homepage: https://github.com/Tonkpils/celluloid-eventsource
132
+ licenses:
133
+ - MIT
134
+ metadata: {}
135
+ post_install_message:
136
+ rdoc_options: []
137
+ require_paths:
138
+ - lib
139
+ required_ruby_version: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - '>='
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ required_rubygems_version: !ruby/object:Gem::Requirement
145
+ requirements:
146
+ - - '>='
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ requirements: []
150
+ rubyforge_project:
151
+ rubygems_version: 2.0.6
152
+ signing_key:
153
+ specification_version: 4
154
+ summary: celluloid-eventsource is a gem to consume SSE streaming API.
155
+ test_files:
156
+ - spec/celluloid/eventsource/response_parser_spec.rb
157
+ - spec/celluloid/eventsource_spec.rb
158
+ - spec/spec_helper.rb
159
+ has_rdoc: