Coder Social home page Coder Social logo

rocknrollmarc / sinatra-mapping Goto Github PK

View Code? Open in Web Editor NEW

This project forked from hallison/sinatra-mapping

0.0 1.0 0.0 373 KB

Sinatra::Mapping extension is a minimal module that is useful for create map names for Sinatra web application.

Home Page: http://sinatra-mapping.rubyforge.org

License: MIT License

sinatra-mapping's Introduction

Sinatra::Mapping

Map easily URLs in your Web applications.

The extension Sinatra::Mapping is a minimal module that is useful for create map names for Ruby Sinatra web applications.

Getting start

Install stable version gem from RubyForge.org:

gem install sinatra-mapping

Or, install development version gem from GitHub.com:

gem install hallison-sinatra-mapping --source http://gems.github.com

How to use

Sinatra implements REST routes:

get '/' do
  .. show something ..
end

post '/' do
  .. create something ..
end

put '/' do
  .. update something ..
end

delete '/' do
  .. annihilate something ..
end

For improve this routes use extension by registered method in the main source of application. To better understand, copy and paste the following example in Ruby source file weblog.rb:

#!/usr/bin/env ruby
require 'rubygems'
require 'sinatra'
require 'sinatra/mapping' # only this line for use mapping!

map :root,    "blog"    # /blog/
map :entries, "posts"   # /blog/posts
map :tags,    "labels"  # /blog/labels

mapping :entry          => "posts/:entry_id",           # /blog/posts/id-for-post
        :entry_comments => "posts/:entry_id/comments",  # /blog/posts/id-for-post/comments
        :tagged_entries => "labels/:tag_id/entries"     # /blog/labels/id-for-tag/entries

# /blog/
get root_path do
  <<-end_content
    <h1>Welcome to Foo Web Application</h1>
    <ul>
      <li>#{link_to title_path(:entries), :entries, :title => title_path(:entries)}</li>
      <li>#{link_to title_path(:tags),    :tags,    :title => title_path(:tags)}</li>
    </ul>
  end_content
end

# /blog/entries
get entries_path do
  <<-end_content
    <h1>Welcome to Foo Web Application</h1>
    <h2>#{title_path(:entries)}</h2>
    <ul>
      <li>#{link_to "Testing new entry ...", :entries, "testing-new-entry"}</li>
      <li>#{link_to "Testing old entry ...", :entries, "testing-old-entry"}</li>
    </ul>
    <p>
      #{link_to "Back", :root}
    </p>
  end_content
end

# /blog/labels/tag-id-for-show-content
get tags_path do
  <<-end_content
    <h1>Welcome to Foo Web Application</h1>
    <h2>#{title_path(:tags)}</h2>
    <ul>
      <li>#{link_to "Ruby",    :tags, "ruby",    "entries"}</li>
      <li>#{link_to "Sinatra", :tags, "sinatra", "entries"}</li>
      <li>#{link_to "Mapping", :tags, "mapping", "entries"}</li>
    </ul>
    <p>
      #{link_to "Back", :root}
    </p>
  end_content
end

# /blog/entries/entry-id-for-show-content
get entry_path do |entry_id|
  title = entry_id.gsub('-',' ').capitalize
  <<-end_content
    <h1>Welcome to Foo Web Application</h1>
    <h2>#{title}</h2>
    <p>
      It works!
    </p>
    <p>
      #{link_to "Back", :root} | #{link_to "Comments", :entries, entry_id, 'comments'}
    </p>
  end_content
end

# /blog/entries/entry-id-for-show-content/comments
get entry_comments_path do |entry_id|
  title = entry_id.gsub('-',' ').capitalize
  <<-end_content
    <h1>Welcome to Foo Web Application</h1>
    <h2>#{title}</h2>
    <p>
      It works and show comments for "#{title}".
    </p>
    <p>
      #{link_to "Back", :root}
    </p>
  end_content
end

get tagged_entries_path do |tag_id|
  <<-end_content
    <h1>Welcome to Foo Web Application</h1>
    <h2>#{tag_id.capitalize}</h2>
    <p>
      It works and show all entries tagged with "#{tag_id}".
    </p>
    <p>
      #{link_to "Back", :root}
    </p>
  end_content
end

Run:

$ ruby weblog.rb -p 3000

Open Web browser localhost:3000/blog/ and look … it works!

:include:LICENSE

sinatra-mapping's People

Watchers

 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.