How to show all the html files under a folder with ruby? - ruby

If use this way to show only index.html file under public folder, it works:
require 'rubygems'
require 'sinatra'
set :bind, '0.0.0.0'
get '/' do
File.read('index.html')
end
But want to show all files at the current folder, tried below but not work.
require 'rubygems'
require 'sinatra'
set :bind, '0.0.0.0'
get '/' do
File.read('*.html')
end
Got error:
Errno::ENOENT at /
No such file or directory # rb_sysopen - *.html

Use Dir.glob('*.html'). For example,
Dir.glob('*.html'){ |f| File.read f }
NOTE: Dir.glob('*.html').each{ } also works, but each is superfluous.

It is not necessary to use sinatra for this task, I think that you want some thing simlar to python simplehttpserver
Try with this aproximation:
In a console inside the folder try:
ruby -run -e httpd -- -p 5000
╭─ ~/learn/ruby/ruby-way/stackoverflow/q-static-html-007/html
╰─ tree
.
.
├── 1.html
└── 2.html

Related

Difference between '.' and File.dirname(__FILE__) in RUBY

I am working my way through a Lynda.com Ruby course and building a small app.
In order to make the file portable, the teacher uses this form below.
APP_ROOT = File.dirname(__FILE__)
I understand the purpose of the constant FILE as it always resolves the current file no matter what its name.
When I type p APP_ROOT it resolves to a . # => '.' which I understand represents this current directory.
What I don't get is why, if File.dirname(__FILE__) always == '.', why not just use '.' ?
For example the output of:
$LOAD_PATH.unshift( File.join(APP_ROOT, 'lib'))
p $:
is equal to
$LOAD_PATH.unshift( File.join('.', 'lib'))
p $:
when I p $: I get the same exact results for either line. What is the value of File.dirname(__FILE__) over '.' ?
Also, I searched all over but I'm not sure.
If I am in directory /home/one/apps
If I enter '.'
is that equal to the apps directory OR does that mean the entire absolute path including the final directory? so . is really /home/ones/apps not just /apps
Thanks in advance.
File.dirname(__FILE__) isn’t always the same as the current directory. It will be the same if you start your Ruby program from the same directory as the file, but will be different if you start from a different directory.
For example if you have a subdirectory subdir containing foo.rb with the contents:
puts File.dirname(__FILE__)
then running from parent directory you get this:
$ ruby subdir/foo.rb
subdir
but if you cd into the directory and then run the file you get this:
$ cd subdir
$ ruby foo.rb
.

How to run external Ruby scripts from a Sinatra application

I have a Ruby script in a folder outside of my Sinatra application that I would like to run with a button click.
This is app.rb file:
require 'sinatra'
get '/' do
erb :home
end
get '/launch_script' do
system("ruby path\\to\\file\\delete_rows_csv.rb")
end
This is home.erb file:
<a href='/launch_script'> Launch a Script </a>
Am I supposed to load a filepath or require the file to get this to work?
This is the solution that worked great:
require 'sinatra'
get '/' do
erb :home
end
get '/launch_script' do
load 'path\\to\\file\\delete_rows_csv.rb'
end
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

Ruby require module/class on another folder

I am new to Ruby and I'm trying to understand a good way to require modules or classes defines elsewhere. I have this setup:
test/
database/
base.rb
scripts/
run.rb
base.rb
module A
def hi
puts "It works"
end
end
run.rb
# I don't know how to require module A here
hi()
Now I know I can do something like: require "#{File.dirname(__FILE__)}/database/base" but that looks fragile. I want to know if there is a way to add a folder to the LOAD_PATH of a particular folder or the whole application.
I believe the following will work:
require_relative '../database/base'
Inside run.rb file, include A and then run file
It depends on from which file you are requiring other ruby file. Its relative to the host file.
In you situation
test/
database/
base.rb
scripts/
run.rb
I suppose you are tying to require the base.rb from run.rb then do this
require '../database/base.rb'
class SomeClass
include A
def some_method
hi()
end
end

ruby require_relative gives LoadError: cannot infer basepath inside IRB

I am currently in
Dropbox/96_2013/work/ror/dmc/dmStaffing/QA/selenium_server_wyatt/spec/2day/units/
I can go into irb and require a file but it's a really long require...
require '/home/durrantm/Dropbox/96_2013/work/ror/dmc/dmStaffing/QA/selenium_server_wyatt/spec/2day/units/login_as_admin_spec.rb'
=> true
I want to use require_relative, as in
$ cd /home/durrantm/Dropbox/96_2013/work/ror/dmc/dmStaffing/QA/selenium_server_wyatt/spec/2day/
$ pwd
/home/durrantm/Dropbox/96_2013/work/ror/dmc/dmStaffing/QA/selenium_server_wyatt/spec/2day
$ irb
irb(main):001:0> require_relative 'units/login_as_admin_spec.rb'
but I get:
LoadError: cannot infer basepath
require_relative requires a file relative to the file the call to require_relative is in. Your call to require_relative isn't in any file, it's in the interactive interpreter, therefore it doesn't work.
You can use the long form of require by explicitly passing the full path:
require './units/login_as_admin_spec.rb'
Or you add the current directory to the $LOAD_PATH and just require as usual:
$LOAD_PATH << '.'
require 'units/login_as_admin_spec'
This is a known bug in ruby:
Ruby bug #4487: require_relative fails in an eval'ed file
If you are using Pry, instead of IRB, this can be fixed by installing the pry-require_relative gem.
gem install pry-require_relative
This worked:
require File.expand_path("../login_as_admin_spec.rb", __FILE__)
require_relative works in the context of the current source file. This is different than the current working directory. I don't believe irb or pry have an understanding of "this current source file" concept; since you're not actually in a file.
In these REPLs, just use a relative path reference require './units/login_as_admin_spec.rb'.

sinatra and http PUT

suppose i want to use curl to put a file to a webservice this way
curl -v --location --upload-file file.txt http://localhost:4567/upload/filename
in sinatra i can do:
#!/usr/bin/env ruby
require 'rubygems'
require 'sinatra'
put '/upload/:id' do
#
# tbd
#
end
how can i read the streaming file?
more or less i want something like this:
http://www.php.net/manual/en/features.file-upload.put-method.php#56985
The most basic example is writing it to the currect directory you are running sinatra in with no checking for existing files ... just clobbering them.
#!/usr/bin/env ruby
require 'rubygems'
require 'sinatra'
put '/upload/:id' do
File.open(params[:id], 'w+') do |file|
file.write(request.body.read)
end
end
Also, you can leave off the filename portion in the curl command and it will fill it in for you with the filename. Foe example:
curl -v --location --upload-file file.txt http://localhost:4567/upload/
will result in writing the file to http://localhost:4567/upload/file.txt
require 'rubygems'
require 'sinatra'
require 'ftools'
put '/upload' do
tempfile = params['file'][:tempfile]
filename = params['file'][:filename]
File.mv(tempfile.path,File.join(File.expand_path(File.dirname(File.dirname(__FILE__))),"public","#{filename}"))
redirect '/'
end
In this way, you does not have to worry about the size of the file, since he's not opened (readed) in memory but just moved from the temp directory to the right location skipping a crucial blocker. In fact, the php code do the same stuff, read the file in 1k chunks and store in a new file, but since the file its the same, its pointless.
To try you can follow the answer of Ben.

Resources