1

I'm developing an application in Ruby. It is used at the command line and has a lot of options, some of them fairly complex. I'd like to provide the ability to use the application through a web interface.

So I'm asking the general question of the best way to do that, or if there might be a better way to do this.

The idea I'm currently working with is that the application would fork itself and use WEBrick to create an http server on some 1024+ port. Then, in the parent process, it would launch the user's web browser using sensible-browser, sending the browser to localhost:port.

The user uses the web application. When they click some particular button (probably "save") then the server would send a final page and shut itself down. That last message would include a message telling the user to close the browser.

The parent process sees that the browser and server processes have closed and proceeds to use the information that was saved to some local file.

All that sounds rather complicated, which is why I'm not crazy about the idea. However, I'm setting out to create a really easy interface, which means not requiring the user to learn a bunch of command line parameters (though I probably will provide those too).

I can't find any gems that provides cursors-based forms, which is why I'm considering the web browser concept.

Opinions?

tscheingeld
  • 789
  • 7
  • 24
  • There are bindings to most GUI toolkits. The question is also a bit: who are your users, what system(s) do they run and how do they access your program? How is it distributed? – Felix Dec 06 '17 at 17:05

1 Answers1

0

If I'm understanding everything properly, one thing you might want to consider is breaking out the command line interface and the web-based (or desktop GUI-based) interface. So, for example, if I'm creating a calculator CLI, which you use in the fashion of

$ myscript -a 2 3

where -a is for the "add" function and then you might have -s, -d, and -m (subtract, divide, multiply)...not very friendly having to remember that for the average Joe user. You could create a class such as

class Calculator
  class<<self
    def perform(operation, left_side, right_side)
      # figure out from operation what I need to do and do it or delegate it
    end
  end
end

and then you create a CLI script

#!/usr/bin/env ruby
require 'calculator'

# parse options, couple ways to do this, you probably already have one in mind
operation_option = ... # check for -a or -s or -d or -m
left_side_option = ...
right_side_option = ...

Calculator.perform(operation_option, left_side_option, right_side_option)

and now you can also create a web based (or desktop based) interface for it that also just calls out to the calculator class:

require 'calculator'

# read from text fields/drop downs/whatever
operation_option = ...
left_side_option = ...
right_side_option = ...

Calculator.perform(operation_option, left_side_option, right_side_option)

This way is a tiny little bit of duplication, but not really...either way you would need to parse the text fields from the interface and just instead of writing them to an intermediate file that the CLI then reads from, you just bypass the CLI and go straight to the code you want to execute. So you have 2 (or more) interface files that are just responsible for showing (if applicable) and reading from their specific interface and passing the data to the "work horse" class in a common format.

In terms of what to make the second interface, Ruby does have support for curses (I have a gem installed by that name) though you'll need to build the forms and interactions on your own (as far as I'm aware, I've never looked for gems to help make forms easier in curses) and a desktop based one is probably better than writing a web based thing, from your description (ruby does have a fair number of GUI gems, but I've never used any except for the curses one), but at this point it's all just "what's right for your project".

Simple Lime
  • 10,790
  • 2
  • 17
  • 32