How to pass data from ruby to distant web site (React)

Hi, I am searching for a way to pass data between Ruby and my website, but I haven’t been able to find a solution yet.

I attempted using dialog.add_action_callback , but it was not successful.

Can you please help me understand how to pass data to my remote website?

This is a JavaScript to Ruby connection, that can pass data (JavaScript Object → Ruby Hash) to Ruby from your webpage. But it can also just cause the Ruby-side to send data by some means to your webpage or webserver.

The simplest way is to take data from a Ruby Hash object convert it to JSON using Ruby’s JSON library and pass it to the dialog via SketchUp API’s UI::HtmlDialog#execute_script method.

Ruby, at top of your extension submodule:

require "json"

Ruby, in your dialog setup method:

@dialog.add_action_callback('send_data') do |_unused, params|
  # @data would be an instance variable to a Hash
  data = @data.to_json
  @dialog.execute_script( %[var data = JSON.parse('#{data}');] )

Now in your webpage you can use the data JS object.

But for more complex communication, you can use the SketchUp API’s Http::Request class to send the JSON data in the body of a request.

Likely you will set the "Content-Type" header to "application/json"

There are other topics here on using JSON with html dialogs. Search this forum category for more.

1 Like