An API server of smaT for shinshu-futureapp contest. This API server is running on Ruby on Rails.
- ruby 2.5
- gem 2.7
- bundler 1.16
$ bundle install --path=vendor/bundle
$ bundle exec rails db:migrate
$ bundle exec rails db:seed
$ bundle exec rails g rspec:integration NAME
$ bundle exec rspec
- GET
/v1/teachers/:teacher_id/exams
- POST
/v1/teachers/:teacher_id/exams
- GET
/v1/exams/:exam_id/questions
- GET
/v1/exams/:exam_id/questions
- POST
/v1/exams/:exam_id/questions
- GET
/v1/questions/:id
- PATCH/PUT
/v1/questions/:id
- PATCH/PUT
/v1/exams/:id
- POST
/v1/exams/:id/open
- POST
/v1/exams/:id/close
- GET
/v1/exmas/:exam_id/results
under development - GET
/v1/teachers/:id
- POST
/v1/teachers/
- POST
/v1/auth/teacher/login
- GET
/v1/rooms/:room_id/questions
- GET
/v1/rooms/:room_id/questions/:id
- POST
/v1/results
under development
- HOSTは
smat-api-dev.herokuapp.com
のようなAPIサーバーのホスト先URIを示す params
における*
は空白を許さないという意味
指定した教員教員(:teacher_id)が作成した試験情報一覧を返す。 試験内の問題は含まない。
Nothing.
- Array of Exam's JSON
- HTTP 200
- get
Couldn't find
message - HTTP 400
$ curl https://HOST/v1/teachers/1/exams | jq
[
{
"id": 1,
"title": "Exam0",
"status": 0,
"room_id": -1,
"teacher_id": 1,
"created_at": "2018-11-22T19:05:33.871+09:00",
"updated_at": "2018-11-22T19:05:33.871+09:00",
"description": null
},
{
"id": 2,
"title": "Exam1",
"status": 0,
"room_id": -1,
"teacher_id": 1,
"created_at": "2018-11-22T19:05:33.877+09:00",
"updated_at": "2018-11-22T19:05:33.877+09:00",
"description": null
},
...
指定した教員に新しく試験を作成する。
Name | Type |
---|---|
title | String* |
description | String |
- single Exam's JSON created by params
- HTTP 201
Validation failed
message- HTTP 400
# valid request
$ curl -X POST -H 'Content-Type: application/json' https://HOST/v1/teachers/1/exams -d '{"title": "Temp Exam", "description": "For write document"}' | jq
{
"id": 13,
"title": "Temp Exam",
"status": 0,
"room_id": -1,
"teacher_id": 1,
"created_at": "2018-11-22T20:49:56.802+09:00",
"updated_at": "2018-11-22T20:49:56.802+09:00",
"description": "For write document"
}
# invalid request
$ curl -X POST -H 'Content-Type: application/json' https://HOST/v1/teachers/1/exams -d '{"title": "", "description": ""}'
{
"message":"Validation failed: Title can't be blank"
}
INFO: description
accepts empty
指定した試験(:exam_id)の問題情報一覧を返す。
Nothing.
- Array of Question's JSON
- HTTP 200
Couldn't find Exam
message- HTTP 400
# Exam exists
$ curl https://HOST/v1/exams/1/questions | jq
[
{
"id": 2,
"question_type": "Math",
"ans_smatex": "+-2",
"exam_id": 1,
"created_at": "2018-11-22T19:05:33.967+09:00",
"updated_at": "2018-11-22T19:05:33.967+09:00",
"latex": "\\sqrt{4}",
"smatex": "#{4}",
"ans_latex": "+-2"
},
{
"id": 3,
"question_type": "Math",
"ans_smatex": "3025",
"exam_id": 1,
"created_at": "2018-11-22T19:05:33.972+09:00",
"updated_at": "2018-11-22T19:05:33.972+09:00",
"latex": "55^2",
"smatex": "55^2",
"ans_latex": "3025"
},
...
# Exam doesn't exists
$ curl https://HOST/v1/exams/0/questions | jq
{
"message": "Couldn't find Exam with 'id'=0"
}
指定した試験に新しく問題を作成する。
Name | Type |
---|---|
smatex | String* |
latex | String* |
ans_smatex | String* |
ans_latex | String* |
question_type | String* |
- single Question's JSON created by params
- HTTP 201
- get
Validation failed
message - HTTP 400
# valid request
$ curl -X POST -H 'Content-Type: application/json' https://HOST/v1/exams/1/questions -d '{"smatex": "[2]%[4]", "latex": "\\frac{2}{4}", "ans_smatex": "[1]%[2]", "ans_latex": "\\frac{1}{2}", "question_type": "Math"}' | jq
{
"id": 60,
"question_type": "Math",
"ans_smatex": "[1]%[2]",
"exam_id": 1,
"created_at": "2018-11-22T22:21:26.675+09:00",
"updated_at": "2018-11-22T22:21:26.675+09:00",
"latex": "\\frac{2}{4}",
"smatex": "[2]%[4]",
"ans_latex": "\\frac{1}{2}"
}
# invalid request
$ curl -X POST -H 'Content-Type: application/json' https://smat-api-dev.herokuapp.com/v1/exams/1/questions -d '{"smatex": "", "latex": "", "ans_smatex": "", "ans_latex": "", "question_type": ""}' | jq
{
"message": "Validation failed: Smatex can't be blank, Latex can't be blank, Question type can't be blank, Ans smatex can't be blank, Ans latex can't be blank"
}
指定された問題(:question_id)の情報を返す。
Nothing.
- single Question's JSON
- HTTP 200
Couldn't find Question
message- HTTP 400
# question exists
$ curl https://HOST/v1/questions/1 | jq
{
"id": 1,
"question_type": "Math",
"ans_smatex": "166375",
"exam_id": 1,
"created_at": "2018-11-22T19:05:33.961+09:00",
"updated_at": "2018-11-22T19:20:29.226+09:00",
"latex": "55^3",
"smatex": "55^3",
"ans_latex": "166375"
}
# question doesn't exists
$ curl https://HOST/v1/questions/0 | jq
{
"message": "Couldn't find Question with 'id'=0"
}
指定された問題の情報を更新する。
Name | Type |
---|---|
smatex | String* |
latex | String* |
ans_smatex | String* |
ans_latex | String* |
question_type | String* |
- single Question's JSON updated by params
- HTTP 200
- get
Validation failed
message - HTTP 400
# request is valid
$ curl -X PUT -H 'Content-Type: application/json' https://HOST/v1/questions/1 -d '{"smatex": "#{2}", "latex": "\\sqrt{2}", "ans_smatex": "1.41421356", "ans_latex": "1.41421356", "question_type": "Matematics"}' | jq
{
"id": 1,
"smatex": "#{2}",
"latex": "\\sqrt{2}",
"ans_smatex": "1.41421356",
"ans_latex": "1.41421356",
"question_type": "Matematics",
"exam_id": 1,
"created_at": "2018-11-22T19:05:33.961+09:00",
"updated_at": "2018-11-22T22:41:06.586+09:00"
}
# request is invalid
$
curl -X PUT -H 'Content-Type: application/json' https://HOST/v1/questions/1 -d '{"smatex": "", "latex": "", "ans_smatex": "", "ans_latex": "", "question_type": ""}' | jq
{
"message": "Validation failed: Smatex can't be blank, Latex can't be blank, Question type can't be blank, Ans smatex can't be blank, Ans latex can't be blank"
}
指定された試験の情報を更新する。
Name | Type |
---|---|
title | String* |
description | String |
- single Exam's JSON updated by params
- HTTP 200
Validation failed
message- HTTP 400
# valid request
$ curl -X PUT -H 'Content-Type: application/json' https://HOST/v1/exams/1 -d '{"title": "Updated Exam", "description": "Description New"}' | jq
{
"room_id": -1,
"status": 0,
"id": 1,
"title": "Updated Exam",
"description": "Description New",
"teacher_id": 1,
"created_at": "2018-11-22T19:05:33.871+09:00",
"updated_at": "2018-11-22T22:46:27.038+09:00"
}
# invalid request
$ curl -X PUT -H 'Content-Type: application/json' https://HOST/v1/exams/1 -d '{"title": "", "description": ""}' | jq'
{
"message":"Validation failed: Title can't be blank"
}
INFO: description
accepts empty
指定した試験を公開(生徒が入室できる)する。
Nothing.
- single Exam's JSON which is specified room_id
- HTTP 200
Couldn't find
message- HTTP 400
Couldn't find
message- HTTP 400
# exam exists
$ curl -X POST https://HOST/v1/exams/1/open | jq
{
"room_id": 435,
"status": 1,
"id": 1,
"title": "Updated Exam",
"teacher_id": 1,
"created_at": "2018-11-22T19:05:33.871+09:00",
"updated_at": "2018-11-22T23:03:56.534+09:00",
"description": "Description New"
}
# exam already opened
$ curl -X POST https://HOST/v1/exams/1/open | jq
{
"message": "Couldn't find Exam with 'id'=1 [WHERE \"exams\".\"status\" = $1]"
}
# exam doesn't exist
$ curl -X POST https://HOST/v1/exams/0/open | jq
{
"message": "Couldn't find Exam with 'id'=0 [WHERE \"exams\".\"status\" = $1]"
}
指定した試験を非公開(生徒の入室を不可に)にする。
Nothing.
- single Exam's JSON which set room_id = -1
- HTTP 200
Couldn't find
message- HTTP 400
# exam exists
$ curl -X POST https://HOST/v1/exams/1/close | jq
{
"room_id": -1,
"status": 0,
"id": 1,
"title": "Updated Exam",
"teacher_id": 1,
"created_at": "2018-11-22T19:05:33.871+09:00",
"updated_at": "2018-11-22T23:13:50.087+09:00",
"description": "Description New"
}
# exam doesn't exist
$ curl -X POST https://HOST/v1/exams/0/close | jq
{
"message": "Couldn't find Exam with 'id'=0 [WHERE \"exams\".\"status\" = $1]"
}
指定した試験における結果一覧を返す。
Nothing.
- Array of Result's JSON through its question in specified Exam
- HTTP 200
Couldn't find
message- HTTP 400
Add in the future...
指定した教員(:id)の情報を返す。
Authorization: Token|Bearer hogehoge
ヘッダによる認証が必要。
Nothing.
- shingle Teacher's JSON specified by id
- HTTP 200
- get
Couldn't find
message - HTTP 400
- get
Authentication required
message - HTTP 401
# token is valid
$ curl -H 'Authorization: Token hogehoge' https://HOST/v1/teachers/1 | jq
{
"id": 1,
"fullname": "Smart Teacher",
"username": "smat",
"password_digest": "50ecc45020be014e68d714cd076007e84a9621d9a5e589a916e45273014830b399d143a57f525554bfe9e751d97fe0fa884dbdea7b07721723b4eff39e9d28ad",
"token": "DQDjUXRfpFquNRQPbjuaemS4",
"created_at": "2018-11-22T19:05:33.818+09:00",
"updated_at": "2018-11-22T19:05:33.818+09:00"
}
# token is valid but id is invalid
$ curl -H 'Authorization: Token hogehoge' https://HOST/v1/teachers/0 | jq
{
"message": "Couldn't find Teacher with 'id'=0"
}
# token is invalid(not specified)
$ curl https://HOST/v1/teachers/1 | jq
{
"message": "Authorization required"
}
指定された内容で教員を新しく作成する。
Name | Type |
---|---|
fullname | String* |
username | String* |
password_digest | String* |
- single Teacher's JSON created by params
- HTTP 200
- get
Validation failed
message - HTTP 400
# request is valid
$ curl -X POST -H 'Content-Type: application/json' https://HOST/v1/teachers -d '{"fullname": "John Henecy", "username": "john_h", "password_digest": "5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8"}' | jq
{
"id": 2,
"fullname": "John Henecy",
"username": "john_h",
"password_digest": "5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8",
"token": "AdE4qxCC1iNEQ9hyvV9zjZnW",
"created_at": "2018-11-22T23:47:39.800+09:00",
"updated_at": "2018-11-22T23:47:39.800+09:00"
}
# request is invalid
$ curl -X POST -H 'Content-Type: application/json' https://smat-api-dev.herokuapp.com/v1/teachers -d '{"fullname": "", "username": "", "password_digest": ""}' | jq
{
"message": "Validation failed: Fullname can't be blank, Username can't be blank, Password digest can't be blank"
}
ユーザーネームとパスワードを用いて認証に必要なTokenを返す。
Name | Type |
---|---|
username | String* |
password_digest | String* |
- returns Token specified by username
- HTTP 200
- get
Couldn't find
message - HTTP 400
# request is valid
$ curl -X POST -H 'Content-Type: application/json' https://HOST/v1/auth/teacher/login -d '{"username": "john_h", "password_digest": "5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8"}' | jq
{
"token": "hogehoge"
}
# request is invalid
$ curl -X POST -H 'Content-Type: application/json' https://smat-api-dev.herokuapp.com/v1/auth/teacher/login -d '{"username": "", "password_digest": ""}'
{
"message": "Couldn't find Teacher"
}
指定された試験(:room_id ExamのDB上ではない別のID)における問題情報一覧を返す。
挙動についてはGET /v1/exams/:exam_id/questions
と同様。
指定された試験における問題情報(:id)を1つ返す。
ここでの:id
はDB上のIDではなく、1以上の整数である。
例えば
$ curl https://HOST/v1/rooms/637/questions/1
とすると、room_id=637
である試験における問題一覧の1番目に登録されている問題情報を返す。
$ curl https://HOST/v1/rooms/637/questions/2
のようにすれば該当試験において2番目に登録されている問題情報が、
$ curl https://HOST/v1/rooms/637/questions/3
とすれば3番目の問題情報が...というように、
GET /v1/questions/:id
では必要だったDBにおけるIDを知らずとも問題情報の参照が可能となっている。
生徒の回答結果を送信、保存する。
{"q_id": "", "j": "", "c": ""}
returns Results submitted.
.
Improve it to return Invalid request format.
message.
Name | Type |
---|---|
q_id | String* |
j | String* |
c | String* |
- get
Results submitted.
message - HTTP 201
- get
Invalid request format.
message - HTTP 400
- get
Invalid request format.
message - HTTP 400
# request is valid
$ curl -X POST -H 'Content-Type: application/json' https://HOST/v1/results -d '{"q_id": "1,2,3,4", "j": "0,0,1,0", "c": "3,3,5,3"}' | jq
{
"message": "Results submitted."
}
# request is empty
$ curl -X POST -H 'Content-Type: application/json' https://HOST/v1/results -d '{}' | jq
{
"message": "Invalid request format."
}
# request's format is invalid
$ curl -X POST -H 'Content-Type: application/json' https://HOST/v1/results -d '{"q_id": "1,2,34", "j": "0", "c": "3,15"}' | jq
{
"message": "Invalid request format."
}