Skip to content

Latest commit

 

History

History
96 lines (87 loc) · 2.16 KB

sinatra-cheatsheet.md

File metadata and controls

96 lines (87 loc) · 2.16 KB

Sinatra Cheat Sheet

Route params
Serving static assets
Sending data to client
Handling forms

Getting started

gem install sinatra
gem install puma
ruby main.rb

Basic route

    require 'sinatra'
    get '/frank-says' do
        'Put this in your pipe & smoke it!'
    end 

Route params

    # http://127.0.0.1:4567/hello/friend 
    get '/hello/:name' do
        # matches "GET /hello/foo" and "GET /hello/bar"
        # params['name'] is 'foo' or 'bar'
        "Hello #{params['name']}!"
    end

Multiple route params

    # http://127.0.0.1:4567/hello/my/player 
    get '/hello/:first/:second' do 
        "Hello #{params['second']}!"
    end

Serving static assets

    =begin
    /static/
    - index.html
    - style.css 
    http://127.0.0.1:4567/index.html 
    note: use a template language such as Erb or HAML for dynamic HTML
    =end
    set :public_folder, __dir__ + '/static' 

Sending data to client

    get '/' do
        @name = "TIM" 
        @names = ["tom", "taylor", "tamara"]
        erb :index  
    end 

Handling forms (POST)

    =begin
    /views/index.erb 
    <h1><%= @name %></h1>
    <ul>
    <% @names.each do |name| %>
        <li><%= name %></li>
    <% end %>
    <ul>
    <form method="POST" action="/greeting">
    <input type="text" placeholder="name" name="name" /> 
    <input type="submit" />
    </form>	  
    <b>Greeting: <%= greeting %> 
    =end 
    post '/greeting' do
        # investigate template layouts ;) 
        @name = "TIM" 
        @names = ["tom", "taylor", "tamara"]
        erb :index, :locals => {:greeting => "hi #{params[:name]}" }  
    end

REST Endpoint

    # curl http://127.0.0.1:4567/rest-api 
    get '/rest-api' do 
        # todo: add cors example 
        content_type :json 
        { json: "rest-api examle"}.to_json 
    end 

    not_found do
        return if JSON.parse( body[0] ).include?( 'error' ) rescue nil # Error already defined
        [ 404, { message: 'Not found :/ ' }.to_json ]
    end