GithubHelp home page GithubHelp logo

what-is-sinatra's Introduction

What is Sinatra?

Overview

We'll introduce you to the concept of web frameworks and Sinatra in particular. You'll make sure Sinatra is installed in your computer and run a simple web application with it.

Objectives

  1. Differentiate between a website and a web application
  2. Describe how Web Frameworks like Sinatra simplify web application development through common patterns and conventions
  3. Distinguish between Sinatra and Rails
  4. Install Sinatra on your computer
  5. Run a basic Sinatra application

Web Applications

How does Twitter have different pages for each of its 300 million users? How can AirBnB support over 40 million guests with over 1.5 million listings worldwide? All of this is possible because of web applications.

What's a Web Framework?

A web application framework (WAF) is a software framework that is designed to support the development of dynamic websites, web applications, web services and web resources. The framework aims to alleviate the overhead associated with common activities performed in web development. Wikipedia

Building dynamic web applications in any language is a complex job requiring intimate knowledge of hundreds of technologies and specifications. The good news, however, is that many of these requirements are universal and every web application must conform to these standards.

For example, any robust web application will need to handle request routing and provide a mechanism for the application to respond to different URLs with the appropriate response. Even a simple blog application has to handle a request to GET '/posts' to show all the recent blog posts vs a request to GET '/authors' to list all the authors.

Similarly, web applications require the ability to render templates to produce consistently structured dynamic content. A GET request to '/posts/1' must render the HTML for the first post just as a request to GET '/posts/2' will render identically structured HTML but with content for the second post. This is possible because of templates.

Web frameworks take all these routine and common requirements of any web application and abstract them into code and patterns that provide these functionalities to your application without requiring you to build them yourself.

Frameworks provide structure and libraries that allow you to focus on your application and not applications in general. The bigger the framework, the more you can rely on it to provide you with common needs. The smaller the framework, the more you'll have to build things yourself.

What is Sinatra?

Sinatra is a Domain Specific Language implemented in Ruby that's used for writing web applications. Created by Blake Mizerany, Sinatra is Rack-based, which means it can fit into any Rack-based application stack, including Rails. It's used by companies such as Apple, BBC, GitHub, LinkedIn, and more.

Essentially, Sinatra is nothing more than some pre-written methods that we can include in our applications to turn them into Ruby web applications.

Unlike Ruby on Rails, which is a Full Stack Web Development Framework that provides everything needed from front to back, Sinatra is designed to be lightweight and flexible. Sinatra is designed to provide you with the bare minimum requirements and abstractions for building simple and dynamic Ruby web applications.

In addition to being a great tool for certain projects, Sinatra is a great way to get started in web application development with Ruby and will prepare you for learning other larger frameworks, including Rails.

Why Sinatra Before Rails

We've all heard of "Ruby on Rails" and how powerful it is. You can build impressive web applications in mere hours! How amazing. Most people, when they learn Rails for the first time, literally say "It's like magic!". But we're developers, and we know that magic isn't real and that other smart developers just built an impressive framework.

That means it's important to understand the basic concepts of Rails before diving into Rails itself. Enter Sinatra.

Sinatra is considered a light weight framework where the responsibility of app structure and communication falls solely on the developer. Sinatra doesn't give you a lot to get started with. There is no way to auto-generate files and directories, no way for the app to make assumptions about routes, or "Sinatra magic".

Because of this, working with Sinatra allows you to dive in deep with the major concepts of MVC, a system for building web applications that governs 90% of the worlds' apps. You are required to manually set up routes and connect them to other pieces of your application. Without this manual setup, your application does not automatically know how to communicate with your database or what HTML files to load in the browser. And even more importantly, without a manual setup, you lose connection to the major components of a web application, and in particular, all the moving pieces of MVC.

So introduce yourself to Sinatra. Get to know it, and know it well. The better your foundation, the more you'll be able to know (and like) Rails.

The Sinatra DSL

Any application that requires the sinatra library will get access to methods like: get and post. These methods provide the ability to instantly transform a Ruby application into an application that can respond to HTTP requests.

Basic Sinatra Applications

First, make sure Sinatra is installed by running gem install sinatra in your terminal.

The simplest Sinatra application would be:

File: app.rb

require 'sinatra'

class App < Sinatra::Base

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

end

You could start this web application by running rackup app.rb. You'll see something similar to:

$ rackup app.rb
Puma starting in single mode...
* Version 3.10.0 (ruby 2.3.0-p0), codename: Russell's Teapot
* Min threads: 0, max threads: 16
* Environment: development
* Listening on tcp://localhost:9292
Use Ctrl-C to stop

If you are running Ruby version 2.6.1 or another similar version of Ruby, you'll see something such as:

[2019-09-23 13:29:58] INFO  WEBrick 1.4.2
[2019-09-23 13:29:58] INFO  ruby 2.6.1 (2019-01-30) [x86_64-darwin17]
[2019-09-23 13:29:58] INFO  WEBrick::HTTPServer#start: pid=82970 port=9292

Note: If you're using the Learn IDE, that last line will look more like this: Listening on 192.241.134.186:30000, CTRL+C to stop and you'll have to navigate to the address shown (in this case: 192.241.134.186:30000) to see your site!

This is telling us that Sinatra has started a web application running on your computer listening to HTTP requests at port 9292, the Sinatra default. If you start this application and navigate to http://localhost:9292 you'll see "Hello, World!" in your browser. Go back to your terminal running the Sinatra application and stop it by typing CTRL+C. You should see:

* Listening on tcp://localhost:9292
Use Ctrl-C to stop
^C- Gracefully stopping, waiting for requests to finish
=== puma shutdown: 2017-11-15 09:41:19 -0500 ===
- Goodbye!
[00:01:11] (wip-lesson) what-is-sinatra
$

and for version 2.6.1 and similar versions you should see:

[2019-09-23 13:53:27] INFO  going to shutdown ...
[2019-09-23 13:53:27] INFO  WEBrick::HTTPServer#start done.

This is the most basic Sinatra application structure and is actually pretty uncommon. More commonly, Sinatra is used in a modular style encapsulated by Controller Classes and booted via the config.ru Rack convention.

Resources

what-is-sinatra's People

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

what-is-sinatra's Issues

different terminal output for ruby 2.6.1

output looks like this

[2019-03-31 14:45:45] INFO WEBrick 1.4.2 [2019-03-31 14:45:45] INFO ruby 2.6.1 (2019-01-30) [x86_64-darwin18] [2019-03-31 14:45:45] INFO WEBrick::HTTPServer#start: pid=69165 port=9292 ^C[2019-03-31 14:45:55] INFO going to shutdown ... [2019-03-31 14:45:55] INFO WEBrick::HTTPServer#start done.

not
`$ rackup app.rb
Puma starting in single mode...

  • Version 3.10.0 (ruby 2.3.0-p0), codename: Russell's Teapot
  • Min threads: 0, max threads: 16
  • Environment: development
  • Listening on tcp://localhost:9292
    Use Ctrl-C to stop and not
  • Listening on tcp://localhost:9292
    Use Ctrl-C to stop
    ^C- Gracefully stopping, waiting for requests to finish
    === puma shutdown: 2017-11-15 09:41:19 -0500 ===
  • Goodbye!
    [00:01:11] (wip-lesson) what-is-sinatra
    $`

Repeated clause

The last paragraph of Why Sinatra Before Rails has a duplicate clause: "a system for building web applications that governs 90% of the worlds' apps." It's in both the first and last sentence.

Content to address for the Learn Qualifying Track

Since the last unit before the unit on Sinatra is on OO, there are a couple of knowledge gaps that should be addressed.

screen shot 2015-10-01 at 11 37 24 am

^^ Add "libraries of code" since having "libraries alone can be confusing.

screen shot 2015-10-01 at 11 40 55 am

Suggest Wikipedia or other link that explains DSL. Student won't know what Rack is or Rails. Also, can we assume that they know what a Full Stack Development Framework is?

screen shot 2015-10-01 at 11 42 47 am

^^ Unclear whether the student should actually make this file and run it.

screen shot 2015-10-01 at 11 44 08 am

^^ Will students know what "listening to HTTP requests" mean? Unclear what a port is and how port 4567 is the Sinatra default.

screen shot 2015-10-01 at 11 45 34 am

^^ Really unsure what this application structure is? Should it be broken apart a bit moer? Also, "modular style encapsulated by Controller Classes and booted via the config.ru Rack convention" makes no sense to me right now. In addition to tweaking the language slightly, could be worth mentioning that these things will be mentioned in the next readme.

@matbalez

Unable to start Sinatra server via ruby app.rb

When students run rackup or shotgun in the IDE to start a Sinatra server, the command is properly intercepted and routed on the backend. However, when students run ruby app.rb it isn't, and it results in the following failure:

[02:01:30] (master) sinatra-hello-world-basics-v-000                                            
// โ™ฅ ruby app.rb                                                                                
== Sinatra (v1.4.7) has taken the stage on 4567 for development with backup from Thin           
Thin web server (v1.7.0 codename Dunder Mifflin)                                                
Maximum connections set to 1024                                                                 
Listening on localhost:4567, CTRL+C to stop                                                     
Stopping ...                                                                                    
== Sinatra has ended his set (crowd applauds)                                                   
/usr/local/rvm/gems/ruby-2.3.1/gems/eventmachine-1.2.0.1/lib/eventmachine.rb:530:in `start_tcp_server': no acceptor (port is in use or requires root privileges) (RuntimeError)                 
        from /usr/local/rvm/gems/ruby-2.3.1/gems/eventmachine-1.2.0.1/lib/eventmachine.rb:530:in `start_server'                                                                                 
        from /usr/local/rvm/gems/ruby-2.3.1/gems/thin-1.7.0/lib/thin/backends/tcp_server.rb:16:in `connect'                                                                                     
        from /usr/local/rvm/gems/ruby-2.3.1/gems/thin-1.7.0/lib/thin/backends/base.rb:63:in `block in start'                                                                                    
        from /usr/local/rvm/gems/ruby-2.3.1/gems/eventmachine-1.2.0.1/lib/eventmachine.rb:194:in `run_machine'                                                                                  
        from /usr/local/rvm/gems/ruby-2.3.1/gems/eventmachine-1.2.0.1/lib/eventmachine.rb:194:in `run'                                                                                          
        from /usr/local/rvm/gems/ruby-2.3.1/gems/thin-1.7.0/lib/thin/backends/base.rb:73:in `start'                                                                                             
        from /usr/local/rvm/gems/ruby-2.3.1/gems/thin-1.7.0/lib/thin/server.rb:162:in `start'   
        from /usr/local/rvm/gems/ruby-2.3.1/gems/rack-1.6.4/lib/rack/handler/thin.rb:19:in `run'
        from /usr/local/rvm/gems/ruby-2.3.1/gems/sinatra-1.4.7/lib/sinatra/base.rb:1506:in `start_server'                                                                                       
        from /usr/local/rvm/gems/ruby-2.3.1/gems/sinatra-1.4.7/lib/sinatra/base.rb:1444:in `run!'                                                                                               
        from /usr/local/rvm/gems/ruby-2.3.1/gems/sinatra-1.4.7/lib/sinatra/main.rb:25:in `block in <module:Sinatra>'

Have to either change that functionality or the README for this lesson, which instructs students to start this web application by running ruby app.rb.

cc: @aturkewi

P.S.: Props to @mekowalski for (apparently) being the only student using the IDE to actually try to follow the instructions! ๐Ÿ˜†

ENV["HOST_PORT"] returns nil

This lesson wasn't able to load up the sinatra server when a student did ruby app.rb. On as screen share I discovered that ENV["HOST_PORT"] was returning nil, most likely because this key was removed from or changed in ENV hash. I was able to get Sinatra to work by manually passing in an port number but this could potentially cause more issue for students in the future.

@aturkewi

Incorrect directions

Hello,

in the readme it says to comment out two lines if running from learn IDE... however, it only works when I uncommented the two lines.

Sinatra RuntimeError

The lesson says after you do gem install sinatra and set up your app.rb to be (if you're on the IDE):

require 'sinatra'

# Add these two lines below if you're on the Learn IDE!
set :bind, ENV["HOST_IP"]
set :port, ENV["HTTP_PORT"]

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

Then when you run ruby app.rb you should have a running Sinatra server on SOME_IP:SOME_PORT, but instead, this is coming up:

// โ™ฅ ruby app.rb
== Sinatra (v2.0.0) has taken the stage on  for development with backup from Thin
Thin web server (v1.7.2 codename Bachmanity)
Maximum connections set to 1024
Listening on 67.205.130.239:8080, CTRL+C to stop
Stopping ...
== Sinatra has ended his set (crowd applauds)
/usr/local/rvm/gems/ruby-2.3.1/gems/eventmachine-1.2.5/lib/eventmachine.rb:530:in `start_tcp_server': no acceptor (port is in use or requires root privileges) (RuntimeError)
        from /usr/local/rvm/gems/ruby-2.3.1/gems/eventmachine-1.2.5/lib/eventmachine.rb:530:in `start_server'
        from /usr/local/rvm/gems/ruby-2.3.1/gems/thin-1.7.2/lib/thin/backends/tcp_server.rb:16:in `connect'
        from /usr/local/rvm/gems/ruby-2.3.1/gems/thin-1.7.2/lib/thin/backends/base.rb:63:in `block in start'
        from /usr/local/rvm/gems/ruby-2.3.1/gems/eventmachine-1.2.5/lib/eventmachine.rb:194:in `run_machine'
        from /usr/local/rvm/gems/ruby-2.3.1/gems/eventmachine-1.2.5/lib/eventmachine.rb:194:in `run'
        from /usr/local/rvm/gems/ruby-2.3.1/gems/thin-1.7.2/lib/thin/backends/base.rb:73:in `start'
        from /usr/local/rvm/gems/ruby-2.3.1/gems/thin-1.7.2/lib/thin/server.rb:162:in `start'
        from /usr/local/rvm/gems/ruby-2.3.1/gems/rack-2.0.3/lib/rack/handler/thin.rb:22:in `run'
        from /usr/local/rvm/gems/ruby-2.3.1/gems/sinatra-2.0.0/lib/sinatra/base.rb:1522:in `start_server'
        from /usr/local/rvm/gems/ruby-2.3.1/gems/sinatra-2.0.0/lib/sinatra/base.rb:1456:in `run!'
        from /usr/local/rvm/gems/ruby-2.3.1/gems/sinatra-2.0.0/lib/sinatra/main.rb:26:in `block in <module:Sinatra>'

If I ran: echo $HTTP_PORT it showed up blank, so I tried substituting RACK_PORT and hard-coding a port# instead of HTTP_PORT, but just got the same message but for a different port.

Narrative issues

The overview- "How does Twitter have different pages for each of its 300 million users? How can AirBnB support over 40 million guests with over 1.5 million listings worldwide? All of this is possible because of web applications"- is never addressed again in the rest of the readme. It feels almost like a non sequitur

Answering that first in the "What's a Web Framework" section would ease the reader in better to the rest of the info in there, and might also motivate them to keep going further into the readme. Or can we make Web Frameworks its own separate Readme, and in there, you can compare Rails vs. Sinatra as well as why pedagogically, teaching Sinatra first makes sense.

I think then this Sinatra Readme can just cover them creating the simple Sinatra app, which while pedagogically not that useful maybe, will show them the "magic" of seeing something on the web.

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.