How to call a ruby script from webpage and output the results? - html

Okay so maybe you guys can advise me. Here is what I am attempting to do.
I am trying to call my ruby script from a html form on a website that has already been created.
The form needs to pass a variable entered in a text box to the ruby script.
Once the script is finished (it's doing a sql query) it needs to output the results to the webpage.
What do you guys think this is the best way to go about doing this? I looked at Rails but the webpage is already created and I don't want to create full scale webapp. I also looked at Rack, but I am not sure if that is the best option. Thanks!

Ruby code is executed in a RVM (MRI, JRUBY, etc). It is served by a Webserver(NGINX, Apache, etc) which processes it by a RVM WebServer stack(CGI/Phussion Passenger) and passes it through Ruby middleware (Rack) and associated frameworks (Sinatra, Rails, etc).
Some WebServers have RVM Stacks built into them Puma, Unicorn, etc.
Using nginx to proxy-pass a specific request path from a url to a RVM enabled webstack + sinatra app would work fine. You might include the uri/pages as an iframe to inject into your current server as well.
A simple example of exposing a sinatra app running in a rvm unicorn stack through nginx.
https://github.com/p8952/nginx-unicorn-sinatra

Related

Can the Trinidad server use log files in development?

I'm working on an app that's served using Trinidad, and in development, the server defaults to pushing all of its output to stdout. This is making it very difficult to use a command line debugger (in my case Pry). Is there some way to make it use log files in dev the way that it does in prod? For reference, I'm using version 1.4.4 of Trinidad.
Alternatively, if there's some workaround for this in Pry, I'd love to learn about that too.
Thanks!
It turns out this was actually due to a logger monkey patch in my specific app, which was otherwise using log files. I was just thrown off by the Trinidad README on GitHub, which claims that printing to stdout is the default in dev.
If anyone has clever ideas for working around a situation like this using Pry and/or Unix utilities, please share anyway as that might be useful in other similar scenarios.

How can I use ReactJS as a static website?

I want to develop my static web application with React. I have just done with the Tic-tac-toe getting started tutorial. Are there anyways I can "compile" (or whatever the term is) ReactJS straight into my HTML file? So, far to run that ReactJS application I need to run it with a server from Yarn.
I prefer to not use CDN because I want to update and manage the dependencies.
Let say, for my starting point, I want to have that Tic-tac-toe game from official React getting started tutorial to be served with just one HTML file (CSS and JS in that one HTML file). I want to see if this is possible or not, so I don't care about the best practice for this question.
You should have a look at Gatsby JS
It's a static site generator for React. Probably that's what you're looking for.
Run npm run build or yarn build and see the output in the build folder. It generates static HTML, CSS and Javascript.
Sounds like you are using Yarn with create-react-app. If so you are running:
yarn start
Now run:
yarn build
See more info here: https://github.com/facebookincubator/create-react-app
The static web application will be built to a build folder. You will find all of the static assets there like JS, CSS and HTML.
I have tested. yarn build works only with a server. Even python3 -m http.server works. So, no! ReactJS downloaded from non-Bower package manager will not work without server out-of-the-box.

Testing local website with Protractor

I am developing simple website (html, css) and I want to write few tests against it in Protractor.
Is it possible to check that kind of website with Protractor? I have html file on my computer only. Should I run this website locally on server? I don't think I can run test on file directly.
Yes, its possible to open a stand-alone local html file without hosting it on a local server.You have to add this browser.resetUrl = "file:///"in your onPrepare() function and then a browser.get("file:///C:/Users/demo/test.html") would work
There are some good examples #this question - Opening a file with protractor
A common way to do it is to approach it in multiple steps:
build your application
run a local web server from the build directory (I think this is the part you are asking about)
run tests against your app served by the local web server
stop the local web server
Usually, this multi-step process is handled by a task runner like grunt or gulp. We use grunt, grunt-contrib-connect to serve from a directory, grunt-protractor-runner to run protractor from grunt.

Running Ruby script with HTML button?

I have a Ruby script that I run through the Command Line locally. Now I want to run that script through a browser, with a simple HTML button. Is there any way to make a simple HTML page and run the script similar to a JS onclick event? OR will I have to set up a rails environment first?
To run a Ruby script, you need a Ruby interpreter. JavaScript can be run locally by the browser that executes the HTML page, Ruby no.
Therefore, your cannot run the Ruby code directly in the browser. There are several possible solutions.
You can host the Ruby script somewhere, and have the HTML page reference it. If the script is reasonably simple, you can use one of those services that run a Ruby script in a sandbox.
But if it's a Rails app, no way. You need to host it somewhere.
Short answer, you need a server/computer with the Ruby interpreter to execute your script. It can't be executed by the client browser.
There are a few options here. Depending on your use case the most likely two are:
Convert your Ruby script into JavaScript via Opal so that it can run in the browser.
Set up a simple server with Sinatra and run it on a server somewhere, then call it via AJAX.
There are couple of options available for your use case but I would use Sinatra for this. Here is a very simple example for you.
your_file.rb
require 'sinatra'
get '/' do
"<a href='/another-file'> Link to another file </a>"
end
get '/another-file' do
"Today is #{Time.now}"
end
If you do not have sinatra gem installed then please try
gem install sinatra
Once done then you can run your script on your desire port (I'm using 5000)
ruby your_file.rb -p 5000
Now you can browse your simple app at http://localhost:5000. Please checkout sinatra website for complete documentation.

Browser-based app framework for Ruby

I've a ruby script running out of the command line. I want to provide a local GUI for it (for my use). As I have some exposure to Sinatra and other web frameworks, I want to use HTML pages as my front-end. But I don't want to start a server and type in a URL every time I want to launch my app.
My solution would be to write a shell script which will start a Sinatra based server and then launch Chromium(Browser) in app mode to that url.
Is there some framework which can do it better/cleaner?
I'm not interested in learning a non-HTML framework like Shoes or Ruby-Gnome2.
#!/bin/sh
ruby $1 &
chromium localhost:4567
Put that somewhere in your $PATH (or change it to contain $HOME/bin with export PATH=$HOME/bin:$PATH and put it there), make it executable with chmod +x <file> and have fun by calling <file> <sinatra startup file>
You could extend this to read the port from Sinatra, but that would require a ruby startup, and this should do in most of the cases (the 80%, as people call it).