I have seen this question asked before, but I am curious as to accomplish this using sinatra. Recognizing that sinatra already has a streaming method, I assume the solution is already 80% complete.
These are some similiar questions for reference:
Generic redirect stdout: Streaming stdout to a web page
Using sinatra to stream stdout (not working for me): Sinatra 1.3 Streaming w/ Ruby stdout redirection
Here's an example of what I am trying to accomplish(simplified for clarity):
require 'sinatra'
require 'thin'
get '/' do
stream do |out|
out << method_that_puts
end
end
def method_that_puts
puts 'I would like...'
sleep 1.0
puts 'to display this...'
sleep 1.0
puts 'on a web page!'
end
EDIT: The perfect example of this is travis-ci which is also built with sinatra. They are redirecting stdout to the build page... How is this possible?
UPDATE:
Thanks to the help so far, I have a halfway solution working. I am currently saving $stdout
to a new instance of StringIO
and displaying it afterwards. BUT, this isn't the answer. For long running scripts (ie. travis-ci build) it would be ridiculous to wait for entire script to complete then display the $stdout
. Need to figure out how to stream it as it comes...
Here's what I have so far:
foo = StringIO.new
$stdout = foo
get '/' do
stream do |out|
method_that_puts
out.puts $stdout.string
end
end
def method_that_puts
puts 'I would like...'
sleep 1.0
puts 'to display this...'
sleep 1.0
puts 'on a web page!'
end