File: streaming.rb

package info (click to toggle)
ruby-async-http 0.59.5-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 572 kB
  • sloc: ruby: 4,164; javascript: 40; makefile: 4
file content (35 lines) | stat: -rw-r--r-- 829 bytes parent folder | download | duplicates (2)
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
34
35

require 'trenni/template'

require 'async'
require 'async/http/body/writable'

# The template, using inline text. The sleep could be anything - database query, HTTP request, redis, etc.
buffer = Trenni::Buffer.new(<<-EOF)
The "\#{self[:count]} bottles of \#{self[:drink]} on the wall" song!

<?r self[:count].downto(1) do |index| ?>
	\#{index} bottles of \#{self[:drink]} on the wall,
	\#{index} bottles of \#{self[:drink]},
	take one down, and pass it around,
	\#{index - 1} bottles of \#{self[:drink]} on the wall.
	
	<?r Async::Task.current.sleep(1) ?>
<?r end ?>
EOF

template = Trenni::Template.new(buffer)

Async do
	body = Async::HTTP::Body::Writable.new

	generator = Async do
		template.to_string({count: 100, drink: 'coffee'}, body)
	end

	while chunk = body.read
		$stdout.write chunk
	end
	
	generator.wait
end.wait