Class: SlideEmUp::RemoteAPI

Inherits:
Goliath::API
  • Object
show all
Defined in:
lib/slide-em-up/remote_api.rb

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ RemoteAPI

Returns a new instance of RemoteAPI.



6
7
8
9
10
11
12
# File 'lib/slide-em-up/remote_api.rb', line 6

def initialize(key)
  # Secret token...
  @key  = key

  # Share channel between connections
  @chan = ::EM::Channel.new
end

Instance Method Details

#on_close(env) ⇒ Object



31
32
33
34
35
# File 'lib/slide-em-up/remote_api.rb', line 31

def on_close(env)
  return unless env['subscription']
  env.logger.info "Stream connection closed."
  @chan.unsubscribe(env['subscription'])
end

#response(env) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/slide-em-up/remote_api.rb', line 14

def response(env)
  path_info = Rack::Utils.unescape(env['PATH_INFO'])
  slash, key, action = path_info.split('/', 3)

  # Events are public
  return stream_events(env) if 'events' == action

  # Sending events is restricted
  return forbidden unless key == @key

  @chan.push(action)
  [200, {
    "Content-Type"   => "text/plain; charset=utf-8",
    "Content-Length" => Rack::Utils.bytesize(action).to_s
  }, [action]]
end