Yea, you cannot run a loop like this in event driven embedded Ruby.
(1) The docs for this class’ constructor, clearly state:
Keeping a reference to the request is necessary in order to ensure the use of the response.
“Keeping a reference” means using the correct type of reference (also loosely called a variable,) that will not go out of scope and get garbage collected by Ruby’s housekeeping.
Local variables are temporary references and go out of scope quickly, such as when the method wrapping around them, finishes executing. When that happens, (the method ends) the
Sketchup::Http::Request instance object is no longer referenced, and will get GC’d.
If you have the code above in a script, at the top level, Ruby destroys local references when the script is finished loading. This safeguard is built-into the
require() methods, to prevent local variables from propagating into every object, (like everyone else’s classes and modules,) because the TOPLEVEL_BINDING, is
So you need to use a persistent reference, such as a instance variable (ie,
@request,) or a class/module variable (ie,
(2) You do not need to implement a looping construct, in order to watch the object for a responce.
It is already built-into the class. The
start method registers a block of callback code, to be executed when a HTTP Response has been received.
There needs to be a way better example, rather than having to stitch together all the method examples.