Giter Site home page Giter Site logo

sinatra-basics-v-000's Introduction

Sinatra Basics

Overview

We'll discuss modular Sinatra applications and mounting a controller.

Objectives

  1. Distinguish between single file Sinatra apps and modular Sinatra apps
  2. Explain the purpose of the config.ru and what is included in the file
  3. Define and create a Sinatra controller
  4. Mount a Sinatra controller in config.ru

Modular Sinatra Applications

Web applications, even simple Sinatra ones, tend to require a certain degree of complexity. For example, your application might have multiple routes, route-handlers, and configuration. To handle this, Sinatra is more commonly used through the Modular Sinatra Pattern (over the classical, single file app.rb pattern).

config.ru

The first new convention this pattern introduces is a config.ru file. The purpose of config.ru is to detail to Rack the environment requirements of the application and start the application.

A common 'config.ru' might look like:

File: config.ru

require 'sinatra'

require_relative './app.rb'

run Application

In the first line of config.ru we load the Sinatra library. The second line requires our application file, defined in app.rb. The last line of the file uses run to start the application represented by the ruby class Application, which is defined in app.rb.

Sinatra Controllers: app.rb

config.ru requires a valid Sinatra Controller to run. A Sinatra Controller is simply a ruby class that inherits from Sinatra::Base. This inheritance transforms into a web application by giving it a Rack-compatible interface behind the scenes via the Sinatra framework. Open app.rb

class Application < Sinatra::Base

  get '/' do
    "Hello, World!"
  end

end

We simply create a new class Application and inherit from Sinatra::Base.Our class constant could have been anything descriptive of the functionality provided by the class. The classes that inherit from Sinatra::Base and define the HTTP interface for our application are called Controllers.

In a single controller application, a single file defining the controller, like app.rb, will suffice. That controller will define every single URL and response of our application.

Controllers define an HTTP method using the sinatra routing DSL provided by methods like get and post. When you enclose these methods within a ruby class that is a Sinatra Controller, these HTTP routes are scoped and attached to the controller.

The final step in creating a controller is mounting it in config.ru. Mounting a controller means telling Rack that part of your web application is defined within the following class. We do this in config.ru by using run Application where run is the mounting method and Application is the controller class that inherits from Sinatra::Base and is defined in a previously required file.

The class name we defined in our application controller (app.rb) is just a normal Ruby class. We could have named it MyToDoApp:

class MyToDoApp < Sinatra::Base

  get '/' do
    "Hello, World!"
  end

end

If this was our class name, we would need to change config.ru to run the appropriate class:

run MyToDoApp

Resources

View Sinatra Basics on Learn.co and start learning to code for free.

sinatra-basics-v-000's People

Contributors

ipc103 avatar annjohn avatar aviflombaum avatar victhevenot avatar franknowinski avatar jmburges avatar sophiedebenedetto avatar

Watchers

James Cloos avatar Liah Wallace avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.