Now, let's take a little more professional approach by adding a Gemfile to the application. In the same folder as the other two files, let's add the Gemfile
with the following contents:
source :rubygems gem 'sinatra'
And now we simply bundle this together and run it:
$ bundle install
...
$ bundle exec rackup config.ru
INFO WEBrick 1.3.1
INFO ruby 1.9.2 (2011-07-09) [i386-darwin9.8.0]
INFO WEBrick::HTTPServer#start: pid=16574 port=9292
This is now a full-fledged setup.
Now, let's see how we can add Mongoid to this application. We need to simply add models to the application. In other words, just require these model files. Here are the changes we make to the Gemfile:
source :rubygems gem 'sinatra' gem 'mongoid' gem 'bson' gem 'bson_ext'
As we have included Mongoid, let's also include the Mongoid models. But first, let's create the models in the models
directory:
$ mkdir models
And let's add some models. We can add the Author, Book
, and Category
models as follows:
# models/author.rb class Author include Mongoid::Document field :name, type: String end # models/book.rb class Book include Mongoid::Document field :title, type: String field :publisher, type: String field :published_on, type: Date end # models/category.rb class Category include Mongoid::Document field :name, type: String end
Now that we have added the models, we should also include them properly in the config.ru
and also configure MongoDB. The config.ru
is configured as:
require 'sinatra' require 'mongoid' require './app' Dir["models/*.rb"].each do |file| require "./models/#{File.basename(file, '.rb')}" end run Sinatra::Application
And this is what the code in the main application file, called app.rb
, should look like:
# app.rb require 'mongoid' require 'sinatra' configure do Mongoid.configure do |config| name = "sodibee_development" host = "localhost" config.master = Mongo::Connection.new.db(name) config.persist_in_safe_mode = false end end get "/" do "Hello World" end get "/books" do Book.first.name end
That's it! Let's see what the browser has to say now:
We got MongoDB working using a Sinatra application. Let's see the code in detail. The Gemfile
needs no explanation as it has the gems we require— — sinatra, mongoid
, and bson_ext
. Let's look at the config.ru
rackup file, it looks like this:
require 'sinatra'
require 'mongoid'
require './app'
Dir["models/*.rb"].each do |file|
require "./models/#{File.basename(file, '.rb')}"
end
run Sinatra::Application
Requiring the mongoid
and sinatra
gems is straightforward. However, we also need to include app.rb—
the main application. Let's have a look at the config.ru
rackup file again:
require 'sinatra'
require 'mongoid'
require './app'
Dir["models/*.rb"].each do |file|
require "./models/#{File.basename(file, '.rb')}"
end
run Sinatra::Application
The highlighted code lists all the .rb
files in a directory and loads them. Let's take a look at config.ru
a second time:
require 'sinatra'
require 'mongoid'
require './app'
Dir["models/*.rb"].each do |file|
require "./models/#{File.basename(file, '.rb')}"
end
run Sinatra::Application
The highlighted code is a call to actually run the Sinatra application. Remember, that we have already loaded the application file that has routes, configuration, and control code!
Let's have a look at the main application file app.rb:
# app.rb
require 'mongoid'
require 'sinatra'
configure do
Mongoid.configure do |config|
name = "sodibee_development"
host = "localhost"
config.master = Mongo::Connection.new.db(name)
config.persist_in_safe_mode = false
end
end
get "/" do
"Hello World"
end
get "/books" do
Book.first.name
end
The configure block sets up MongoDB. We set the name as well as host and use the mongo-ruby-driver to configure the database. Now, all the models that have mongoid
included in them and they can directly access the database!
Have a look at app.rb
again:
# app.rb
require 'mongoid'
require 'sinatra'
configure do
Mongoid.configure do |config|
name = "sodibee_development"
host = "localhost"
config.master = Mongo::Connection.new.db(name)
config.persist_in_safe_mode = false
end
end
get "/" do
"Hello World"
end
get "/books" do
Book.first.name
end
This is the web server root path. That means that if the URL does not contain anything but the domain and the port, this path will be used. An application must have at least this route defined to work.
Let's take a look at app.rb
a third time:
# app.rb
require 'mongoid'
require 'sinatra'
configure do
Mongoid.configure do |config|
name = "sodibee_development"
host = "localhost"
config.master = Mongo::Connection.new.db(name)
config.persist_in_safe_mode = false
end
end
get "/" do
"Hello World"
end
get "/books" do
Book.first.name
end
Using the"/books"
route for the Sinatra application, we can directly access the books using the Book
model. The preceding code prints the name of the first book!
We have heard the word Rack earlier. But what is Rack and what does it mean?
Rack is the glue that binds web frameworks with the web servers. Every web server is expected to respond to HTTP requests with a status, header, and body. Rack simplifies this and defines the standard in which a web server should respond. The simplest Rack application is:
class HelloWorld
def call(env)
[200, {"Content-Type" => "text/plain"}, ["Hello world!"]]
end
end
The previous code is from one of the famous resources for introducing Rack http://chneukirchen.org/blog/archive/2007/02/introducing-rack.html. This is an excellent example to understand what Rack means. In the preceding code, 200
represents the HTTP status code, {"Content-Type" => "text/plain"}
represents the HTTP headers, and [" Hello world!"]
is the HTTP body.
Simple and sweet! Where do Sinatra and Rails fit in? They fit right into the Rack by implementing the call
method internally.
18.218.70.170