Initializing an empty sinatra application - ruby

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

Related

Unable to push sinatra app to heroku

having trouble pushing my sinatra app to heroku based on the current setup. I have looked through all of the docs on heroku to figure this out but alas, i'm without resolution and continue to get the following error. No default language could be detected for this app.
app
--.git
-- public
-- views
- app.rb
- config.ru
- gemfile
- Gemfile.lock
- Procfile
- foo.csv
Procfile
web: bundle exec ruby app.rb -p $PORT
config.ru
require './app'
run Sinatra::Application
Gemfile
source 'https://rubygems.org'
ruby "2.4.0"
gem 'sinatra'
app.rb
require 'rubygems'
require 'sinatra'
require 'csv'
Tilt.register Tilt::ERBTemplate, 'html.erb'
set :public_folder, 'public'
get "/" do
erb :index
end
Update
Still not quite sure what the issue was here. I ended up grabbing the following sinatra/heroku shell from github, and replaced it with the assets from my app. Once I ran a bundle and pushed to heroku everything ended up working. I'm sure the issue is structurally nuanced and can be pulled from a deeper look at the working setup, for now - i'm just happy it's working.
https://github.com/runemadsen/Sinatra-Heroku-Template

Rake tasks won't run inside of my sinatra app

I have a sinatra app with a Rakefile.rake and server.rb file. In the server.rb file I have
get '/' do
rake :test
end
the app loads up but crashes when I load localhost:4567/ and says undefined method 'rake' for sinatra application. I try and require the file by using
require '/home/user/project/Rakefile'
and I also try Rakefile.rake but both give me an error that reads "require cannot load such file." I'm using Ubuntu.
I'm not sure why sinatra can't load the rakefile, but when I run rake test in terminal that works.
Any help would be great.
get '/' do
system 'rake test'
end

What is the differenence between ruby app.rb and rackup

What is the difference between running ruby app.rb and rackup? When running rackup, what script is actually running?
App.rb is Sinatra's front controller and can be call whatever you choose to call it
rackup is used to start rack based application servers it defaults to config.ru when no config file is supplied

Accessing the irb in a modular Sinatra application

I am building an application which subclasses Sinatra like so:
require 'rubygems'
require 'sinatra/base'
require 'sinatra/assetpack'
class App < Sinatra::Base
...
run!
end
How can I access irb? Options are not parsed when executing sinatra this way, how do I programmatically open an irb shell?
Just type as below (at the shell prompt):
irb -r ./my_app.rb
I'm a little confused whether you want to open an IRB session from within your app (?) or use IRB to debug your Sinatra project?
For debugging Rack-based apps (such as Sinatra), I like using the racksh gem, which "is like script/console in Rails" for Rack applications. Its main advantage over IRB is that racksh loads the entire application environment into the shell, making debugging a breeze.
From racksh's Github page: "It's purpose is to allow developer to introspect his application and/or make some initial setup. You can for example run DataMapper.auto_migrate! or make a request to /users/666 and check response details. It's mainly aimed at apps that don't have console-like component (ie. apps built with Sinatra) but all frameworks can benefit from interactive Rack stack and request introspection."
However, racksh requires your app to have a config.ru file, so you would have to re-write your app:
# in config.ru
require 'rubygems'
require 'sinatra/base'
require 'sinatra/assetpack'
require 'app.rb'
# in app.rb
class App < Sinatra::Base
...
run!
end
Then in your app folder (where config.ru resides):
$ gem install racksh # or add gem 'racksh' to your Gemfile and run bundle
$ racksh
Check this simple search interface for Microsoft's Bing using Sinatra and binger gem.
If you follow the instructions from there you will understand better.
First at all, create a Gemfile and add:
source "https://rubygems.org"
gem 'sinatra'
gem 'binger'
Then run the bundle command that will generated Gemfile.lock.
Then create a config.ru file, and add by example:
require 'rubygems'
require 'bundler'
Bundler.require
require './app.rb'
run MyApp
Your app.rb could look like this:
class MyApp < Sinatra::Base
get '/' do
#title = "Index"
erb:index
end
end
You must have a folder named views. Create index.erb and add:
< % = #title % >
Finally, run rackup.
Source: https://github.com/thinkphp/sinatra-bing
Demo: http://sinatra-bing.herokuapp.com/

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