pusher 0.5.3 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -38,6 +38,15 @@ To avoid blocking in a typical web application, if you are running inside eventm
38
38
  # error is a pusher exception
39
39
  }
40
40
 
41
+ Private channels
42
+ -----------------------
43
+ The Pusher Gem also deals with signing requests for authenticated private channels. A quick Rails controller example:
44
+
45
+ reponse = Pusher['private-my_channel'].authenticate(params[:socket_id])
46
+ render :json => response
47
+
48
+ Read more about private channels in [the docs](http://pusherapp.com/docs/private_channels).
49
+
41
50
  Copyright
42
51
  ---------
43
52
 
data/Rakefile CHANGED
@@ -15,6 +15,7 @@ begin
15
15
  gem.add_dependency "crack"
16
16
  gem.add_dependency "ruby-hmac"
17
17
  gem.add_dependency 'signature'
18
+ # gem.add_dependency 'em-http-request', ">= 0.2.7"
18
19
  gem.add_development_dependency "rspec", ">= 1.2.9"
19
20
  gem.add_development_dependency "webmock"
20
21
  # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.3
1
+ 0.6.0
@@ -63,5 +63,6 @@ module Pusher
63
63
  end
64
64
  end
65
65
 
66
+ require 'pusher/json'
66
67
  require 'pusher/channel'
67
68
  require 'pusher/request'
@@ -61,17 +61,33 @@ module Pusher
61
61
  rescue StandardError => e
62
62
  handle_error e
63
63
  end
64
-
65
- def socket_auth(socket_id)
64
+
65
+ # Auth string is:
66
+ # if custom data provided:
67
+ # socket_id:channel_name:[JSON-encoded custom data]
68
+ # if no custom data:
69
+ # socket_id:channel_name
70
+ def socket_auth(socket_id, custom_string = nil)
66
71
  raise "Invalid socket_id" if socket_id.nil? || socket_id.empty?
72
+ raise 'Custom argument must be a string' unless custom_string.nil? || custom_string.kind_of?(String)
67
73
 
68
- string_to_sign = "#{socket_id}:#{name}"
74
+ string_to_sign = [socket_id, name, custom_string].compact.map{|e|e.to_s}.join(':')
69
75
  Pusher.logger.debug "Signing #{string_to_sign}"
70
76
  token = Pusher.authentication_token
71
77
  signature = HMAC::SHA256.hexdigest(token.secret, string_to_sign)
72
78
 
73
79
  return "#{token.key}:#{signature}"
74
80
  end
81
+
82
+ # Custom data is sent to server as JSON-encoded string in the :data key
83
+ # If :data present, server must include it in auth check
84
+ def authenticate(socket_id, custom_data = nil)
85
+ custom_data = Pusher::JSON.generate(custom_data) if custom_data
86
+ auth = socket_auth(socket_id, custom_data)
87
+ r = {:auth => auth}
88
+ r[:channel_data] = custom_data if custom_data
89
+ r
90
+ end
75
91
 
76
92
  private
77
93
 
@@ -0,0 +1,15 @@
1
+ require 'json'
2
+
3
+ module Pusher
4
+ module JSON
5
+
6
+ def self.generate(data)
7
+ if Object.const_defined?('ActiveSupport')
8
+ ActiveSupport::JSON.encode(data)
9
+ else
10
+ ::JSON.generate(data)
11
+ end
12
+ end
13
+
14
+ end
15
+ end
@@ -1,6 +1,5 @@
1
1
  require 'signature'
2
2
  require 'digest/md5'
3
- require 'json'
4
3
 
5
4
  module Pusher
6
5
  class Request
@@ -17,7 +16,7 @@ module Pusher
17
16
  data
18
17
  else
19
18
  begin
20
- self.class.turn_into_json(data)
19
+ Pusher::JSON.generate(data)
21
20
  rescue => e
22
21
  Pusher.logger.error("Could not convert #{data.inspect} into JSON")
23
22
  raise e
@@ -31,12 +30,5 @@ module Pusher
31
30
  @query = params.merge(auth_hash)
32
31
  end
33
32
 
34
- def self.turn_into_json(data)
35
- if Object.const_defined?('ActiveSupport')
36
- ActiveSupport::JSON.encode(data)
37
- else
38
- JSON.generate(data)
39
- end
40
- end
41
33
  end
42
34
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{pusher}
8
- s.version = "0.5.3"
8
+ s.version = "0.6.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["New Bamboo"]
12
- s.date = %q{2010-06-01}
12
+ s.date = %q{2010-08-09}
13
13
  s.description = %q{Wrapper for pusherapp.com REST api}
14
14
  s.email = %q{support@pusherapp.com}
15
15
  s.extra_rdoc_files = [
@@ -19,13 +19,13 @@ Gem::Specification.new do |s|
19
19
  s.files = [
20
20
  ".document",
21
21
  ".gitignore",
22
- "CHANGELOG",
23
22
  "LICENSE",
24
23
  "README.md",
25
24
  "Rakefile",
26
25
  "VERSION",
27
26
  "lib/pusher.rb",
28
27
  "lib/pusher/channel.rb",
28
+ "lib/pusher/json.rb",
29
29
  "lib/pusher/request.rb",
30
30
  "pusher.gemspec",
31
31
  "spec/channel_spec.rb",
@@ -235,5 +235,52 @@ describe Pusher::Channel do
235
235
  }.should raise_error
236
236
  end
237
237
  end
238
+
239
+ describe 'with extra string argument' do
240
+
241
+ it 'should be a string or nil' do
242
+ lambda {
243
+ @channel.socket_auth('socketid', 'boom')
244
+ }.should_not raise_error
245
+
246
+ lambda {
247
+ @channel.socket_auth('socketid', 123)
248
+ }.should raise_error
249
+
250
+ lambda {
251
+ @channel.socket_auth('socketid', nil)
252
+ }.should_not raise_error
253
+
254
+ lambda {
255
+ @channel.socket_auth('socketid', {})
256
+ }.should raise_error
257
+ end
258
+
259
+ it "should return an authentication string given a socket id and custom args" do
260
+ auth = @channel.socket_auth('socketid', 'foobar')
261
+
262
+ auth.should == "12345678900000001:#{HMAC::SHA256.hexdigest(Pusher.secret, "socketid:test_channel:foobar")}"
263
+ end
264
+
265
+ end
266
+ end
267
+
268
+ describe '#authenticate' do
269
+
270
+ before :each do
271
+ @channel = Pusher['test_channel']
272
+ @custom_data = {:uid => 123, :info => {:name => 'Foo'}}
273
+ end
274
+
275
+ it 'should return a hash with signature including custom data and data as json string' do
276
+ Pusher::JSON.stub!(:generate).with(@custom_data).and_return 'a json string'
277
+
278
+ response = @channel.authenticate('socketid', @custom_data)
279
+
280
+ response.should == {
281
+ :auth => "12345678900000001:#{HMAC::SHA256.hexdigest(Pusher.secret, "socketid:test_channel:a json string")}",
282
+ :channel_data => 'a json string'
283
+ }
284
+ end
238
285
  end
239
286
  end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 5
8
- - 3
9
- version: 0.5.3
7
+ - 6
8
+ - 0
9
+ version: 0.6.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - New Bamboo
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-06-01 00:00:00 +01:00
17
+ date: 2010-08-09 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -103,13 +103,13 @@ extra_rdoc_files:
103
103
  files:
104
104
  - .document
105
105
  - .gitignore
106
- - CHANGELOG
107
106
  - LICENSE
108
107
  - README.md
109
108
  - Rakefile
110
109
  - VERSION
111
110
  - lib/pusher.rb
112
111
  - lib/pusher/channel.rb
112
+ - lib/pusher/json.rb
113
113
  - lib/pusher/request.rb
114
114
  - pusher.gemspec
115
115
  - spec/channel_spec.rb
data/CHANGELOG DELETED
@@ -1,11 +0,0 @@
1
- * master *
2
-
3
- * Pusher v0.5.1 (20th May, 2010) *
4
-
5
- * Use ActiveSupport::JSON.encode instead of #to_json to avoid conflicts with JSON gem. [OL]
6
- * Support loading Pusher credentials from a PUSHER_URL environment variable. [ML]
7
-
8
- * Pusher v0.5.2 (25th May, 2010) *
9
-
10
- * Bugfix: required uri correctly
11
- * Bugfix: channel objects not being reused