Sha256: 06d9235b359831c522d84c505e283282c809639257ff0acf15bc1fc2aaa845d9

Contents?: true

Size: 1.22 KB

Versions: 5

Compression:

Stored size: 1.22 KB

Contents

require "goliath/api"

module SlideEmUp
  class RemoteAPI < Goliath::API
    def initialize(key)
      # Secret token...
      @key  = key

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

    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

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

  protected

    def stream_events(env)
      env['subscription'] = @chan.subscribe do |msg|
        env.stream_send("data: #{msg}\n\n")
      end
      streaming_response(200, {"Content-Type" => "text/event-stream"})
    end

    def forbidden
      [403, {
        "Content-Type"   => "text/plain",
        "Content-Length" => "10"
      }, ["Forbidden\n"]]
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
slide-em-up-0.2.4 lib/slide-em-up/remote_api.rb
slide-em-up-0.2.3 lib/slide-em-up/remote_api.rb
slide-em-up-0.2.2 lib/slide-em-up/remote_api.rb
slide-em-up-0.2.1 lib/slide-em-up/remote_api.rb
slide-em-up-0.2.0 lib/slide-em-up/remote_api.rb