Skip to content

Ruby on Rails 5 API mode Example. a simple CRUD REST: API

Notifications You must be signed in to change notification settings

imkikev/rails-api-mode-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Ruby on Rails 5 API mode Example

This code show you how to use a Rails API mode application using [JSON:API specification] (http://jsonapi.org). this example can:

- Create a session
- Destroy a session
- List users featuring pagination, ordering and filtering
- Create a user
- Show a user
- Update a user
- Delete a user

Versions

  • Ruby version 2.3.1

  • Rails version 5.0.1

  • PostgreSQL 9.5.5

Development

Setup

  1. Get the code:
  • git clone https://github.com/kikewan1/rails-api-mode-example.git
  1. Install Gems
  • bundle install
  1. DB configurations
  • rails db:create
  • rails db:migrate
  • rails db:seed
  1. Listing Users

    • rails s

4.1 Basic

4.2 Filtering

4.3 Pagination

4.4 Ordering

  1. Sessions Client need to create a token session to create, update y delete records, go to console and execute:
  • curl -H "Content-Type: application/vnd.api+json" -X POST -d '{"data": { "type": "sessions", "attributes": { "email": "john@kimpus.com", "password": "test" } }}' http://localhost:3000/sessions

  • Response: {"data":{"id":"1","type":"users","attributes":{"email":"john@kimpus.com","token":"f7SE6x78WV9D6oB8yPgSnMvw","name":"John","lastname":"Wayne"}},"meta":{"licence":"CC-0","authors":["kike"]}}

  1. CRUD User (use token already created)

6.1 Show user:

6.2 Create user:

  • curl -H "Content-Type: application/vnd.api+json" -H "X-Api-Key: f7SE6x78WV9D6oB8yPgSnMvw" -X POST -d '{"data": { "type": "users", "attributes": { "email": "info@kimpus.com", "password": "test", "name":"Enrique", "lastname":"Vargas", "password_confirmation":"test" } }}' http://localhost:3000/api/v1/users

6.3 Update user:

  • Client ll' be able to change name and last name only

  • curl -H "Content-Type: application/vnd.api+json" -H "X-Api-Key: f7SE6x78WV9D6oB8yPgSnMvw" -X PUT -d '{"data": { "type": "users", "attributes": { "email": "test@yahoo.com", "name":"Kike", "lastname":"Vargas A" } }}' http://localhost:3000/api/v1/users/2

6.4 Delete:

  1. Testing
  • This example has testing scripts, to execute:
  • rails test

License

The project is available as open source under the terms of the MIT License.

Troubleshooting

Please create an issue.

About

Ruby on Rails 5 API mode Example. a simple CRUD REST: API

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published