pusher 1.0.0 → 1.1.0

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: 41c533b3af72b8bbc59b837e057140c199df3dde
4
- data.tar.gz: 72c2170cf91722d54cee192da0afda69f3304462
3
+ metadata.gz: 6daeeb10cfc7c29540cc4640cda4379f08c97a0a
4
+ data.tar.gz: d1387f40e14a45e68c5b7c2ba457d68d0e6f04e8
5
5
  SHA512:
6
- metadata.gz: 7991ee28bfe11b60a97fae9deaec6896fd538c45747ece3804d6db77f4b01aaedc4a407a3f4d77c5b1a3d12c7eac2365c9262ce3da1f2cad3f70652f0ce459ae
7
- data.tar.gz: 02eb45ed9d8b1ed4a899084a2eca8912facd311214b45f6131b83c721a12681246b28733d394c25cbff730c1e5005d9a81878687557855949ed832c8d81aec2e
6
+ metadata.gz: 68870356440d4b596c15ad6a51d65769d89cb036e782cffaedccc9f47d4d4707020c4e02671919ccd3c519019da9bb02505a6bfa9739e7e96fe358e38adfef4c
7
+ data.tar.gz: 3eeaa5528655dd1fc071e93adff493a39fc21843aea95f38b9076f8f9aebbb025f5b68afcd06f82bea733f985fa29f5e07611305051d5cc7bc4b8b04b95471af
@@ -1,4 +1,9 @@
1
1
 
2
+ 1.1.0 / 2016-05-20
3
+ ==================
4
+
5
+ * Add support for batch events
6
+
2
7
  1.0.0 / 2016-05-19
3
8
  ==================
4
9
 
data/README.md CHANGED
@@ -131,6 +131,18 @@ An optional fourth argument may be used to send additional parameters to the API
131
131
  Pusher.trigger('channel', 'event', {foo: 'bar'}, {socket_id: '123.456'})
132
132
  ```
133
133
 
134
+ #### Batches
135
+
136
+ It's also possible to send multiple events with a single API call (max 10
137
+ events per call on multi-tenant clusters):
138
+
139
+ ``` ruby
140
+ Pusher.trigger_batch([
141
+ {channel: 'channel_1', name: 'event_name', data: { foo: 'bar' }}
142
+ {channel: 'channel_1', name: 'event_name', data: { hello: 'world' }}
143
+ ])
144
+ ```
145
+
134
146
  #### Deprecated publisher API
135
147
 
136
148
  Most examples and documentation will refer to the following syntax for triggering an event:
@@ -269,6 +269,21 @@ module Pusher
269
269
  post('/events', trigger_params(channels, event_name, data, params))
270
270
  end
271
271
 
272
+ # Trigger multiple events at the same time
273
+ #
274
+ # POST /apps/[app_id]/batch_events
275
+ #
276
+ # @param events [Array] List of events to publish
277
+ #
278
+ # @return [Hash] See Pusher API docs
279
+ #
280
+ # @raise [Pusher::Error] Unsuccessful response - see the error message
281
+ # @raise [Pusher::HTTPError] Error raised inside http client. The original error is wrapped in error.original_error
282
+ #
283
+ def trigger_batch(*events)
284
+ post('/batch_events', trigger_batch_params(events.flatten))
285
+ end
286
+
272
287
  # Trigger an event on one or more channels asynchronously.
273
288
  # For parameters see #trigger
274
289
  #
@@ -276,6 +291,13 @@ module Pusher
276
291
  post_async('/events', trigger_params(channels, event_name, data, params))
277
292
  end
278
293
 
294
+ # Trigger multiple events asynchronously.
295
+ # For parameters see #trigger_batch
296
+ #
297
+ def trigger_batch_async(*events)
298
+ post_async('/batch_events', trigger_batch_params(events.flatten))
299
+ end
300
+
279
301
  # Generate the expected response for an authentication endpoint.
280
302
  # See http://pusher.com/docs/authenticating_users for details.
281
303
  #
@@ -351,23 +373,27 @@ module Pusher
351
373
  channels = Array(channels).map(&:to_s)
352
374
  raise Pusher::Error, "Too many channels (#{channels.length}), max 10" if channels.length > 10
353
375
 
354
- encoded_data = case data
355
- when String
356
- data
357
- else
358
- begin
359
- MultiJson.encode(data)
360
- rescue MultiJson::DecodeError => e
361
- Pusher.logger.error("Could not convert #{data.inspect} into JSON")
362
- raise e
376
+ params.merge({
377
+ name: event_name,
378
+ channels: channels,
379
+ data: encode_data(data),
380
+ })
381
+ end
382
+
383
+ def trigger_batch_params(events)
384
+ {
385
+ batch: events.map do |event|
386
+ event.dup.tap do |e|
387
+ e[:data] = encode_data(e[:data])
388
+ end
363
389
  end
364
- end
390
+ }
391
+ end
365
392
 
366
- return params.merge({
367
- :name => event_name,
368
- :channels => channels,
369
- :data => encoded_data,
370
- })
393
+ # JSON-encode the data if it's not a string
394
+ def encode_data(data)
395
+ return data if data.is_a? String
396
+ MultiJson.encode(data)
371
397
  end
372
398
 
373
399
  def configured?
@@ -1,9 +1,7 @@
1
1
  # -*- encoding: utf-8 -*-
2
- $:.push File.expand_path("../lib", __FILE__)
3
-
4
2
  Gem::Specification.new do |s|
5
3
  s.name = "pusher"
6
- s.version = "1.0.0"
4
+ s.version = "1.1.0"
7
5
  s.platform = Gem::Platform::RUBY
8
6
  s.authors = ["Pusher"]
9
7
  s.email = ["support@pusher.com"]
@@ -274,6 +274,37 @@ describe Pusher do
274
274
  end
275
275
  end
276
276
 
277
+ describe '#trigger_batch' do
278
+ before :each do
279
+ @api_path = %r{/apps/20/batch_events}
280
+ stub_request(:post, @api_path).to_return({
281
+ :status => 200,
282
+ :body => MultiJson.encode({})
283
+ })
284
+ end
285
+
286
+ it "should call correct URL" do
287
+ expect(@client.trigger_batch(channel: 'mychannel', name: 'event', data: {'some' => 'data'})).
288
+ to eq({})
289
+ end
290
+
291
+ it "should convert non string data to JSON before posting" do
292
+ @client.trigger_batch(
293
+ {channel: 'mychannel', name: 'event', data: {'some' => 'data'}},
294
+ {channel: 'mychannel', name: 'event', data: 'already encoded'},
295
+ )
296
+ expect(WebMock).to have_requested(:post, @api_path).with { |req|
297
+ parsed = MultiJson.decode(req.body)
298
+ expect(parsed).to eq(
299
+ "batch" => [
300
+ { "channel" => "mychannel", "name" => "event", "data" => "{\"some\":\"data\"}"},
301
+ { "channel" => "mychannel", "name" => "event", "data" => "already encoded"}
302
+ ]
303
+ )
304
+ }
305
+ end
306
+ end
307
+
277
308
  describe '#trigger_async' do
278
309
  before :each do
279
310
  @api_path = %r{/apps/20/events}
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pusher
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pusher
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-05-19 00:00:00.000000000 Z
11
+ date: 2016-05-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: multi_json