Forum: Ruby ruby on rails or sinatra for dynamic browser application

Posted by LMH medchem (lmhmedchem)
on 2012-12-13 21:21
I need to write a simple html file or web application that will allow me
to display the contents of a text file in a browser. This will involve
some formatting and eventually some dynamic features like sorting. This
is tabular data, so I am more or less creating a spreadsheet display in
a browser. I don't know how to display a variable amount of data in
html. I can hard code things, but for this application, the number of
rows and columns will be variable.

I want to use ruby because I am using ruby to access a sqlite database
from which the text file is created. Eventually the text file will go
away and the application will just show data that is queried from a
database. It makes sense to use ruby for the dynamic code on the webpabe
if I am using it to access the database. I know that both ruby on rails
and sinatra are used to create web pages, but I don't know much more
than that.

Can someone point me to a tutorial or example of how to do something
similar? Basically the application needs to open a text file (possibly a
browse button on the webpage to give it a path), read the file into
variables, and then use the variables to populate rows in a display.

Am I on the right track here?

LMHmedchem
Posted by Carlo E. Prelz (Guest)
on 2012-12-13 21:45
(Received via mailing list)
Subject: ruby on rails or sinatra for dynamic browser application
  Date: Fri 14 Dec 12 05:21:27AM +0900

Quoting LMH medchem (lists@ruby-forum.com):

> away and the application will just show data that is queried from a
> Am I on the right track here?
If I were you, I would not pull in rails for a simple thing -
especially if you do not plan to have many queries. Webrick (the
pure-ruby web server included in the Ruby source) works
very well.

Remember that you do not need to serve HTML from a web server: simple
text files may be served as well.

Here is a short example about how you may serve text files:

--8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<--
require 'webrick'
require 'webrick/httpserver'

class Webserver < WEBrick::HTTPServer
  PORT=12345

  def initialize
    super(:Port=>PORT)
    mount_proc('/',method(:content).to_proc())
  end

  def content(req,res)
    res['content-type']='text/plain'
    res.body= <<-EOF
This is
your content
EOF
    res.status=WEBrick::HTTPStatus::RC_OK
  end
end

Webserver::new().start()
--8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<--

Basically, whenever you access port 12345 of your computer,
method 'content' is called, which here returns a hardcoded text - but
the text could be whatever.

Carlo
Posted by Brian Candler (candlerb)
on 2012-12-13 22:07
Carlo E. Prelz wrote in post #1089024:
> If I were you, I would not pull in rails for a simple thing -
> especially if you do not plan to have many queries. Webrick (the
> pure-ruby web server included in the Ruby source) works
> very well.

I would recommend Sinatra instead, because it sits on top of a number of 
webservers (including Webrick), making it more flexible, and it hides 
the low-level detail.

However, start by writing a program which opens your file, iterates 
through the lines and writes it to stdout (i.e. forget about the web 
part). This should be easy enough if you're already using ruby.

Then to convert it to sinatra, you just need to append your results to a 
string instead.

require 'sinatra'
get '/' do
  res = ""
  res << "<table>\n"
  res << "<tr><td>Hello</td></tr>\n"
  res << "</table>\n"
  res
end

If you want to continue using "puts" you can even use StringIO:

require 'sinatra'
require 'stringio'
get '/' do
  r = StringIO.new
  r.puts "<table>"
  r.puts "<tr><td>Hello</td></tr>"
  r.puts "</table>"
  r.string
end

Then if you want to use a query string to control the output (e.g. to 
select which columns to sort by), look at the Sinatra docs for params.

http://www.sinatrarb.com/intro
Posted by 7stud -- (7stud)
on 2012-12-14 11:04
LMH medchem wrote in post #1089022:
>
> I don't know how to display a variable amount of data in
> html.
>

data1 = [1, 2, 3]
data2 = ['a', 'b']

data1.each do |item|
  puts "<div>#{item}</div>"
end

data2.each do |item|
  puts "<div>#{item}</div>"
end

--output:--
<div>1</div>
<div>2</div>
<div>3</div>
<div>a</div>
<div>b</div>
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.