Class: Slanger::ApiServer

Inherits:
Sinatra::Base
  • Object
show all
Defined in:
lib/slanger/api_server.rb

Instance Method Summary collapse

Instance Method Details

#authenticateObject



51
52
53
54
55
56
# File 'lib/slanger/api_server.rb', line 51

def authenticate
  # authenticate request. exclude 'channel_id' and 'app_id' included by sinatra but not sent by Pusher.
  # Raises Signature::AuthenticationError if request does not authenticate.
  Signature::Request.new('POST', env['PATH_INFO'], params.except('channel_id', 'app_id')).
    authenticate { |key| Signature::Token.new key, Slanger::Config.secret }
end

#payload(channel, event, data) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/slanger/api_server.rb', line 42

def payload(channel, event, data)
  {
    event:     event,
    data:      data,
    channel:   channel,
    socket_id: params[:socket_id]
  }.select { |_,v| v }.to_json
end

#publish(channel, event, data) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/slanger/api_server.rb', line 58

def publish(channel, event, data)
  f = Fiber.current

  # Publish the event in Redis and translate the result into an HTTP
  # status to return to the client.
  Slanger::Redis.publish(channel, payload(channel, event, data)).tap do |r|
    r.callback { f.resume [202, {}, "202 ACCEPTED\n"] }
    r.errback  { f.resume [500, {}, "500 INTERNAL SERVER ERROR\n"] }
  end

  Fiber.yield
end