sinatra-cometio 0.1.3 → 0.1.4

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.
data/History.txt CHANGED
@@ -1,3 +1,7 @@
1
+ === 0.1.4 2013-3-2
2
+
3
+ * add CometIO::Client
4
+
1
5
  === 0.1.3 2013-2-19
2
6
 
3
7
  * bugfix queing on CometIO.push
@@ -58,8 +58,8 @@ module Sinatra
58
58
  type = params[:type]
59
59
  data = params[:data]
60
60
  from = params[:session]
61
- CometIO.emit type, data, from if type.size > 0
62
- {:session => session, :type => type, :data => data}.to_json
61
+ CometIO.emit type, data, from if type.to_s.size > 0
62
+ {:session => from, :type => type, :data => data}.to_json
63
63
  end
64
64
 
65
65
  end
@@ -0,0 +1,63 @@
1
+ require File.expand_path '../../sinatra-cometio/version', File.dirname(__FILE__)
2
+ require 'event_emitter'
3
+ require 'httparty'
4
+ require 'json'
5
+
6
+ class CometIO
7
+ class Client
8
+ class Error < StandardError
9
+ end
10
+
11
+ include EventEmitter
12
+ attr_reader :url, :session
13
+
14
+ def initialize(url)
15
+ raise ArgumentError, "invalid URL (#{url})" unless url.kind_of? String and url =~ /^https?:\/\/.+/
16
+ @url = url
17
+ @session = nil
18
+ end
19
+
20
+ def push(type, data)
21
+ begin
22
+ res = HTTParty.post @url, :body => {:type => type, :data => data, :session => @session}
23
+ rescue StandardError, Timeout::Error => e
24
+ emit :error, "CometIO push error"
25
+ ensure
26
+ emit :error, "CometIO push error" unless res.code == 200
27
+ end
28
+ end
29
+
30
+ def connect
31
+ self.on :__session_id do |session|
32
+ @session = session
33
+ self.emit :connect, @session
34
+ end
35
+ get
36
+ return self
37
+ end
38
+
39
+ private
40
+ def get
41
+ Thread.new do
42
+ loop do
43
+ begin
44
+ res = HTTParty.get "#{@url}?session=#{@session}", :timeout => 60000
45
+ unless res.code == 200
46
+ self.emit :error, "CometIO get error"
47
+ sleep 10
48
+ next
49
+ else
50
+ data = JSON.parse res.body
51
+ self.emit data['type'], data['data']
52
+ end
53
+ rescue StandardError, Timeout::Error
54
+ self.emit :error, "CometIO get error"
55
+ sleep 10
56
+ next
57
+ end
58
+ end
59
+ end
60
+ end
61
+
62
+ end
63
+ end
@@ -1,3 +1,3 @@
1
1
  module SinatraCometIO
2
- VERSION = '0.1.3'
2
+ VERSION = '0.1.4'
3
3
  end
data/sample/Gemfile CHANGED
@@ -9,3 +9,4 @@ gem 'event_emitter'
9
9
  gem 'json'
10
10
  gem 'haml'
11
11
  gem 'sass'
12
+ gem 'httparty'
data/sample/Gemfile.lock CHANGED
@@ -1,21 +1,27 @@
1
1
  GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
- backports (2.8.2)
4
+ backports (3.0.3)
5
5
  daemons (1.1.9)
6
6
  event_emitter (0.2.2)
7
- eventmachine (1.0.0)
7
+ eventmachine (1.0.1)
8
8
  foreman (0.61.0)
9
9
  thor (>= 0.13.6)
10
- haml (3.1.7)
11
- json (1.7.6)
12
- rack (1.5.1)
13
- rack-protection (1.3.2)
10
+ haml (4.0.0)
11
+ tilt
12
+ httparty (0.10.2)
13
+ multi_json (~> 1.0)
14
+ multi_xml (>= 0.5.2)
15
+ json (1.7.7)
16
+ multi_json (1.6.1)
17
+ multi_xml (0.5.3)
18
+ rack (1.5.2)
19
+ rack-protection (1.4.0)
14
20
  rack
15
21
  rack-test (0.6.2)
16
22
  rack (>= 1.0)
17
- sass (3.2.5)
18
- sinatra (1.3.4)
23
+ sass (3.2.6)
24
+ sinatra (1.3.5)
19
25
  rack (~> 1.4)
20
26
  rack-protection (~> 1.3)
21
27
  tilt (~> 1.3, >= 1.3.3)
@@ -31,7 +37,7 @@ GEM
31
37
  eventmachine (>= 0.12.6)
32
38
  rack (>= 1.0.0)
33
39
  thor (0.17.0)
34
- tilt (1.3.3)
40
+ tilt (1.3.4)
35
41
 
36
42
  PLATFORMS
37
43
  ruby
@@ -40,6 +46,7 @@ DEPENDENCIES
40
46
  event_emitter
41
47
  foreman
42
48
  haml
49
+ httparty
43
50
  json
44
51
  rack
45
52
  sass
data/sample/README.md CHANGED
@@ -20,6 +20,12 @@ Run
20
20
  => http://localhost:5000
21
21
 
22
22
 
23
+ CUI Client
24
+ ----------
25
+
26
+ % ruby bin/cui_chat_client.rb
27
+
28
+
23
29
  Deploy Heroku
24
30
  -------------
25
31
 
@@ -0,0 +1,26 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ $:.unshift File.expand_path '../../lib', File.dirname(__FILE__)
4
+ require 'sinatra/cometio/client'
5
+
6
+ name = `whoami`.strip || 'shokai'
7
+
8
+ client = CometIO::Client.new('http://localhost:5000/cometio/io').connect
9
+
10
+ client.on :connect do |session|
11
+ puts "connect!! (session_id:#{session})"
12
+ end
13
+
14
+ client.on :chat do |data|
15
+ puts "<#{data['name']}> #{data['message']}"
16
+ end
17
+
18
+ client.on :error do |err|
19
+ STDERR.puts err
20
+ end
21
+
22
+ loop do
23
+ line = STDIN.gets.strip
24
+ next if line.empty?
25
+ client.push :chat, {:message => line, :name => name}
26
+ end
@@ -22,4 +22,5 @@ Gem::Specification.new do |gem|
22
22
  gem.add_dependency 'sinatra-contrib', '>= 1.3.2'
23
23
  gem.add_dependency 'json', '>= 1.7.0'
24
24
  gem.add_dependency 'event_emitter', '>= 0.2.0'
25
+ gem.add_dependency 'httparty'
25
26
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinatra-cometio
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-02-18 00:00:00.000000000 Z
12
+ date: 2013-03-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rack
@@ -107,6 +107,22 @@ dependencies:
107
107
  - - ! '>='
108
108
  - !ruby/object:Gem::Version
109
109
  version: 0.2.0
110
+ - !ruby/object:Gem::Dependency
111
+ name: httparty
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
110
126
  description: Node.js like Comet I/O plugin for Sinatra.
111
127
  email:
112
128
  - hashimoto@shokai.org
@@ -126,11 +142,13 @@ files:
126
142
  - lib/sinatra-cometio/version.rb
127
143
  - lib/sinatra/application.rb
128
144
  - lib/sinatra/cometio.rb
145
+ - lib/sinatra/cometio/client.rb
129
146
  - sample/.gitignore
130
147
  - sample/Gemfile
131
148
  - sample/Gemfile.lock
132
149
  - sample/Procfile
133
150
  - sample/README.md
151
+ - sample/bin/cui_chat_client.rb
134
152
  - sample/config.ru
135
153
  - sample/main.rb
136
154
  - sample/public/js/index.js