Web-api with Ruby on Rails - dis.uniroma1.itberaldi/MACC/Web-api_with_RoR_19.pdf · Step 1: create...

23
Web-api with Ruby on Rails

Transcript of Web-api with Ruby on Rails - dis.uniroma1.itberaldi/MACC/Web-api_with_RoR_19.pdf · Step 1: create...

Web-api with Ruby on Rails

� https://www.youtube.com/watch?v=CuAZ54YknpE

Installation requirements

� Rails 5.2� Ruby 2.5� Postgres 10� Heroku account � Heroku CLI� Git

Step 1: create a heroku app

-create a (free) account on heroku-create a new app-in the deploy tab, you will see the steps requiredto deploy an app from the pc- In this example the name of the app is apibackendmacc

Step 1.

� Install local CLI� heroku regions

Step 2. Customize postgreSQL

� During postgres installation set a password for the postgres user

� If not set, use createuser –s postgres

� From pgAdmin create a new user (that will be used later from RoR) using the postgres password

� For example, create login role ‘android’, password ‘android’

� Provide privilegies to login and create a db

Step 3. Create a local RoR app� rails new apibackendmacc --api --database=postgresql –T� (sudo env ARCHFLAGS='-arch x86_64' gem install pg)� This uses postgres (heroku does not support SQLite3)� In the /config/database.yml file set user and password for the

development db (both android in our case)� Type rake db:create to create the db� Type rails server

Step 4/1. Create a test endpoint

� Type rails g controller calc� This will generate an empty controller template

� In file config\routes.rb add: root ‘calc#test’

� Type rake routes

Step 4/2: create test endpoint

� Add a test method to the CalcController

� type rails s

Step 5: deploying on heroku

apibackendmacc

rvm install ruby --latest

Step 5: deploying on heroku

What we have created?

FUNCTION(no par)

Implementing a remote calculator

Possible architectures

FUNCTION(no par) FUNCTION

+-x/

URI

Single URI

FUNCTION

+-x/

URI per operation

RESOURCE

CRUD

URI per operation

HTTP verbs mapped on CRUDàRestfull API

Google Book API: an example of Rest API

Introduction

� We have seen how to implement a web application for storing movies and their ratings

� We now wants to ‘externalize’ such an application as a service to be accessed by a mobile app

� We need to modify the controller so that it is able to interact with an app (rather than a human)

� We will use the REST architecture with RESTfull API� We need implement the client side

Server side implementation steps

� Modify the router so that it delivers the REST call to the appropriate code

� Test routes and behavior with curl

Sequence diagram

:H T T P C lie nt

:H T T P Se rve r (W e b R ick)

:R a ils Ro u te r :R a ils C o ntro lle r :R a ils M o d e l

DB

Server side sw architecture

GUI HTTP

OPERATION HTTP MESSAGE HTTP ReplyCreate POST + josn Ok, or error

Read GET ?id=id OKRead GET list Json list of movies

Update PUT + json data OKDelete DELETE id=.. OK, error

ROUTER

CONTROLLER

DB

MODEL

RoR

Client side Implementation step

� Sketch a storyboard� Implement the storyboard with views and connections among them

using fake data� Validate the storyboard

� Replace fake data with correct data� make a test for each added functionality

GUI design (storyboard)Card (each card is associated to a movie)

Long pressà update or delete

FAB+

Details

U D

Alert Message

Confirm

EditField

Confirm

Implementing the backend