Downloading from Ruby API and Mac OSX

Hi there ! :wink:

I have a small problem which is due to OSX behavior with Ruby here: I have a Ruby function that is supposed to download a file from the web. The code is as follows:

def download_file_url(url,output_dir,filename)
	ext = url.split('.')[-1]
	output_path = output_dir+'/'+filename+'.'+ext
	File.open(output_path, "wb") do |saved_file|
		# the following "open" is provided by open-uri
		open(url, "rb") do |read_file|
			saved_file.write(read_file.read)
		end
	end
	return(output_path)
end

This works like a charm on Windows but Mac OSX just says the file with path “url” does not exist. Does anyone know of a workaround, either software-wise, hardware-wise or code-wise?

PS: I already have a code that can detect the current running OS, I can use that.

Are you sure that you are actually requiring the open-uri rb ?
http://ruby-doc.org/stdlib-2.0.0/libdoc/open-uri/rdoc/OpenURI.html
Another script might already have done so on your PC, but not on your MAC.
Also what’s the path you are passing ?
Is it properly constructed as in their examples ?
Also passing ‘rb’ as a second argument seems wrong to me ??

Well, I did not include open-uri, as I didn’t need it with the PC…

Besides that, I am just using the regular open function, so…

what’s the file type your trying to download?

can you provide an example url to test against?

john

You do need to include this in your code

require 'open-uri'

If some other script is also requiring it for you on the PC, but not on the MAC, then that might be confusing the issue ?
This applies to PC and MAC - many additional Ruby methods do not auto-load with SketchUp, they need to be explicitly required before you try to use them.

The ‘open’ function that you use is part of openURI so it’s not a File.open etc…

Well, here’s a test URL: https://www.bimandco.com/mapping-files/SketchUp_main.txt

Yeah, maybe an additional plugin was actually loading the script for me. I’ve now added the require 'open-uri' code, just waiting after a way to now test the behabior on Mac.

Also, after multiple tests, I’ve found out that previous versions of SketchUp (I’m using 2016) weren’t downloading the file either. This solved the issue for SketchUp 2015 but not SketchUp 2014. I’m looking in SketchUp 2014 to see what kind of console behavior it has (There’s a begin/rescue code in the main plugin)

EDIT: OK in SketchUp 2014 it’s an SSL-related problem. Not sure if I can solve that.

EDIT2: Now works on Mac! :slight_smile: Thanks for the help

if they’re text files why not use a webDialog and extract the text…

 OSX = ( Object::RUBY_PLATFORM =~ /darwin/i ? true : false ) unless defined? OSX
 
 dlg = UI::WebDialog.new("Mapping Files", true, "", 850, 100, 150, 150, true);
 dlg.set_url "https://www.bimandco.com/mapping-files/SketchUp_main.txt"
 OSX ? dlg.show_modal() : dlg.show()
 
 dlg.add_action_callback('tell_su'){|_, txt| p txt } #or save as file

 wait4dlg = (UI.start_timer(1.0, false) {
   dlg.execute_script(%[
     var val = document.body.innerText;
     window.location = 'skp:tell_su@' + val;
    ])
         
    UI.stop_timer(wait4dlg)
    dlg.close
  })

Let’s just say that in my head, WebDialogs were something which display in the UI ^^ And besides, I was also using this function to download picture files as well

only if there ‘on screen’, you can set them off screen…

john

Yeah, now it seems evident ^^

But then there is also a limit to the length of location strings on PC.

You might find Net::Http more convenient to download files.

http://ruby-doc.org/stdlib-2.0.0/libdoc/net/http/rdoc/index.html