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.
- checksums.yaml +4 -4
- data/README.md +44 -1
- data/lib/slanger/api.rb +5 -0
- data/lib/slanger/api/event.rb +15 -0
- data/lib/slanger/api/event_publisher.rb +24 -0
- data/lib/slanger/api/request_validation.rb +103 -0
- data/lib/slanger/api/server.rb +56 -0
- data/lib/slanger/channel.rb +19 -7
- data/lib/slanger/connection.rb +2 -1
- data/lib/slanger/handler.rb +3 -3
- data/lib/slanger/presence_channel.rb +1 -2
- data/lib/slanger/service.rb +1 -1
- data/lib/slanger/version.rb +1 -1
- data/slanger.rb +4 -0
- data/spec/have_attributes.rb +64 -0
- data/spec/integration/channel_spec.rb +114 -0
- data/spec/integration/integration_spec.rb +68 -0
- data/spec/integration/presence_channel_spec.rb +158 -0
- data/spec/integration/private_channel_spec.rb +79 -0
- data/spec/integration/replaced_handler_spec.rb +23 -0
- data/spec/integration/ssl_spec.rb +18 -0
- data/spec/server.crt +12 -0
- data/spec/server.key +15 -0
- data/spec/slanger_helper_methods.rb +109 -0
- data/spec/spec_helper.rb +43 -0
- data/spec/unit/channel_spec.rb +69 -0
- data/spec/unit/request_validation_spec.rb +64 -0
- data/spec/unit/webhook_spec.rb +43 -0
- metadata +88 -38
- data/lib/slanger/api_server.rb +0 -64
data/lib/slanger/api_server.rb
DELETED
@@ -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
|