garufa 1.0.0 → 1.0.1.rc.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 +4 -4
- data/README.md +50 -8
- data/garufa.gemspec +4 -4
- data/lib/garufa/api.rb +46 -0
- data/lib/garufa/api/authentication.rb +16 -0
- data/lib/garufa/api/event_handler.rb +28 -0
- data/lib/garufa/garufa_app.rb +3 -4
- data/lib/garufa/subscription.rb +10 -1
- data/lib/garufa/subscriptions.rb +4 -10
- data/lib/garufa/version.rb +1 -1
- data/lib/garufa/{websocket/websocket.rb → websocket.rb} +0 -0
- metadata +16 -18
- data/bin/garufa.pid +0 -1
- data/lib/garufa/api/api.rb +0 -37
- data/lib/garufa/api/handler.rb +0 -17
- data/lib/garufa/cuba/authentication.rb +0 -14
- data/lib/garufa/goliath/connection.rb +0 -14
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 51982c470639a14bcfaa9c7cdbcd139984b78d16
|
|
4
|
+
data.tar.gz: fb7cadeb7651dd64c4fdaf6f4eb2916a7a65f629
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: e6f57e7381827e211bb40cb05617fd5168a99a8b0ce29d76cb729f9273ad5fa6d8c7e07770e82c4765334fdd6b49fe50fb504e04e6da0a2ef99a4c2be1c9ef56
|
|
7
|
+
data.tar.gz: ce0e6afef6b42108ffb4336c519f5117cef23a0cf978a244976e98f0411f55ae014f17c924435ef08dee5a35b6850d98ef6d1580d7d2c89dc161914c4f2e9e7c
|
data/README.md
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
[](https://travis-ci.org/Juanmcuello/garufa)
|
|
2
|
-
[](https://codeclimate.com/github/Juanmcuello/garufa)
|
|
3
3
|
|
|
4
4
|
Garufa
|
|
5
5
|
====
|
|
@@ -38,7 +38,7 @@ to run in background (daemonized) just add `-d` flag.
|
|
|
38
38
|
|
|
39
39
|
Now say you want to send events to your browser. Just create an .html file
|
|
40
40
|
which requires the *pusher.js* library and binds to some events, then point
|
|
41
|
-
your browser to that file (for testing purpouse, you can just
|
|
41
|
+
your browser to that file (for testing purpouse, you can just simply open it
|
|
42
42
|
with your browser).
|
|
43
43
|
|
|
44
44
|
Maybe you would like to open your JavaScript console to see JavaScript debug
|
|
@@ -124,12 +124,9 @@ server {
|
|
|
124
124
|
; Set this a little higher than Pusher ACTIVITY_TIMEOUT
|
|
125
125
|
proxy_read_timeout 150;
|
|
126
126
|
|
|
127
|
-
ssl
|
|
128
|
-
ssl_certificate
|
|
129
|
-
ssl_certificate_key
|
|
130
|
-
ssl_session_timeout 5m;
|
|
131
|
-
ssl_ciphers HIGH:!aNULL:!MD5;
|
|
132
|
-
ssl_prefer_server_ciphers on;
|
|
127
|
+
ssl on;
|
|
128
|
+
ssl_certificate /path/to/cert.pem;
|
|
129
|
+
ssl_certificate_key /path/to/cert.key;
|
|
133
130
|
|
|
134
131
|
location / {
|
|
135
132
|
proxy_pass http://garufa;
|
|
@@ -140,6 +137,51 @@ server {
|
|
|
140
137
|
}
|
|
141
138
|
```
|
|
142
139
|
|
|
140
|
+
Setting up Garufa on Heroku
|
|
141
|
+
---------------------------
|
|
142
|
+
|
|
143
|
+
Create a repo for your app:
|
|
144
|
+
|
|
145
|
+
```
|
|
146
|
+
$ mkdir garufa-heroku
|
|
147
|
+
$ cd garufa-heroku
|
|
148
|
+
$ git init .
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
Create Gemfile and Procfile files:
|
|
152
|
+
|
|
153
|
+
``` ruby
|
|
154
|
+
# Gemfile
|
|
155
|
+
source 'http://rubygems.org'
|
|
156
|
+
ruby '2.1.2'
|
|
157
|
+
gem 'garufa'
|
|
158
|
+
|
|
159
|
+
# Procfile
|
|
160
|
+
web: garufa -v --app_key $GARUFA_APP_KEY --secret $GARUFA_SECRET -p $PORT
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
Generate Gemfile.lock and commit your changes:
|
|
164
|
+
```
|
|
165
|
+
$ bundle install
|
|
166
|
+
$ git add .
|
|
167
|
+
$ git commit -m 'Initial commit'
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
Create your app, set environment variables and deploy:
|
|
171
|
+
|
|
172
|
+
```
|
|
173
|
+
$ heroku create
|
|
174
|
+
$ heroku config:set GARUFA_APP_KEY=app-key
|
|
175
|
+
$ heroku config:set GARUFA_SECRET=app-secret
|
|
176
|
+
$ git push heroku master
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
At this point you should have Garufa up and listening on port 80.
|
|
180
|
+
|
|
181
|
+
Set `Pusher.port` and `Pusher.ws_port` in the example above to port 80, and
|
|
182
|
+
`Pusher.host` to the name of your app provided by Heroku (something like
|
|
183
|
+
`random-name-2323.herokuapp.com`).
|
|
184
|
+
|
|
143
185
|
Testing and Contributing
|
|
144
186
|
------------------------
|
|
145
187
|
|
data/garufa.gemspec
CHANGED
|
@@ -25,10 +25,10 @@ Gem::Specification.new do |s|
|
|
|
25
25
|
"test/*.*"
|
|
26
26
|
]
|
|
27
27
|
|
|
28
|
-
s.add_dependency "goliath", "1.0.
|
|
29
|
-
s.add_dependency "faye-websocket", "0.7.
|
|
28
|
+
s.add_dependency "goliath", "1.0.4"
|
|
29
|
+
s.add_dependency "faye-websocket", "0.7.4"
|
|
30
30
|
s.add_dependency "cuba", "3.1.1"
|
|
31
31
|
s.add_dependency "signature", "0.1.7"
|
|
32
|
-
s.add_development_dependency "rake", "10.
|
|
33
|
-
s.add_development_dependency "minitest", "5.
|
|
32
|
+
s.add_development_dependency "rake", "10.3.2"
|
|
33
|
+
s.add_development_dependency "minitest", "5.4.0"
|
|
34
34
|
end
|
data/lib/garufa/api.rb
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
require 'cuba'
|
|
2
|
+
|
|
3
|
+
require 'garufa/api/authentication'
|
|
4
|
+
require 'garufa/api/event_handler'
|
|
5
|
+
|
|
6
|
+
module Garufa
|
|
7
|
+
module API
|
|
8
|
+
class Server < Cuba
|
|
9
|
+
|
|
10
|
+
plugin Authentication
|
|
11
|
+
plugin EventHandler
|
|
12
|
+
|
|
13
|
+
define do
|
|
14
|
+
on "apps/:app_id" do |app_id|
|
|
15
|
+
|
|
16
|
+
authenticate
|
|
17
|
+
|
|
18
|
+
# Events
|
|
19
|
+
on post, "events" do
|
|
20
|
+
handle_events(req.body.read)
|
|
21
|
+
res.status = 202
|
|
22
|
+
res.write "{}"
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# Legacy events
|
|
26
|
+
on post, "channels/:channel_id/events" do |channel_id|
|
|
27
|
+
handle_events(req.body.read, req.GET.merge(channels: [channel_id]))
|
|
28
|
+
res.status = 202
|
|
29
|
+
res.write "{}"
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# Channels
|
|
33
|
+
on get, "channels" do
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
on get, "channels/:channel" do
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# Users
|
|
40
|
+
on get, "channels/:channel/users" do
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
require 'signature'
|
|
2
|
+
require 'garufa/config'
|
|
3
|
+
|
|
4
|
+
module Garufa
|
|
5
|
+
module API
|
|
6
|
+
module Authentication
|
|
7
|
+
def authenticate
|
|
8
|
+
request = Signature::Request.new(req.request_method, req.path, req.params)
|
|
9
|
+
request.authenticate { |key| Signature::Token.new(key, Garufa::Config[:secret]) }
|
|
10
|
+
|
|
11
|
+
rescue Signature::AuthenticationError
|
|
12
|
+
halt([401, {}, ['401 Unauthorized']])
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
require 'json'
|
|
2
|
+
|
|
3
|
+
require 'garufa/subscriptions'
|
|
4
|
+
require 'garufa/message'
|
|
5
|
+
|
|
6
|
+
module Garufa
|
|
7
|
+
module API
|
|
8
|
+
module EventHandler
|
|
9
|
+
def handle_events(body, params = {})
|
|
10
|
+
body_params = JSON.parse(body)
|
|
11
|
+
|
|
12
|
+
# Some old api clients send channel and event in the url, while only data is
|
|
13
|
+
# in the body. New clients send everything in the body. We have to check where
|
|
14
|
+
# data is coming to build the final params.
|
|
15
|
+
params.merge!(body_params['data'] ? body_params : { data: body_params })
|
|
16
|
+
|
|
17
|
+
message = Garufa::Message.new(params)
|
|
18
|
+
options = { data: message.data, socket_id: message.socket_id }
|
|
19
|
+
|
|
20
|
+
# Process event deferred in order to response immediately.
|
|
21
|
+
EM.defer { Garufa::Subscriptions.notify message.channels, message.name, options }
|
|
22
|
+
|
|
23
|
+
rescue JSON::ParserError => e
|
|
24
|
+
env.logger.error e.inspect
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
data/lib/garufa/garufa_app.rb
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
require 'goliath/api'
|
|
2
2
|
require 'goliath/connection'
|
|
3
|
-
require 'garufa/goliath/connection'
|
|
4
3
|
require 'faye/websocket'
|
|
5
4
|
require 'garufa/config'
|
|
6
|
-
require 'garufa/websocket
|
|
7
|
-
require 'garufa/api
|
|
5
|
+
require 'garufa/websocket'
|
|
6
|
+
require 'garufa/api'
|
|
8
7
|
|
|
9
8
|
module Garufa
|
|
10
9
|
|
|
@@ -36,7 +35,7 @@ module Garufa
|
|
|
36
35
|
if Faye::WebSocket.websocket?(env)
|
|
37
36
|
WebSocket::Server.call(env)
|
|
38
37
|
else
|
|
39
|
-
|
|
38
|
+
API::Server.call(env)
|
|
40
39
|
end
|
|
41
40
|
end
|
|
42
41
|
end
|
data/lib/garufa/subscription.rb
CHANGED
|
@@ -3,7 +3,8 @@ require 'garufa/subscriptions'
|
|
|
3
3
|
|
|
4
4
|
module Garufa
|
|
5
5
|
class Subscription
|
|
6
|
-
|
|
6
|
+
|
|
7
|
+
attr_reader :error
|
|
7
8
|
|
|
8
9
|
def initialize(data, connection)
|
|
9
10
|
@data, @connection = data, connection
|
|
@@ -56,6 +57,14 @@ module Garufa
|
|
|
56
57
|
channel[/^private-|presence-/].to_s[0...-1]
|
|
57
58
|
end
|
|
58
59
|
|
|
60
|
+
def notify(message)
|
|
61
|
+
@connection.send_message message
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def socket_id
|
|
65
|
+
@connection.socket_id
|
|
66
|
+
end
|
|
67
|
+
|
|
59
68
|
private
|
|
60
69
|
|
|
61
70
|
def valid_channel?
|
data/lib/garufa/subscriptions.rb
CHANGED
|
@@ -18,17 +18,11 @@ module Garufa
|
|
|
18
18
|
end
|
|
19
19
|
|
|
20
20
|
def notify(channels, event, options = {})
|
|
21
|
-
return unless channels.is_a?(Array) && channels.any?
|
|
22
|
-
|
|
23
21
|
channels.each do |channel|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
next if connection.socket_id == options[:socket_id]
|
|
29
|
-
|
|
30
|
-
message = Message.channel_event(channel, event, options[:data])
|
|
31
|
-
connection.send_message(message)
|
|
22
|
+
subscriptions[channel].each do |sub|
|
|
23
|
+
# Skip notifying if the same socket_id is provided
|
|
24
|
+
next if sub.socket_id == options[:socket_id]
|
|
25
|
+
sub.notify Message.channel_event(channel, event, options[:data])
|
|
32
26
|
end
|
|
33
27
|
end
|
|
34
28
|
end
|
data/lib/garufa/version.rb
CHANGED
|
File without changes
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: garufa
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.0.
|
|
4
|
+
version: 1.0.1.rc.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Juan Manuel Cuello
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2014-
|
|
11
|
+
date: 2014-09-30 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: goliath
|
|
@@ -16,28 +16,28 @@ dependencies:
|
|
|
16
16
|
requirements:
|
|
17
17
|
- - '='
|
|
18
18
|
- !ruby/object:Gem::Version
|
|
19
|
-
version: 1.0.
|
|
19
|
+
version: 1.0.4
|
|
20
20
|
type: :runtime
|
|
21
21
|
prerelease: false
|
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
|
23
23
|
requirements:
|
|
24
24
|
- - '='
|
|
25
25
|
- !ruby/object:Gem::Version
|
|
26
|
-
version: 1.0.
|
|
26
|
+
version: 1.0.4
|
|
27
27
|
- !ruby/object:Gem::Dependency
|
|
28
28
|
name: faye-websocket
|
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
|
30
30
|
requirements:
|
|
31
31
|
- - '='
|
|
32
32
|
- !ruby/object:Gem::Version
|
|
33
|
-
version: 0.7.
|
|
33
|
+
version: 0.7.4
|
|
34
34
|
type: :runtime
|
|
35
35
|
prerelease: false
|
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
|
37
37
|
requirements:
|
|
38
38
|
- - '='
|
|
39
39
|
- !ruby/object:Gem::Version
|
|
40
|
-
version: 0.7.
|
|
40
|
+
version: 0.7.4
|
|
41
41
|
- !ruby/object:Gem::Dependency
|
|
42
42
|
name: cuba
|
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -72,28 +72,28 @@ dependencies:
|
|
|
72
72
|
requirements:
|
|
73
73
|
- - '='
|
|
74
74
|
- !ruby/object:Gem::Version
|
|
75
|
-
version: 10.
|
|
75
|
+
version: 10.3.2
|
|
76
76
|
type: :development
|
|
77
77
|
prerelease: false
|
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
|
79
79
|
requirements:
|
|
80
80
|
- - '='
|
|
81
81
|
- !ruby/object:Gem::Version
|
|
82
|
-
version: 10.
|
|
82
|
+
version: 10.3.2
|
|
83
83
|
- !ruby/object:Gem::Dependency
|
|
84
84
|
name: minitest
|
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
|
86
86
|
requirements:
|
|
87
87
|
- - '='
|
|
88
88
|
- !ruby/object:Gem::Version
|
|
89
|
-
version: 5.
|
|
89
|
+
version: 5.4.0
|
|
90
90
|
type: :development
|
|
91
91
|
prerelease: false
|
|
92
92
|
version_requirements: !ruby/object:Gem::Requirement
|
|
93
93
|
requirements:
|
|
94
94
|
- - '='
|
|
95
95
|
- !ruby/object:Gem::Version
|
|
96
|
-
version: 5.
|
|
96
|
+
version: 5.4.0
|
|
97
97
|
description: Garufa is a websocket server compatible with the Pusher protocol.
|
|
98
98
|
email:
|
|
99
99
|
- juanmacuello@gmail.com
|
|
@@ -106,21 +106,19 @@ files:
|
|
|
106
106
|
- README.md
|
|
107
107
|
- Rakefile
|
|
108
108
|
- bin/garufa
|
|
109
|
-
- bin/garufa.pid
|
|
110
109
|
- garufa.gemspec
|
|
111
110
|
- lib/garufa.rb
|
|
112
|
-
- lib/garufa/api
|
|
113
|
-
- lib/garufa/api/
|
|
111
|
+
- lib/garufa/api.rb
|
|
112
|
+
- lib/garufa/api/authentication.rb
|
|
113
|
+
- lib/garufa/api/event_handler.rb
|
|
114
114
|
- lib/garufa/config.rb
|
|
115
115
|
- lib/garufa/connection.rb
|
|
116
|
-
- lib/garufa/cuba/authentication.rb
|
|
117
116
|
- lib/garufa/garufa_app.rb
|
|
118
|
-
- lib/garufa/goliath/connection.rb
|
|
119
117
|
- lib/garufa/message.rb
|
|
120
118
|
- lib/garufa/subscription.rb
|
|
121
119
|
- lib/garufa/subscriptions.rb
|
|
122
120
|
- lib/garufa/version.rb
|
|
123
|
-
- lib/garufa/websocket
|
|
121
|
+
- lib/garufa/websocket.rb
|
|
124
122
|
- test/connection.rb
|
|
125
123
|
- test/helper.rb
|
|
126
124
|
- test/message.rb
|
|
@@ -139,9 +137,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
139
137
|
version: '1.9'
|
|
140
138
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
141
139
|
requirements:
|
|
142
|
-
- - "
|
|
140
|
+
- - ">"
|
|
143
141
|
- !ruby/object:Gem::Version
|
|
144
|
-
version:
|
|
142
|
+
version: 1.3.1
|
|
145
143
|
requirements: []
|
|
146
144
|
rubyforge_project:
|
|
147
145
|
rubygems_version: 2.2.2
|
data/bin/garufa.pid
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
11421
|
data/lib/garufa/api/api.rb
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
require 'cuba'
|
|
2
|
-
|
|
3
|
-
require 'garufa/cuba/authentication'
|
|
4
|
-
require 'garufa/api/handler'
|
|
5
|
-
|
|
6
|
-
module Garufa
|
|
7
|
-
module Api
|
|
8
|
-
|
|
9
|
-
Cuba.plugin Cuba::Authentication
|
|
10
|
-
|
|
11
|
-
Server = Cuba.new do
|
|
12
|
-
|
|
13
|
-
on "apps/:app_id" do |app_id|
|
|
14
|
-
|
|
15
|
-
authenticate
|
|
16
|
-
|
|
17
|
-
# Events
|
|
18
|
-
on post, "events" do
|
|
19
|
-
# Process requests deferred in order to response immediately.
|
|
20
|
-
EM.defer proc { Handler.handle_events(req.body.read) }
|
|
21
|
-
res.write "{}"
|
|
22
|
-
end
|
|
23
|
-
|
|
24
|
-
# Channels
|
|
25
|
-
on get, "channels" do
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
-
on get, "channels/:channel" do
|
|
29
|
-
end
|
|
30
|
-
|
|
31
|
-
# Users
|
|
32
|
-
on get, "channels/:channel/users" do
|
|
33
|
-
end
|
|
34
|
-
end
|
|
35
|
-
end
|
|
36
|
-
end
|
|
37
|
-
end
|
data/lib/garufa/api/handler.rb
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
require 'json'
|
|
2
|
-
|
|
3
|
-
require 'garufa/subscriptions'
|
|
4
|
-
require 'garufa/message'
|
|
5
|
-
|
|
6
|
-
module Garufa
|
|
7
|
-
module Api
|
|
8
|
-
module Handler
|
|
9
|
-
def self.handle_events(body)
|
|
10
|
-
message = Garufa::Message.new(JSON.parse(body))
|
|
11
|
-
options = { data: message.data, socket_id: message.socket_id }
|
|
12
|
-
Garufa::Subscriptions.notify message.channels, message.name, options
|
|
13
|
-
rescue JSON::ParserError
|
|
14
|
-
end
|
|
15
|
-
end
|
|
16
|
-
end
|
|
17
|
-
end
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
require 'signature'
|
|
2
|
-
require 'garufa/config'
|
|
3
|
-
|
|
4
|
-
class Cuba
|
|
5
|
-
module Authentication
|
|
6
|
-
def authenticate
|
|
7
|
-
request = Signature::Request.new(req.request_method, env['REQUEST_PATH'], req.params)
|
|
8
|
-
request.authenticate { |key| Signature::Token.new(key, Garufa::Config[:secret]) }
|
|
9
|
-
|
|
10
|
-
rescue Signature::AuthenticationError
|
|
11
|
-
halt([401, {}, ['401 Unauthorized']])
|
|
12
|
-
end
|
|
13
|
-
end
|
|
14
|
-
end
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
module Goliath
|
|
2
|
-
class Connection < EM::Connection
|
|
3
|
-
alias :goliath_post_init :post_init
|
|
4
|
-
|
|
5
|
-
# Ensure GOLIATH_ENV is set on new connections
|
|
6
|
-
#
|
|
7
|
-
# See https://github.com/postrank-labs/goliath/pull/279
|
|
8
|
-
#
|
|
9
|
-
def post_init
|
|
10
|
-
Thread.current[GOLIATH_ENV] = nil
|
|
11
|
-
goliath_post_init
|
|
12
|
-
end
|
|
13
|
-
end
|
|
14
|
-
end
|