Can I do Sinatra program without restart server? - ruby

When I modify the code and have to restart server to see results. Have any way out?

There are a few options, detailed in the Sinatra FAQ. The simplest appears to be to use shotgun, which you can install and invoke as follows:
$ sudo gem install shotgun
$ shotgun myapp.rb
or if you use define your app by inheriting from Sinatra::Base and use a config.ru file:
$ shotgun config.ru -p 4567

If you use Phusion Passenger, you can put this file in the application’s root folder
tmp/always_restart.txt
and it will restart on every request.
http://www.modrails.com/documentation/Users%20guide%20Apache.html ( section 8.7 )

Better way is to use reloader from sinatra-contrib gem (also from Sinatra FAQ): First install sinatra-contrib gem, then ensure your application .rb file starts with these lines:
require 'sinatra'
require 'sinatra/reloader' if development?
And then any modified config files will be reloaded (no need to restart server!)

Related

Ruby script cannot load a gem installed via bundler

I am trying to include the ruby-mysql gem in my ruby script. I have installed the gem using bundler but when I run bundle exec ./mysql_connector, I receive the error ./mysql_connector:4:in ``require': cannot load such file -- ruby-mysql (LoadError). Can you please help me troubleshoot what the problem is?
What I did
Installed rails in my home directory.
I do not have root access to the server so I have installed rails in my local directory by following the instructions here:
http://www.r-bloggers.com/installing-ruby-on-linux-as-a-user-other-than-root/
Created a directory for my application.
My application resides in my home directory in a folder called connector. It has a Gemfile that looks like this:
source 'https://rubygems.org'
gem 'ruby-mysql'
Call bundle install.
Using ruby-mysql 2.9.14
Using bundler 1.11.2
Bundle complete! 1 Gemfile dependency, 2 gems now installed.
Bundled gems are installed into ./vendor/bundle.
Add dependencies to my script. My script is in connector/mysql_connector and it reads:
#!/home/dcox/bin/ruby
require 'rubygems'
require 'bundler/setup'
require 'ruby-mysql'
Make script executable. I saw that you need to run bundle exec using an executable file, so I followed the instructions here to make my script executable: http://commandercoriander.net/blog/2013/02/16/making-a-ruby-script-executable/
Run the script. I execute using bundle exec mysql_connector and see:
/home/dcox/bin/mysql_connector:4:in `require': cannot load such file -- ruby-mysql (LoadError)
from /home/dcox/bin/mysql_connector:4:in `<main>'
Is it the $LOAD_PATH? After searching around for answers, I discovered a lot of SO answers as well as a blog post (https://codedecoder.wordpress.com/2013/09/23/require-and-load-in-ruby-loaderror-cannot-load-such-file/) that seem to suggest the problem is that the gem is not installed in a directory on the $LOAD_PATH. Here is what I see when I run $LOAD_PATH from IRB:
irb(main):002:0> $LOAD_PATH
=> ["/home/dcox/lib/ruby/site_ruby/2.1.0",
"/home/dcox/lib/ruby/site_ruby/2.1.0/x86_64-linux",
"/home/dcox/lib/ruby/site_ruby", "/home/dcox/lib/ruby/vendor_ruby/2.1.0",
"/home/dcox/lib/ruby/vendor_ruby/2.1.0/x86_64-linux",
"/home/dcox/lib/ruby/vendor_ruby", "/home/dcox/lib/ruby/2.1.0",
"/home/dcox/lib/ruby/2.1.0/x86_64-linux"]
Next I checked to see the location of ruby-mysql:
dcox#analytics1:~/connector$ bundle show ruby-mysql
/data/home/dcox/connector/vendor/bundle/ruby/2.1.0/gems/ruby-mysql-2.9.14
Obviously my connector/vendor/bundle path is not on the $LOAD_PATH. I could add it, but I have a feeling I am missing something simple here because bundler is supposed to work as long as you follow the instructions, right?
Any advice or help is very much appreciated! Thanks!!
If you just want to require this specific gem, require 'mysql' should work (e.g., https://github.com/tmtm/ruby-mysql/blob/master/test/test_mysql.rb#L10).
Your file should call Bundler.setup http://bundler.io/bundler_setup.html
Better yet, if you instead call Bundler.require(:default) it will setup and require all the gems in your Gemfile for you.

Initializing an empty sinatra application

I am trying to create an empty Sinatra application with a config.ru file. Right now the only way I know how is to initialise it with cucumber but doing that creates extra files which I don't need including step definitions etc. When I run 'gem install Sinatra' in my working directory it says:
Successfully installed sinatra-1.4.5
Parsing documentation for sinatra-1.4.5
Done installing documentation for sinatra after 0 seconds
1 gem installed
Yet there is no config.ru file.
How do I initialise a Sinatra app so I have just the config.ru?
From the sinatra documentation
first create a file called app.rb in your working directory containing the following code:
require 'sinatra'
get '/' do
'Hello world!'
end
now create a file called config.ru with the following content:
require './app'
run Sinatra::Application
now run
rackup -p4567
to run the application

How to install and use Slim template engine with Middleman

I'm new to Middleman and ruby in general.
I've installed Ruby
I've installed Middleman and the gems to get it running.
I need to use slim instead of the default template system.
So I installed the Slim gem. Slim's website only says that I need to require 'slim' in order to get it to work.
The middleman website says I only need add the template engine to the config.rb file, but it gives no examples...
For someone with no ruby background, this is no help.
I looked for several config.rb on git and they all have:
require 'slim'
And
# Set slim-lang output style
Slim::Engine.set_default_options :pretty => true
# Set template languages
set :slim, :layout_engine => :slim
I added that to my config.rb file and created the layout.slim and the index.html.slim
When I refresh my local server I get:
Not Found
`/' not found.
I have middleman installed with Boilerplace. I'm not sure if there are more files that I need to change, but I can't find any good resources online, which is odd.
Could anyone give me some direction as to what I'm missing?
So here we go... after much reading and searching google for examples I think I figured it out.
To get Slim working with Middleman
Add gem "slim" to your project's gemfile
go to command line, in your project folder and gem install bundler
In the config.rb file add require 'slim'
Start the middleman server to test it
The middleman-slim project by yterajima is helpful in this regard.
Install is very easy.
$ gem install middleman
$ gem install middleman-slim
$ middleman init PROJECT_NAME --template slim
Bundler tip: you can also include multiple gems at once using Bundler.require. If you have gems in groups, you can include them as such: Bundler.require :group1, :group2 ...

Rackup: cannot load such file 'sinatra'

I already installed sinatra gem and in irb, if I type,
require 'sinatra'
It returns true.
But when I do
rackup -d config.ru
It tells me
nil
Exception `LoadError' at /usr/local/lib/ruby/1.9.1/rubygems/custom_require.rb:36 - cannot load such file -- sinatra
Here is my config.ru
require './app'
set :environment, ENV['RACK_ENV'].to_sym
set :app_file, 'app.rb'
disable :run
run Sinatra::Application
app.rb
require 'rubygems'
require 'sinatra'
get '' do
'Hello World'
end
I don't know what is going wrong.
$ which ruby
/usr/local/bin/ruby
$ which rackup
/usr/local/bin/rackup
$ ruby -v
ruby 1.9.3p374 (2013-01-15 revision 38858) [x86_64-linux]
$ rackup -v
Rack 1.2 (Release: 1.5)
I think this is just the verbose output from setting the -d option. Does the server actually start (after producing a load of output)?
What’s happening is this. Using -d sets Ruby’s $DEBUG flag to true. Rack then tries to load the app through the config.ru, which in turn loads your app.rb. The first thing in app.rb is require 'sinatra'.
Rubygems replaces the original require method with its own. When you call require it tries to load the file normally using the existing load path and the original require method. If the gem hasn’t been loaded this will raise a LoadError, which Rubygems catches before loading the gem.
With the $DEBUG flag set, Ruby will produce a message when an exception is raised even though it is rescued and dealt with, and this is what you’re seeing.
To avoid this simply omit the -d flag to your call to rackup (perhaps enabling warnings with -w would give you a sufficiently verbose output without swamping you in too much detail).
If the server isn’t starting then it will be a different issue rather than not finding Sinatra. If that is the case you’ll need to check the rest of the output for clues.
(Note that I originally thought something else was happening, and that’s what my questions int he comments were about.)
my guess is that your rackup script is a binstub of a 'rack' gem
installed in a diff ruby1.9x vm
maybe earlier version of ruby1.9.2
so it can't see the sinatra installed
I'd try 'which rackup' on the command line
This is definitely issue of load path. Anyway try to setup required ruby and gems via RVM and Bundler. It makes sure that Ruby interpreter and load paths are consistent.

How to get Sinatra to auto-reload the file after each change?

I am using
# my_app.rb
load 'index.rb'
and start the sever like this
ruby my_app.rb
but it never reload any changes I made in index page.
Did I miss anything here?
See the Sinatra FAQ,
"How do I make my Sinatra app reload on changes?"
First off, in-process code reloading in Ruby is hard and having a
solution that works for every scenario is technically impossible.
Which is why we recommend you to do out-of-process reloading.
First you need to install rerun if you haven’t already:
$ gem install rerun
Now if you start your Sinatra app like this:
$ ruby app.rb
All you have to do for reloading is instead do this:
$ rerun 'ruby app.rb'
If you are for instance using rackup, instead do
the following:
$ rerun 'rackup'
You get the idea.
If you still want in-process reloading, check out Sinatra::Reloader.
gem install sinatra-reloader
require 'sinatra'
require 'sinatra/reloader'
Note: it will reload only sinatra handlers (and, maybe some sinatra server configuration commands), but not custom files, which you have to reload manually.
UPD after 9 years: seems like it is already possible to reload other files using also_reload, dont_reload and after_reload -- https://github.com/sinatra/sinatra/pull/1150
You can use the rerun gem.
gem install rerun
rerun 'ruby app.rb'
OR if you are using rackup
rerun 'rackup'
gem install sinatra-reloader
require 'sinatra/base'
require "sinatra/reloader"
class MyApp < Sinatra::Base
register Sinatra::Reloader
get '/' do
"Hello Testing1!"
end
end
You may want to set environment variable to development and conditionally load the gem.
When you run the application with Passenger Standalone, just create a tmp/always_restart file:
$ touch tmp/always_restart.txt
See Passenger documentation for more info.
I like the Shotgun gem. If you're using a modular Sinatra app and have a config.ru file it's easy to run.
shotgun config.ru
Check the gem out here. It's fairly straight forward and no configuration needed.
On Windows, I am using my restart gem for this:
restart ruby my_app.rb
or, with rackup:
restart rackup
See here for more info, hope you find it useful.
You could use guard-rack. Lifted from an article at dblock.org:
Add this to your Gemfile:
group :development do
gem "guard"
gem "guard-bundler"
gem "guard-rack"
end
Then, create a Guardfile at the root of your project with this content:
guard 'bundler' do
watch('Gemfile')
end
guard 'rack' do
watch('Gemfile.lock')
watch(%r{^(config|app|api)/.*})
end
Lastly, run Guard, like so: bundle exec guard, and rackup will reload every time.
If you only change your templates sinatra will always rerender them if you set your environment to development:
ruby app.rb -e development

Resources