slanger 0.4.1 → 0.4.2

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.

Potentially problematic release.


This version of slanger might be problematic. Click here for more details.

@@ -1,64 +0,0 @@
1
- # encoding: utf-8
2
- require 'sinatra/base'
3
- require 'signature'
4
- require 'json'
5
- require 'active_support/core_ext/hash'
6
- require 'eventmachine'
7
- require 'em-hiredis'
8
- require 'rack'
9
- require 'fiber'
10
- require 'rack/fiber_pool'
11
-
12
- module Slanger
13
- class ApiServer < Sinatra::Base
14
- use Rack::FiberPool
15
- set :raise_errors, lambda { false }
16
- set :show_exceptions, false
17
-
18
- # Respond with HTTP 401 Unauthorized if request cannot be authenticated.
19
- error(Signature::AuthenticationError) { |c| halt 401, "401 UNAUTHORIZED\n" }
20
-
21
- post '/apps/:app_id/events' do
22
- authenticate
23
-
24
- # Event and channel data are now serialized in the JSON data
25
- # So, extract and use it
26
- data = JSON.parse(request.body.read.tap{ |s| s.force_encoding('utf-8')})
27
-
28
- # Send event to each channel
29
- data["channels"].each { |channel| publish(channel, data['name'], data['data']) }
30
-
31
- status 202
32
- return {}.to_json
33
- end
34
-
35
- post '/apps/:app_id/channels/:channel_id/events' do
36
- authenticate
37
-
38
- publish(params[:channel_id], params['name'], request.body.read.tap{ |s| s.force_encoding('utf-8') })
39
-
40
- status 202
41
- return {}.to_json
42
- end
43
-
44
- def payload(channel, event, data)
45
- {
46
- event: event,
47
- data: data,
48
- channel: channel,
49
- socket_id: params[:socket_id]
50
- }.select { |_,v| v }.to_json
51
- end
52
-
53
- def authenticate
54
- # authenticate request. exclude 'channel_id' and 'app_id' included by sinatra but not sent by Pusher.
55
- # Raises Signature::AuthenticationError if request does not authenticate.
56
- Signature::Request.new('POST', env['PATH_INFO'], params.except('captures', 'splat' , 'channel_id', 'app_id')).
57
- authenticate { |key| Signature::Token.new key, Slanger::Config.secret }
58
- end
59
-
60
- def publish(channel, event, data)
61
- Slanger::Redis.publish(channel, payload(channel, event, data))
62
- end
63
- end
64
- end