How to invoke watir scripts from a web application? - ruby

I have a watir script ,that i have to run from a web interface, on the same browser invoking the scripts. Is it possible?

This isn't possible to do, as a browser doesn't understand what ruby is.

I believe that it is possible. I've been intending to begin a similar project to what adithi wanted to do at some point, using PHP. From PHP, you can use the shell_exec command or backtick operator to execute commands in the shell. The ruby returns output using puts, which can be read by the PHP.
http://chipmunkninja.com/Program-Execution-in-PHP%3A-exec-m# seems to be a good page detailing this technique.

Related

Ruby (not Rails) on IIS 7 via FastCGI

Hopefully this is a simple one. I want to run Ruby on IIS 7+ as I would PHP, Perl, Python, etc. (that is to set a handler mapping to .rb files).
Every time I Google for it I get Rails. But I don't want to run Rails, I would just like to run straight Ruby.
I know I'll to set up my headers and all sorts of other stuff, since, unlike PHP, Ruby isn't technically native to the web.
I'm looking for a manual installation for this, not something automated with an executable.
My reason for needing this is simply for learning.
Any answers would be very helpful! Thanks!

Ruby as a scripting language for web server

Is it possible to use Ruby as a scripting language with a HTTP server ? I'd like to be able to simply put some Ruby files in a web directory and be able to execute them from my browser - just like I did with PHP.
I have absolutely nothing against frameworks such as RoR, but I was told that I should first learn Ruby and only then move on with higher level frameworks. Of course, I could write some Ruby scripts and run them in the console, but I would prefer getting the input/output from my browser :)
Is that possible at all ? Otherwise, how hard would it be for me to build a quick and simple web framework ?
Take a look at Sinatra - not your plain ol' CGI/PHP style, but really really simple web framework to get started with, elegant so it won't get in your way while learning Ruby, and powerful enough to make quite useful web apps.
ERB is similar in spirit to PHP (in the sense you need).
You need to setup apache to parse rhtml files with erb, here is a guide for OSX.
Depends on your server, but any language can be used with CGI Programming, including Ruby.
Generally speaking - to find out - put a basic "Hello World" .rb or .cgi file on your web server and chmod it's permissions so it is executable to "others":
chmod 755 YourScript.rb
When writing CGI scripts, you have to ensure that you have the appropriate shebang at the top of your file. For Ruby you'd probably use something like...
#!/usre/bin/ruby
(no promises this is the one)
Then write yourself a little hello world CGI script. The output of a CGI script is kind of a partial HTTP Response: (I'm not really a Ruby coder so this might be completely arse-backwards)
puts "Content-Type:text/plain\n"
puts "Content-Length:12\n"
puts "\n"
puts "Hello World!"
Visit the file the same way you would a PHP or Perl script and you'll see if it works. Again; it depends on your server.
You can do that configuring your server for CGI.
You can even write web applications with assembler if you want to, using CGI.
(In the dawn of time, they used C/C++ to write web applications, go figure).

How would you go about making an online IRB that runs in the browser

Im wondering how to go about creating an online IRB that runs in the browser. I have an idea to include an irb console in my blog and give the option for users to send code blocks in my tutorials directly into the irb console so they can play around with it.
_Why did this previously, but of course it is gone now: Cached Version
TryRuby is still available here, with source code at GitHub.
Well, you could use the sandbox that _why created. But you'll need to be able to patch your ruby and it seems to only work on ruby 1.8.5 .
That's insanely dangerous. Don't do this. You expose your system to all sorts of vulnerabilities when you allow users to execute arbitrary Ruby code.
Anyway there are some client-side Ruby implementations in JavaScript/Flash. Take a look at HotRuby.
I would suspect you run Ruby in a sandbox to prevent "bad" commands being run.

Ruby server-side back end scripting tools/resources

I am trying to learn how ruby is used in a server based back end environment. For example I want to be running a ruby script 24/7 on a server. What are the best practices for this and how does one go about doing this?
Can anyone provide some resources on how to do this or if you could label what I am trying to do? I am unsure of the terms that I am supposed to be googling.
Use cron. From OS point of view Ruby app is just a script like bash.
Also all Unix OSes have some kind of daemon script (like see examples in /etc/init.d)
Try BackgroundRb - this is a special Rails plugin that works like a Linux daemon. You could use any classes/models defined in your Rails application within the background code. You could also pass data to/from background process.

How to remotely control Firefox from a script on OS X

I need to write some scripts that access some websites. A script from the command line would get some pages, post some forms, screen-scrape some information, etc.
It cannot really be a library "browser" like libwww-perl, because some steps might require user interactions (CAPTCHAs, Ajax-only forms, any interaction surprises, etc.).
The most practical way I can think of would be remotely opening a tab in Firefox, and injecting JavaScript code into it, something a bit like what Greasemonkey and Selenium do. It doesn't necessarily have to be for Firefox and can be a different browser if that's easier.
So what would be the best way to do that?
Have you considered Selenium Remote Control? I've automated browser interaction using the tool before and it works very well, providing a lot of flexibility
Depending on your exact needs, you might be able to leverage the Selenium IDE which is an easy to use Firefox plugin that allows easy scripting.
You can use XPCOM to extend Firefox in every way imaginable. You could write some kind of interface that connects with another process maybe.
I'm not sure what the "best" way to do it would be, but one possibility would be to use AppleScript for the job. Firefox, however, doesn't have extensive scripting capabilities—if you are willing to use Safari, there is an AppleScript command available to inject JavaScript code into a document (the do JavaScript command—look it up in Safari's scripting dictionary, available from within Script Editor).
Also, in order to run AppleScripts from the command line, use osascript:
osascript path/to/script.scpt
To write srcripts on OS X there are two ways I would recommend, and both of them are in ruby. The first is Watir which is an automated testing framework that will control both firefox and safari on Mac os x.
Another, prehaps better way for screen scraping would be to use hpricot which is a html parser that is really easy to use.
In the background Watir uses JSSh - a TCP/IP JavaScript Shell Server for Firefox to do this is. JSSH allows you you control the browser from a telnet session.
Whichever way you go, if ther eare catchpa's they will stop you though. It's sort of the whole point of them :-)

Resources