|
|
Want to know more about Packt's Article Network? Interested in contributing your article ideas? Please visit our FAQ for more information. See More BROWSE
All Titles WordPress Web Services SOA BPEL Web Graphics & Video Web Development RAW Portugues, Espanol, Italiano, French PHP/MySQL Oracle Open Source Networking & Telephony Moodle Microsoft & .NET Linux Servers jQuery Joomla! JBoss Java e-Learning e-Commerce Dynamics Drupal CRM Cookbook Content Management Beginner Guides Architecture and Analysis AJAX Future Titles Recently Published Titles Ruby on Rails is an active component in the world of web application framework. Ruby is the language used and Ruby on Rails is the framework built upon Ruby. In this article by A.P.Rajshekhar, we will be learning how to develop the login management module and the comment management module .We will start off by creating a login page and then move to implementing the Authenticate method, set up the session, try out applying authorization. After this, we will learn generating the Scaffold, modifying the model, review the View, customizing the controller under the development of comment management module sub title. See More |
Building tiny Web-applications in Ruby using Sinatra
What’s Sinatra?Sinatra is not a framework but a library i.e. a set of classes that allows you to build almost any kind of web-based solution (no matter what the complexity) in a very simple manner, on top of the abstracted HTTP layer it implements from Rack. When you code in Sinatra you’re bound only by HTTP and your Ruby knowledge. Sinatra doesn’t force anything on you, which can lead to awesome or evil code, in equal measures. Sinatra apps are typically written in a single file. It starts up and shuts down nearly instantaneously. It doesn’t use much memory and it serves requests very quickly. But, it also offers nearly every major feature you expect from a full web framework: RESTful resources, templating (ERB, Haml/Sass, and Builder), mime types, file streaming, etags, development/production mode, exception rendering. It’s fully testable with your choice of test or spec framework. It’s multithreaded by default, though you can pass an option to wrap actions in a mutex. You can add in a database by requiring ActiveRecord or DataMapper. And it uses Rack, running on Mongrel by default. Blake Mizerany the creator of Sinatra says that it is better to learn Sinatra before Ruby on Rails: When you learn a large framework first, you’re introduced to an abundance of ideas, constraints, and magic. Worst of all, they start you with a pattern. In the case of Rails, that’s MVC. MVC doesn’t fit most web-applications from the start or at all. You’re doing yourself a disservice starting with it. Back into patterns, never start with them- Reference here Installing SinatraThe simplest way to obtain Sinatra is through Rubygems. Open a command window in Windows and type: c:> gem install sinatra Linux/OS X the command would be: sudo gem install sinatra Installing its Dependencies
c:> gem install mongrel What are Routes?The main feature of Sinatra is defining ‘routes’ as an HTTP verb for a path that executes an arbitrary block of Ruby code. Something like: verb ‘path’ do Sinatra’s routes are designed to respond to the HTTP request methods (GET, POST, PUT, DELETE). In Sinatra, a route is an HTTP method paired with an URL matching pattern. These URL handlers (also called "routing") can be used to match anything from a static string (such as /hello) to a string with parameters (/hello/:name) or anything you can imagine using wildcards and regular expressions. Each route is associated with a block. Let us look at an example: get '/' do Routes are matched in the order they are defined. When a new request comes in, the first route that matches the request is invoked i.e. the handler (the code block) attached to that route gets executed. For this reason, you should put your most specific handlers on top and your most vague handlers on the bottom. A tiny web-applicationHere’s an example of a simple Sinatra application. Write a Ruby program myapp1.rb and store it in the folder: c:sinatra_programs Though the name of the folder is c:>sinatra_programs, we are going to have only one Sinatra program here. The program is: # myapp1.rb Sinatra applications can be run directly: ruby myapp1.rb [-h] [-x] [-e ENVIRONMENT] [-p PORT] [-s HANDLER] The above options are:
In the article- http://gist.github.com/54177, it states that using: require ‘rubygems’, is wrong. It is an environmental issue and not an app issue. The article mentions that you might It is an environmental issue and not an app issue. The article mentions that you might use: ruby -rubygems myapp1.rb Another way is to use RUBYOPT. Refer article – http://rubygems.org/read/chapter/3. By setting the RUBYOPT environment variable to the value rubygems, you tell Ruby to load RubyGems every time it starts up. This is similar to the -rubygems options above, but you only have to specify this once (rather than each time you run a Ruby script). Building Dynamic Web 2.0 Websites with Ruby on Rails
Unix users will want to put the following line in their .profile (or equivalent): export RUBYOPT=rubygems Windows users will want to set the RUBYOPT environment variable using the appropriate system utility. (On XP you can find it under Settings / Control Panel / System. Click the advanced tab and then the "Environment Variables" button near the bottom. Note that the one-click installer will set up RUBYOPT for you automatically (unless you request it not be done). Now let us run our trivial Sinatra application. Open a command window (I am using a Windows XP box) and type: c:sinatra_programs> ruby myapp1.rb Next open a browser window and type http://localhost:4567/ Note that Sinatra uses port 4567. Sinatra does not support application reloading and you need to stop the server and re-start it every time you make a change to the myapp1.rb application. Rack aims to provide a minimal API for connecting web application servers (Mongrel in my case) and web frameworks (Sinatra). Our Ruby web application myapp1.rb is built using the web framework Sinatra. Sinatra is built on top of Rack. Thus myapp1.rb is a Rack app. Here, the web application server Mongrel runs our Rack app myapp1.rb. Rack supports "middleware" - components that sit between the server and your application, monitoring and/or manipulating the HTTP request/response. When we type http://localhost:4567/ in a browser window, a HTTP GET request is sent to the application myapp1.rb You should see the following in your browser window:
The above screenshots suggests writing a get handler. Let’s do that by writing a new file myapp2.rb : # myapp2.rb The above get handler is purposely kept empty. Press Ctrl-Break in the open command window (for Windows XP). Next, type: c:sinatra_programs> ruby myapp2.rb Refresh the browser page (http://localhost:4567/) and you will see a blank page. Next, let us do something in the get handler. Now write a new file myapp3.rb as follows: # myapp3.rb Press Ctrl-Break in the open command window (for Windows XP). Next, type: c:sinatra_programs> ruby myapp3.rb Refresh the browser page and you should see "Welcome from RubyLearning!" in the browser window. This happens because when you use the above URL, Sinatra runs the get block and will return whatever you put inside it, to the web browser. Next, in the command window, press Ctrl-Break to stop Sinatra and then close the command window. DeploymentHeroku is a platform for the instant deployment of Ruby applications and is one of the easiest configuration and deployment option (http://heroku.com/). Heroku has full support for Sinatra applications. Deploying to Heroku is simply a matter of pushing to a remote git repository. Steps to deploy to HerokuPlease ensure that you are connected to the internet and then:
Congrats, you have successfully deployed your first Sinatra app. For other deployment options, refer the URL –http://www.sinatrarb.com/book.html#deployment SummaryIn this article we have explored Sinatra, covered its installation and its dependencies, and then went on to build a tiny web-application which we deployed on Heroku. ResourcesSinatra homepage – Sinatra Documentation – HTTP Made Really Easy – HTTP 101 – Rack - Introduction to Rack – List of applications, libraries, websites and companies using Sinatra – Interviews of people working with Sinatra – Rubyists using Sinatra – How do I learn and master Sinatra? What are the 12 rules of Sinatra? Ruby on Rails Web Mashup Projects
About the AuthorSatish Talim is a senior software consultant based in Pune, India with over 32+ years of I.T. experience. His experience lies in developing and executing business for high technology and manufacturing industry customers. His strengths lie in Business Development and Business Networking apart from new product and solution ideas. Along with good experience of organization development, he has excellent cross disciplinary background in engineering, computer science and management. He has helped start subsidiaries for many US based software companies like Infonox and Servient Inc. He has been associated with Java / J2EE since 1995 and involved with Ruby and Ruby on Rails since 2005. He has started and manages two very active Java (PuneJava) and Ruby (PuneRuby) User Groups in Pune, India. He is a Ruby Mentor on rubyforge.org, helping people with Ruby programming. Recently, he has written an eBook – “Introduction to Sinatra”, available at – http://rubylearning.com/blog/introduction-to-sinatra-ebook/. He can be contacted at http://satishtalim.com/. Books from Packt |
TOP TITLES ![]()
|
| ||||||||