Class: Gameworks::Servlet::AddObserver

Inherits:
Base
  • Object
show all
Defined in:
lib/gameworks/servlet/add_observer.rb

Defined Under Namespace

Classes: DeferrableBody

Constant Summary collapse

AsyncResponse =
[-1, {}, []].freeze

Instance Method Summary collapse

Methods inherited from Base

#initialize, #method_not_allowed, process

Constructor Details

This class inherits a constructor from Gameworks::Servlet::Base

Instance Method Details

#POST(request) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/gameworks/servlet/add_observer.rb', line 25

def POST(request)
  game_id = request[:path].split('/')[1]
  game = @server.game_registry.instance(game_id)
  return [404, {}, ["no such game"]] unless game
  wrapper = (request[:payload] || {})['wrapper']
  body = DeferrableBody.new(wrapper)
  token = game.register_observer(body)

  # application/x-multi-json if a format i just made up,
  # which is a series of json documents separated by newlines
  # (there will be no newlines within the json document)
  request[:async_cb].call [200,
    {'Content-Type' => 'application/x-multi-json',
     'X-Observer-Token' => token}, body]
  body.call(game.to_json)
  AsyncResponse
end