How to integrate rest client in skecthup api


#1

I’m pretty new in ruby and sketchup, and I want to create an extension, in which I have to use rest client . I have installed on my machine Ruby and also installed rest client using this command

gem install rest-client -v 1.8.0

from this site

I’ve tested it in the following way

require 'rest_client'

class Register
  def self.SendData()
       RestClient.post('http://localhost:1334/ControllerName/ActionName', :name_of_file_param => File.new('C:\Users\UserName\Desktop\test.txt')) 
  end
end

and it worked fine.

The problem is when I want to run sketchup it gives me an error

   Error Loading File Exporter.rb
   Error: #<LoadError: cannot load such file -- rest_client>
   C:/Program Files/SketchUp/SketchUp                     
   2018/Tools/RubyStdLib/rubygems/core_ext/kernel_require.rb:54:in `require`
   C:/Program Files/SketchUp/SketchUp     
   2018/Tools/RubyStdLib/rubygems/core_ext/kernel_require.rb:54:in `require`
   C:/Users/karen/AppData/Roaming/SketchUp/SketchUp     
   2018/SketchUp/Plugins/AugPlugin/Register.rb:2:in `<top (required)>`
   C:/Program Files/SketchUp/SketchUp     
   2018/Tools/RubyStdLib/rubygems/core_ext/kernel_require.rb:54:in `require`
   C:/Program Files/SketchUp/SketchUp     
   2018/Tools/RubyStdLib/rubygems/core_ext/kernel_require.rb:54:in `require`
   C:/Users/karen/AppData/Roaming/SketchUp/SketchUp     
   2018/SketchUp/Plugins/Exporter.rb:4:in `<top (required)>`

I suppose that I need to integrate rest client into my extension, but I don not know how to do that correctly.

Is there any example or link where I can see how to do that? Or maybe the problem is something else.


#2

you would need to instal the gem into the SU ruby gem library which may or may not work…

Gem.install 'rest_client'

but, you will likely see Unable to resolve dependency: message…

I spent this morning trying to get a webrick server to work before I remembered that all I needed was to use :set_url pointed at a local file and SU runs as a server…

john


#3

See this recent topic:


#4

Thanks for all your replies. I just understood from the shared topic that in my case the best way is getting rid of external dependencies. But I jus want to ask is there a way to post a file using Net::HTTP ?
I want to post a file as multipart/form-data .


#5

Standard library Net classes are synchronous and blocking within an embedded environment.

Seek to use the SketchUp API’s new Sketchup::Http classes (with SketchUp 2017 and newer.)

They are asynchronous and non-blocking. You can use a upload callback …

module SomeCompany
  module SomePlugin

    def upload_form_data(data)

      @request = Sketchup::Http::Request.new(
        "http://www.somedomain.com:8080", Sketchup::Http::POST
      )

      @request.headers= {
        'Content-Type' => 'multipart/form-data'
      }

      @request.body= data

      @request.set_upload_progress_callback do |current, total|
        puts "Uploading : #{current} of #{total}"
      end

      @request.start do |request, response|
        puts "Upload Complete."
        puts "http status code: #{response.status_code}"
        puts "body: #{response.body}"
      end

    end

  end
end

If you need to encode form data there are some libraries that will do this.
The Ruby standard URI library can.


#6

Thanks a lot.