Correct gemconnection when installing the plug-in

Greetings! An analytics gem has been added to the plugin, looking for a method of how to install it in case it is missing I found this solution for example:

begin
  require "zip"
rescue
  Gem.install("rubyzip")
  require "zip"
end

But, on first run it still gives me an error that the library is not installed. What am I doing wrong, I don’t want to put the library in the plugin.

Error load file «C:/Users/gwynn/AppData/Roaming/SketchUp/SketchUp 2021/SketchUp/Plugins/dev/modules/main.rb»
Error: #<LoadError: cannot load such file -- mixpanel-ruby>

After relaunche mixpanel-ruby gem work normal. But i need fix message on first load)

The “ruby-zip” gem is NOT a good example for other normal gems because it’s has abnormal naming.

Please show us the actual code that produces an error when you post problem scenarios.

No problem, but it doesn’t make much difference.
It is called at the start of the plugin. And as described above, the first time I run it, I get an error that the package is not installed. Although in the folder I see that it was downloaded, after restarting everything is fine.

I do not want to put it locally in the plugin, but this option is also considered.

# frozen_string_literal: true
# Include system
require 'sketchup.rb'
# Init starter module
begin
  require 'mixpanel-ruby'
  rescue
  Gem.install 'mixpanel-ruby'
  require 'mixpanel-ruby'
end

Note that we don’t recommend relying on the Gem system within SketchUp for a number of reasons. (More details in this post: Install Prawn gem in SketchUp 2016 - #2 by tt_su)

If it’s an internal extension then you’re probably fine, but if you are going to publish it then it’d be best to bundle the gems you need in your extension under your own extension namespace. (This is required if you want to host the extension on EW.)

2 Likes

Just a note that the above statement does not “include any system” whatsoever.
It is simply a ruby script that defines a few global methods in the top level ObjectSpace.

None of these few methods are anything special and it is advisable to use your own internal code for these functions.

The "sketchup.rb" file from the "Tools" folder has already been loaded by SketchUp before any extension begins to load so it is not necessary to have a require for it, unless you are using a code editor along with the API stubs. (In the latter case the "sketchup.rb" file from the stubs repository will load the rest of the stub files.)

The reason the above produces an exception …

Error: #<LoadError: cannot load such file -- mixpanel-ruby>

… and then fails to install the gem, is because rescue with no arguments does not trap a LoadError class exception as it is not a StandardError subclass exception.

See the primer on exceptions … File: exceptions.rdoc [Ruby 2.7.2] … which tells you that rescue by default only traps StandardError and it’s subclass exceptions.

So you would need to do this:

begin
  require 'mixpanel-ruby'
rescue LoadError
  puts "Attempting to install the 'mixpanel-ruby' gem ..."
  Gem.install 'mixpanel-ruby'
  require 'mixpanel-ruby'
end

Which installs and then loads fine into SketchUp 2021 (even though I’m not familiar with what it does.)

1 Like

Thank you, yes my mistake I removed “LoadError” during the tests. Because with it the library is loaded, but the program crashes without errors. Then everything works as it should.

Now I will take advice tt_su and will connect the library locally and deliver it with the plugin.

This library is part of the analytics suite, and serves to send events. Since the extension analytics does not provide statistics on downloads or run, and for a better understanding of the user experience - I plugged an external analytics system.

Okay I understand what it does now. It might be using the Net HTTP from Ruby’s Standard library, so you may have the opportunity to tweak it to use the SketchUp API’s Http classes which are asynchronous.

Do you mean edits to the gem itself? Unfortunately this is a public library, I won’t be able to fix it. Only to connect it locally, which has been difficult so far. So, I will study further, perhaps just rewrite this part of the plugin and will not use third-party libraries at all.

Thank you very much, you are very helpful.

Yes … we do mean you need to edit it anyway to wrap it within your extension submodule, so you could replace calls to Net:HTTP with calls to Sketchup::Http classes.