180 likes | 312 Views
Creating Web Services with Ruby on Rails. Robert Thew Internet and Web Systems II. What is Ruby?. Ruby is a language created in Japan in 1995 by Yukihiro Matsumoto It ’ s an Object-Oriented language that also has elements of Functional languages. Why Create Ruby?.
E N D
Creating Web Services with Ruby on Rails Robert Thew Internet and Web Systems II
What is Ruby? • Ruby is a language created in Japan in 1995 by Yukihiro Matsumoto • It’s an Object-Oriented language that also has elements of Functional languages
Why Create Ruby? • Matsumoto wanted to create a language that combined all his favorite features into one language
What Are Ruby’s Features? • Completely Object-Oriented • Can do Functional as well as Imperative programming • Clean, simple and consistent syntax
Basics of Ruby • Demo using irb
Ruby on Rails • A Model-View-Controller Web Framework for Ruby developed in 2004 by David Heinemeier Hansson • Guiding principle is Convention over Configuration • Very little boilerplate code • Small classes with short methods • Allows for rapid development
Installation • Ruby and RoR come installed on MacPros • Can be downloaded and installed from: • ruby-lang.org • rubyonrails.org • MySQL can be downloaded from mysql.com
Building an Application • Run the rails command to create a new application framework • Go into the new directory and edit the database config file • Add user and password for development entry $rails new biblio $cd biblio $vi config/database.yml
Create Database • Create an empty development database instance $rake db:create
What Is Rake? • Rake is Ruby’s version of Make • Rakefiles use ruby syntax – so no whitespace problems • Rails applications come with rakefiles for running database commands and for doing unit tests
Test Application • Rails uses WEBrick, a ruby library that provides a basic HTTP server >rails s => Booting WEBrick => Rails 3.0.5 application starting in development on http://0.0.0.0:3000 => Call with -d to detach => Ctrl-C to shutdown server
Generate MVC Files • Rails comes with a generator that will create database migrations, model, view, controller, and test files >rails generate scaffold Article title:stringauthor:stringurl:stringabstract:text
Database Migration • Migration classes are used to create and modify tables >rake db:migrate
Model and Controller Code • Note how short all the methods are • Convention over Configuration means code can be kept simple • No need for explicit getters and setters in the Model
RESTful Architecture • All the URLs will follow the same pattern for CRUD (Create Read Update Delete)operations • REST (Representational State Transfer) is a simpler and consistent alternative to SOAP
URL Patterns These URLs are routed to their respective controller methods. GET /articles GET /articles/new POST /articles GET /articles/1 GET /articles/1.xml GET /articles/1/edit PUT /articles/1 DELETE /articles/1
Test Application >rails s >rails c
Rails Tools • Git – Distributed versioning system • Capistrano – For scripting deployments • RubyGems – Package dependency manager • RVM – Run multiple versions on one system • Heroku – Ruby Cloud Platform as a Service