File: timeout_server.rb

package info (click to toggle)
ruby-curb 0.8.6-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 604 kB
  • ctags: 880
  • sloc: ansic: 4,242; ruby: 2,768; makefile: 3
file content (33 lines) | stat: -rw-r--r-- 810 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# This Sinatra application must be run with mongrel
# or possibly with unicorn for the serve action to work properly.
# See http://efreedom.com/Question/1-3669674/Streaming-Data-Sinatra-Rack-Application

require 'sinatra'

get '/wait/:time' do |time|
  time = time.to_i
  sleep(time)
  "Slept #{time} at #{Time.now}"
end

# http://efreedom.com/Question/1-3027435/Way-Flush-Html-Wire-Sinatra
class Streamer
  def initialize(time, chunks)
    @time = time
    @chunks = chunks
  end
  
  def each
    @chunks.each do |chunk|
      sleep(@time)
      yield chunk
    end
  end
end

get '/serve/:chunk_size/every/:time/for/:count' do |chunk_size, time, count|
  chunk_size, time, count = chunk_size.to_i, time.to_i, count.to_i
  chunk = 'x' * chunk_size
  chunks = [chunk] * count
  Streamer.new(time, chunks)
end