95-733 Internet Technologies Homework 3 Assigned: Thursday, February 12, 2015 Ruby on Rails Due: 11:59:59 PM, Tuesday, February 26, 2015 OData 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 Part I. Stand alone Ruby programming ==================================== (0) 15 Points. Write a stand alone Ruby program that reads a JSON representation of the Northwind service document and displays a list of available collections. Document your program with plenty of comments - describing what the program is doing on each Ruby instruction. Name this program "DisplayServiceDocument.rb". An example execution of my solution follows: ruby DisplayServiceDocument.rb Northwind Collections from "http://services.odata.org/Northwind/Northwind.svc/?$format=json". Categories CustomerDemographics Customers Employees Order_Details Orders Products Regions Shippers Suppliers Territories Alphabetical_list_of_products Category_Sales_for_1997 Current_Product_Lists Customer_and_Suppliers_by_Cities Invoices Order_Details_Extendeds Order_Subtotals Orders_Qries Product_Sales_for_1997 Products_Above_Average_Prices Products_by_Categories Sales_by_Categories Sales_Totals_by_Amounts Summary_of_Sales_by_Quarters Summary_of_Sales_by_Years (1) 15 Points. Write a stand alone Ruby program that prompts the user for a Product ID from the Northwind OData data source. The program will then fetch JSON data from Northwind and display the product name, supplier name and whether the product is discontinued or active. Document your program with plenty of comments - describing what the program is doing on each Ruby instruction. Name this program "GetSupplierGivenID.rb". Here is a sample execution: ruby GetSupplierGivenID.rb Enter Product ID 5 Product ID:5 Product name: Chef Anton's Gumbo Mix Supplier ID: 2 Active/Discontinued Chef Anton's Gumbo Mix is a discontinued product Supplier name : New Orleans Cajun Delights (2) 15 Points. Write a stand alone Ruby program that prompts the user for a product ID from the Northwind Odata data source. The program will then display a list of the order ID's associated with that product along with the total price of each order (taking into account the unit price, quantity and discount.) For example, my solution runs as follows: ruby GivenProductIDListOrders.rb Enter Product ID 3 Product ID: 3 The number of orders for product ID 3 is 12 Order ID's Total Price after discount 10289 $240.0 10405 $400.0 10485 $144.0 10540 $600.0 10591 $140.0 10702 $60.0 10742 $200.0 10764 $180.0 10849 $490.0 10857 $300.0 11017 $250.0 11077 $40.0 (3) 15 Points. Write a stand alone Ruby program that prompts the user for a product ID from the Northwind Odata data source. The program will then display a list of contact names of customers that have ordered that product. My solution runs as follows: ruby GivenProductIDListCustomerContactNames.rb Enter Product ID and I will find Customer's who ordered it 5 Product ID: 5 The number of orders for product ID 5 is 10 Roland Mendel Paula Wilson Pedro Afonso Roland Mendel Giovanni Rovelli Liz Nixon Elizabeth Brown Yvonne Moncada Jose Pavarotti Ann Devon (4) 15 Points. Write a stand alone Ruby program that reads the Sales_Totals_by_Amounts collection and displays a list of sale amounts. My solution begin like this: ruby Sales_Totals_By_Amounts.rb 3192.6500 3815.2500 : : Part II. Ruby on Rails programming ================================== (5) 25 Points. Develop a Rails web application that performs the same functions as programs (1), (2) and (3) as written in Part I. The design of the web user interface is in your hands. However, there must be an introductory page. From that page the user will be able to select which one of the three functions that he/she wants to perform. Name the application NorthWindWebApp These three functions are: - Given the Product ID, find the supplier. - Given the Product ID, list each Order ID and the price after discount. - Given the Product ID, list the customer's Contact Name for each order. There are some notes on getting started with rails at this link: http://www.andrew.cmu.edu/user/mm6/95-733/Rails_Getting_Started.txt 5 of the 25 points are reserved for the students who utilize jQuery Mobile and responsive design. See the notes on jQuery Mobile and Rails at http://www.andrew.cmu.edu/user/mm6/95-733/CreatejQueryMobileApp.txt 5 of the 25 points are reserved for the students who uses git and deploy the solution to Heroku. See the notes on git and Heroku located at http://www.andrew.cmu.edu/user/mm6/95-733/Rails_Git_Heroku_Notes.txt Summary ======= Submit a single zip file with the following six names in the root directory: 0) DisplayServiceDocument.rb 1) GetSupplierGivenID.rb 2) GivenProductIDListOrders.rb 3) GivenProductIDListCustomerContactNames.rb 4) Sales_Totals_By_Amounts.rb 5) NorthWindWebApp Note, this is the directory name of your Rails application. 6) Heroku.txt Note, this is a text file containing directions on how to visit your web application on Heroku. It should also say whether or not it uses jQuery mobile for a responsive design. Getting familiar with the OData data source =========================================== Structured data is a major source of information for decision makers in almost all organizations. Visit the Northwind OData service document with a browser. http://services.odata.org/Northwind/Northwind.svc/ The Northwind OData data represents a typical relational database. 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-version-2-0/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-version-2-0/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' # 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