garufa 1.0.0 → 1.0.1.rc.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: feb8e6e2251eca7ffa865791866ced7d91434c87
4
- data.tar.gz: 306edcf627d01d05336297f9db7e4b8b241abfdb
3
+ metadata.gz: 51982c470639a14bcfaa9c7cdbcd139984b78d16
4
+ data.tar.gz: fb7cadeb7651dd64c4fdaf6f4eb2916a7a65f629
5
5
  SHA512:
6
- metadata.gz: 9655597ace7f0e79e59832e4b4ffa6644fa3d64409d4e82c992ddc46e1fb1088d9992346ea4ab746ca01854a6def3cc488bed4af6695a2e28e34cba545bbeb96
7
- data.tar.gz: 398b5e33ff82a0b8fef3bfe06ec35e9642120733beee835ac02344bb17bb40b49b65ef3347587dcab8c2a8f3a9043a39106a83ba0d175d72337b3fcc7f2ee724
6
+ metadata.gz: e6f57e7381827e211bb40cb05617fd5168a99a8b0ce29d76cb729f9273ad5fa6d8c7e07770e82c4765334fdd6b49fe50fb504e04e6da0a2ef99a4c2be1c9ef56
7
+ data.tar.gz: ce0e6afef6b42108ffb4336c519f5117cef23a0cf978a244976e98f0411f55ae014f17c924435ef08dee5a35b6850d98ef6d1580d7d2c89dc161914c4f2e9e7c
data/README.md CHANGED
@@ -1,5 +1,5 @@
1
1
  [![Build Status](https://travis-ci.org/Juanmcuello/garufa.png?branch=master)](https://travis-ci.org/Juanmcuello/garufa)
2
- [![Code Climate](https://codeclimate.com/github/Juanmcuello/garufa.png)](https://codeclimate.com/github/Juanmcuello/garufa)
2
+ [![Code Climate](https://codeclimate.com/github/Juanmcuello/garufa/badges/gpa.svg)](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 simple open it
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 on;
128
- ssl_certificate /path/to/cert.pem;
129
- ssl_certificate_key /path/to/cert.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.3"
29
- s.add_dependency "faye-websocket", "0.7.2"
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.1.0"
33
- s.add_development_dependency "minitest", "5.3.1"
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
@@ -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/websocket'
7
- require 'garufa/api/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
- Api::Server.call(env)
38
+ API::Server.call(env)
40
39
  end
41
40
  end
42
41
  end
@@ -3,7 +3,8 @@ require 'garufa/subscriptions'
3
3
 
4
4
  module Garufa
5
5
  class Subscription
6
- attr_reader :data, :connection, :error
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?
@@ -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
- connections = subscriptions[channel].map { |s| s.connection }
25
- next unless connections.any?
26
-
27
- connections.each do |connection|
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
@@ -1,4 +1,4 @@
1
1
  module Garufa
2
- VERSION = '1.0.0'
2
+ VERSION = '1.0.1.rc.1'
3
3
  end
4
4
 
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.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-07-09 00:00:00.000000000 Z
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.3
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.3
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.2
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.2
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.1.0
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.1.0
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.3.1
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.3.1
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/api.rb
113
- - lib/garufa/api/handler.rb
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/websocket.rb
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: '0'
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
@@ -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
@@ -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