Web-api with Ruby on Rails - dis.uniroma1.itberaldi/MACC/Web-api_with_RoR_19.pdf · Step 1: create...
-
Upload
trinhthien -
Category
Documents
-
view
214 -
download
0
Transcript of Web-api with Ruby on Rails - dis.uniroma1.itberaldi/MACC/Web-api_with_RoR_19.pdf · Step 1: create...
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 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
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
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