Skip to content

Latest commit

 

History

History
43 lines (26 loc) · 1.44 KB

README.md

File metadata and controls

43 lines (26 loc) · 1.44 KB

#README

####Environment

Ruby version: 2.2.4

Rails version: 4.2.5.1

##Overview

This ia a REST API client application. It shows how to call REST API from the API provider application 'rest_api_provider' and parse the JSON data. Via this API client application, you can create, read, update and delete data and files on the API provider server.

The API provider 'rest_api_provider' creates two APIs, one for User model and the other for Photo model. User model has text fields only and is designed to require no API authentication for API access, while Photo model contains image field and is designed to require API authentication.

This application uses RestClient gem to parse JSON data and uses 'figaro' gem to hide sensitive information such as API_KEY when accessing third-party's API (i.e. 'rest_api_provider' in our case).

##Call REST_API and parse JSON data If API authenticaion is not requred, e.g. the User api in our case, use the code as below.

url = "http://localhost:3000/users.json"
response = RestClient.get(url)
@users = JSON.parse(response.body)

If API authenticaion is requred, e.g. the Photo api in our case, set url like as below.

url = "http://localhost:3000/api/v1/photos.json?token=22XRYWnwHdMrYthba1PbtAtt"
response = RestClient.get(url)
@photos = JSON.parse(response.body)

That means a valid API_KEY should be passed to url for authentication.