Run Ruby Script That Is Stored on Internet

Run Ruby script that is stored on internet

To run the ruby program located on website, you can try:

curl -s http://wwww.example.com/script.rb | ruby

-s option to curl is for silent mode so that there is no additional output from curl command.

How do I run a ruby script from a webpage?

In general, to run off simple commands like that, you can run ruby scripts via the terminal:

ruby /path/to/ruby/script.rb

Otherwise, if you insist on using a webserver for such purpose (or, if your use-case requires so), you should look at simple ruby-based web servers/frameworks like rack, sinatra, etc.

Particularly, in rack, you can save the following file (lets, say as: hello.rb):

require 'rubygems'
require 'rack'

def application(env)
[200, {"Content-Type" => "text/html"}, "Hello world."]
end

Rack::Handler::Mongrel.run method(:application), :Port => 9292

Then, you can run the above file using terminal:

ruby /path/to/hello.rb

And, this will create a local web server with port 9292, and you can open the above page at: http://127.0.0.1:9292/, and the page will say: Hello world.

remotely launching a ruby script via a webpage

There are a bunch of different ways to launch a subprocess from ruby-- system, exec, backticks, open3. They're all slightly different depending on how you want to interact with it exactly-- this answer to a similar question contains a handy flowchart. I'm not sure which works best on which version of Windows with which version of Ruby; you should search with each one of these options and your versions to find out.

Within a controller action in your rails app, you can call one of these to execute your ruby script, ex:

require 'open3'

class WhateverController < ActionController
def start
Open3.popen3('ruby your_script.rb') {|stdin, stdout, stderr, wait_thr|
# do whatever you need to with the output streams here
}
render nothing: true # So that the request returns to the user who pushed the button
end
end

I'd recommend making this a POST request, making sure you have authorization on it, possibly keeping track of the last time anyone ran this if it takes a long time to run and you don't want people pushing the button a lot, etc.

Using a Ruby script to login to a website via https

Mechanize

Mechanize is ruby library which imititates the behaviour of a web browser. You can click links, fill out forms und submit them. It even has a history and remebers cookies. It seems your problem could be easily solved with the help of mechanize.

The following example is taken from http://docs.seattlerb.org/mechanize/EXAMPLES_rdoc.html:

require 'rubygems'
require 'mechanize'

a = Mechanize.new
a.get('http://rubyforge.org/') do |page|
# Click the login link
login_page = a.click(page.link_with(:text => /Log In/))

# Submit the login form
my_page = login_page.form_with(:action => '/account/login.php') do |f|
f.form_loginname = ARGV[0]
f.form_pw = ARGV[1]
end.click_button

my_page.links.each do |link|
text = link.text.strip
next unless text.length > 0
puts text
end
end

How to run external Ruby scripts from a Sinatra application

You can use require or require_relative (depending if your file is located in a static place or relative to this file). Adding .rb is not required at the end either.

require 'sinatra'

get '/' do
erb :home
end

get '/launch_script' do
require 'path\\to\\file\\delete_rows_csv'
end

run ruby script in rails application

If you want to call a script from a rails app it gets complex. You would want to use a background job or some sort of queue to run these jobs because they do block the server and your users would be waiting for the call to complete and the results to load, most likely hitting a timeout.

See delayed_job
and you might want to try creating a small wrapper script in ruby that can interface with your application.

Good luck!



Related Topics



Leave a reply



Submit