- Run the following commands.
mkdir ruby-odata
cd ruby-odata/
touch config.ru
touch app.rb
touch Gemfile
mkdir views
touch views/products.slim
2. Copy following code to file app.rb
```
#!/usr/bin/env ruby
require ‘ruby_odata’
require ‘sinatra’
require ‘slim’
set :bind, '0.0.0.0'
$headers = ['PRODUCTID', 'CATEGORYID', 'LINEID', 'PRODUCTNAME', 'PRODUCTDESCRIPTION', 'CATEGORYNAME', 'CATEGORYDESCRIPTION', 'LINENAME', 'LINEDESCRIPTION', 'DISCONTINUED', 'CAPACITY', 'SCREENSIZE']
def retrieveProductData
src = OData::Service.new "http://<YOUR_HOSTNAME>:8090/ODataTutorial/product.xsodata", { :username => "<YOUR_USERNAME>", :password=> "<YOUR_PASSWORD>" }
# Querying, pull all products from service 'src'
src.products
products = src.execute
# Generate an 2d array that contains products data
productData = products.map do |p|
# 'p.send h' calls method p."h", e.g. p.PRODUCTID when h == "PRODUCTID"
$headers.collect {|h| p.send h}
end
return productData
end
get '/' do
@data = retrieveProductData
@headers = $headers
slim :products
end
```
- Copy following code to file
views/products.slim
doctype html
html
head
title HANA Express OData Service Example
body
h1 Show data from Table 'Products'
table border="1px"
thead
tr
- @headers.each do |header|
th = header
tbody
- @data.each do |row|
tr
- row.each do |cell|
td = cell
- Copy the below code to the
Gemfile
file in the directory.
```
source “https://rubygems.org”
gem "sinatra"
gem "slim"
gem "ruby_odata"
```
- Run the following commands at the root of the project directory.
```
~ruby-odata$bundle install
# This command should generate ‘Gemfile.lock’ file, which is needed in deploying
~ruby-odata$ruby app.rb
(dev_appserver.py does not support Ruby currently)
```
- Browse the application at
http://localhost:4567
.
The results should look like below.