A simple content manager backend api server with RoR and Grape.
rails srails db:fixtures:load�There are 3 accounts:
| \ | password | is_admin | |
|---|---|---|---|
| root | [email protected] | root | true |
| user1 | [email protected] | user | false |
| user2 | [email protected] | user | false |
You need to get the auth token first for the following apis
curl -X POST \
http://localhost:3000/api/v0/users/login \
-H 'content-type: application/json' \
-d '{"email":"[email protected]","password":"root"}
'and you'll get the response like below
{
"token":"...your-token...",
"exp":"2006-01-02T15:04:05Z"
}Please put the token in the Authorization header.
This Token will be expired in 1 day.
Create
curl -X POST \
http://localhost:3000/api/v0/courses \
-H 'authorization: your-token' \
-H 'content-type: application/json' \
-d '{
"topic":"course_topic",
"description":"course_description",
"price":0.0,
"currency":"NTD",
"category":"computer_science",
"url":"https://course.com",
"expiration":86400,
"available": "true"
}
'return value
{
"location": "/api/v0/courses/<course_id>",
"created_at": "2006-01-02T15:04:05Z"
}"topic": string, must in the range of 1 to 20 words.
"description": string, must in the range of 1 to 20 words.
"price": float, must greater than or equal to 0.
"currency": enum, currently only support NTD, USD or EUR.
"category": string, can't be blank.
"url": url, can't be blank.
"expiration": int, the course available window in secound after purchasing, must in the range of 86400 to 86400*30.
"available": boolean, the course is available or not.
Update
curl -X PUT \
http://localhost:3000/api/v0/courses/<course_id> \
-H 'authorization: your-token' \
-H 'content-type: application/json' \
-d '{
"topic":"course_topic",
"description":"course_description",
"price":0.0,
"currency":"NTD",
"category":"computer_science",
"url":"https://course.com",
"expiration":86400,
"available": "true"
}
'same as Create api
Delete
curl -X DELETE \
http://localhost:3000/api/v0/courses/<course_id> \
-H 'authorization: your-token' \
-H 'content-type: application/json'Get
curl -X GET \
http://localhost:3000/api/v0/courses/<course_id> \
-H 'authorization: your-token' \
-H 'content-type: application/json'return value
{
"course": {
"id": 207281424,
"topic": "ruby",
"description": "ruby tutorial",
"price": 100,
"currency": "NTD",
"category": "computer_science",
"url": "https://course_url.com.tw/ruby",
"expiration": 86400,
"available": true,
"created_at": "2020-07-11T08:44:19.991Z"
}
}List
curl -X GET \
http://localhost:3000/api/v0/courses \
-H 'authorization: your-token' \
-H 'content-type: application/json'"page": int, for paging, can't be negative, default 1.
"per_page": int, for paging, must between 1 to 50, default 10.
{
"courses": [
{
"id": 207281424,
"topic": "ruby",
"description": "ruby tutorial",
"price": 100,
"currency": "NTD",
"category": "computer_science",
"url": "https://course_url.com.tw/ruby",
"expiration": 86400,
"available": true,
"created_at": "2020-07-11T08:44:19.991Z"
},
...
],
// paging info
"paginator": {
"page": 1,
"per_page": 3,
"total_page": 2
}
}curl -X GET \
http://localhost:3000/api/v0/user/courses \
-H 'authorization: your-token'"available": boolean, true to show the courses which are still available for current user.
"category": string, filter by the specific category name.
"page": int, for paging, can't be negative, default 1.
"per_page": int, for paging, must between 1 to 50, default 10.
{
"purchased_courses": [
{
"id": 238762669,
"course_id": 207281424,
"topic": "ruby",
"description": "ruby tutorial",
"price": 100,
"currency": "NTD",
"category": "computer_science",
"url": "https://course_url.com.tw/ruby",
"expiration": 86400,
"created_at": "2020-07-11T08:44:19.991Z",
"pay_by": "ibon"
},
...
],
// paging info
"paginator": {
"page": 1,
"per_page": 3,
"total_page": 2
}
}curl -X POST \
http://localhost:3000/api/v0/user/purchase-courses/<course_id> \
-H 'authorization: your-token' \
-H 'content-type: application/json' \
-d '{"pay_by": "<the_way_you_pay_the_bill>"}'"pay_by": enum, currently only support ibon, visa, apple_pay, and free.
List
curl -X GET \
http://localhost:3000/api/v0/usersCreate
curl -X POST \
http://localhost:3000/api/v0/users/ \
-H 'content-type: application/json' \
-d '{"email":"email","password":"root", "admin":"true"}'"email": string, email address as user id.
"password": string, user password.
"admin": boolean, this user is admin or not.
Update
curl -X PUT \
http://localhost:3000/api/v0/users/<user_id> \
-H 'content-type: application/json' \
-d '{"email":"email","password":"root", "admin":"true"}'same as Create api