95-733 Internet Technologies Homework 3 September 24, 2013 Due: Midnight Friday, October 11, 2013 Ruby on Rails ============= One goal of this project is to expose the student to the Ruby programming language and the Rails framework. Rails is based on the model, view, controller (MVC) architecture. Another goal is to introduce the student to OData. OData allows one to publish and consume data on the web in a machine readable format. That data will often be from a database but need not be. OData is described here: http://www.odata.org. The OData data source that we will use is called Northwind and its schema is here: http://merc.tv/img/fig/Northwind_diagram.jpg. Please look over the schema and ask yourself how you would make this data available without OData. And, would your solution easily interoperate with a diverse collection of clients and a variety of client platforms? I recommend, but do not require, that you use Ruby and Rails command line tools rather than an IDE for this project. To begin using Ruby on Rails on Windows, the best approach is probably to use the installation site referred to on the course schedule. See http://railsinstaller.org/. On Mac, I have used RVM with good success. See http://www.rackspace.com/blog/installing-rails-on-a-mac/. For learning Ruby, I recommend the following site: http://www.ruby-lang.org/en/ For learning Rails, I recommend the following site: http://rubyonrails.org/ For parsing the JSON data from Northwind, I recommend using a ruby gem described here: http://rubygems.org/gems/json Your assignment is to: (1) Write a stand alone Ruby program that prompts the user for a Product ID from the Northwind OData source. The program will then fetch JSON data from Northwind and display the product name and supplier name. That is, you will display the name of the supplier of that particular product. Document your program with plenty of comments - describing what the program is doing on each Ruby instruction. Name this program "GetSupplierGivenID.rb". Post this single file to Blackboard. (2) Develop a Rails web application that performs the same function as the program in part 1. That is, the browser will prompt the user for the product ID and the Rails application will fetch the data from Northwind. The product name and supplier name will appear as an HTML response in the browser. Zip your Rails application and submit a single zip file to Blackboard. Commenting of all code is essential. Getting familiar with the OData data source =========================================== Visit the Northwind OData service document with a browser that displays XML http://services.odata.org/Northwind/Northwind.svc/ An OData service document lists available collections. One may view the service document in XML or in JSON. Review the service document. Note that it contains a collection called Products. Visit the Products feed at http://services.odata.org/Northwind/Northwind.svc/Products. Review the structure of the first two Products. You may need to copy the document into a text editor so that newlines can be entered and so that you can see the document's structure. Next, visit a particular product at http://services.odata.org/Northwind/Northwind.svc/Products(2) Verify that the feed for a particular product is also found in the Products feed. Note the regular use of URI's. See the following page for more on URI's with OData. http://www.odata.org/documentation/odata-v2-documentation/uri-conventions/ Visit the service document again, this time retrieve the JSON format. http://services.odata.org/Northwind/Northwind.svc/?$format=json Visit the Products feed at http://services.odata.org/Northwind/Northwind.svc/Products?$format=json Visit the second Product with http://services.odata.org/Northwind/Northwind.svc/Products(2)?$format=json For a discussion of the JSON representation, see http://www.odata.org/documentation/odata-v2-documentation/json-format/ OData is largely based on the REST design approach. The client is not only allowed to perform HTTP GET operations but is also allowed to perform PUTS and DELETES and so on. In this homework, we will only be performing GETS. But the student should be aware that OData is not just about consuming data. Writing and modifying data is also provided in many OData environments. This is a sample Ruby program that demonstrates handling JSON and how to access the Nortwind OData data source. =================================================================================== # This programs demonstrates how Ruby may be used to parse JSON strings. # Ruby represents the JSON object as a hash. require 'net/http' require 'json' # Simple test example. Set up a string holding a JSON object. s = '{"Pirates":{"CF" : "McCutchen","P" : "Bernett","RF" : "Clemente"}}' # Get a hash from the JSON object parsedData = JSON.parse(s) # Display print parsedData["Pirates"] # returns a Ruby hash print "\n" print parsedData["Pirates"]["P"] + "\n" #Bernett print parsedData["Pirates"]["RF"] + "\n" #Clemente # Go out to the internet and collect some JSON # Set up the URL url = "http://services.odata.org/Northwind/Northwind.svc/Products(2)?$format=json" # Make an HTTP request and place the result in jsonStr jsonStr = Net::HTTP.get_response(URI.parse(url)) data = jsonStr.body jsonHash = JSON.parse(data) # See if the product is discontinued if (jsonHash["Discontinued"]) print jsonHash["ProductName"].to_s + " is a discontinued product" else print jsonHash["ProductName"].to_s + " is an active product" end Here are some tested instructions on getting started with Rails 3.x.x ===================================================================== # Build a new directory called Rails_Examples. # Inside the directory Rails_Examples, do the # following: # Check the rails version. $rails -v Rails 3.2.2 # Create a new rails application. In this case we will use the sqlite3 database. # Database use is optional. $rails new CoolRSSJobApp -d sqlite3 $cd CoolRSSJobApp # Run bundle install to satisfy all dependencies $bundle install # Create the model (app/models/job.rb) , migration script, # database table maintenance controller (app/controllers/jobs_controller.rb). # Under app/views/jobs, this command creates CRUD views .html.erb files. $rails generate scaffold Job source:string url:string # Create the actual database. $rake db:migrate # Run the server and visit the CRUD application. $rails server => Booting WEBrick => Rails 3.2.2 application starting in development on http://0.0.0.0:3000 => Call with -d to detach => Ctrl-C to shutdown server # The controller is called jobs_controller.rb. There # is a method in the controller # called index. There is an index.html.erb in # app/views/jobs. # Visit the index method with a browser at : http://localhost:3000/jobs # You can perform CRUD operations on the new database. # Experiment! Study the directories, controller and views. # Add a new controller called main_controller.rb. $rails generate controller main # Add a method to main controller. class MainController < ApplicationController def welcome @num_sources = Job.count end end # edit your config/routes.rb file # add this line before 'end' in the file match '/main/welcome' => 'main#welcome' # Create a partial HTML file called welcome.html.erb. # This files needs to reside in the # CoolRSSJobApp/app/views/main directory. # Ruby automatically includes the remaining # HTML tags. We only need to provide this partial # document.

Welcome to the Job Seeker Application

We currently have <%= @num_sources %> sources. # Notice how the view is able to extract # information from the instance variables in # the controller. # Shut down and restart the web application. # Vist with: http://localhost:3000/main/welcome # Note the controller file is named main_controller.rb. # The method within # the controller is named welcome. The file named welcome.html.erb # is in app/views/main. The HTML file is delivered to the user # after the welcome method is run. # If you want to visit with http://localhost:3000/main/ # then you need to add an index method to the main controller # and an index.html.erb to the app/views/main directory.