Contracts API is a simple Rails API designed to manage vendor contracts and its lifetime.
- Ruby 2.4.1
- PostgreSQL
Clone the repository, install gems and setup the database running the following commands:
git clone git@github.com:evandrodutra/contracts.git
cd contracts
bundle install
bundle exec bin/setup
bundle exec rails s
To run the test environment execute rspec:
bundle exec rspec
All requests must use Content-Type: application/json
header.
For a private endpoint you must specify the Authorization header using the JWT token generated for your account, example:
curl -X GET -i -H "Authorization: Bearer MY_JWT_TOKEN" -H "Content-Type: application/json" "http://localhost:3000/contracts/:id"
All POST actions that sends a content body must use the JSON-API format, example:
{
"data": {
"attributes": {
"full_name": "Douglas Adams",
"email": "dm@example.com",
"password": "humans"
}
}
}
When the response has a content body instead of only the HTTP status, the returned data follows the JSON-API format, example:
{
"data": {
"id": "db22fed2-0c07-4737-be36-f082146fc8d4",
"type": "users",
"attributes": {
"full-name": "Douglas Adams",
"email": "dm@example.com",
"jwt": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyIjoiZGIyMmZlZDItMGMwNy00NzM3LWJlMzYtZjA4Mj"
}
}
}
When the response contains entity errors the response body returns its description and status, example:
{
"errors": [
{
"status": 422,
"detail": "Full name should not be empty",
"source": {
"pointer": "/data/attributes/full_name"
}
},
{
"status": 422,
"detail": "Email is already taken",
"source": {
"pointer": "/data/attributes/email"
}
}
]
}
Crates a user and returns its JWT token.
POST /users
Content-Type: "application/json"
{
"data": {
"attributes": {
"full_name": "Douglas Adams",
"email": "dm@example.com",
"password": "humans"
}
}
}
Attribute | Description |
---|---|
full_name | user name |
valid email | |
password | user password |
201 Created
Content-Type: "application/vnd.api+json"
{
"data": {
"id": "f3a17518-3c85-441b-bb99-448fd7da0a00",
"type": "users",
"attributes": {
"full-name": "Douglas Adams",
"email": "dm@example.com",
"jwt": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyIjoiZjNhMTc1MTgtM2M4NS00NDFiLWJiOTktNDQ4ZmQ3ZGEwYTAwIn0.8ilUS6nx8OlM1KLHvHV6_PE1pCoiaeq_TQczvLl6rzg",
"created-at": "2018-01-15T12:54:31.818Z",
"updated-at": "2018-01-15T12:54:31.818Z"
}
}
}
Crates a contract for a given user-token and returns its data:
POST /contracts
Content-Type: "application/json"
Authorization: "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyIjoiZjNhMTc1MTgtM2M4NS00NDFiLWJiOTktNDQ4ZmQ3ZGEwYTAwIn0.8ilUS6nx8OlM1KLHvHV6_PE1pCoiaeq_TQczvLl6rzg"
{
"data": {
"attributes": {
"vendor": "DB",
"starts_on": "30-01-2018",
"ends_on": "30-12-2018",
"price": "328.79"
}
}
}
Attribute | Description |
---|---|
vendor | vendor name |
starts_on | contract start date |
ends_on | contract end date |
price | contract price |
201 Created
Content-Type: "application/vnd.api+json"
{
"data": {
"id": "2c31b4fb-ac16-4fce-8634-16f3dd2a093c",
"type": "contracts",
"attributes": {
"vendor": "DB",
"starts-on": "2018-01-30 00:00:00",
"ends-on": "2018-12-30 00:00:00",
"price": "328.79",
"created-at": "2018-01-15T13:02:43.346Z",
"updated-at": "2018-01-15T13:02:43.346Z"
}
}
}
Returns the contract for a given user-token and returns its data:
GET /contracts/2c31b4fb-ac16-4fce-8634-16f3dd2a093c
Content-Type: "application/json"
Authorization: "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyIjoiZjNhMTc1MTgtM2M4NS00NDFiLWJiOTktNDQ4ZmQ3ZGEwYTAwIn0.8ilUS6nx8OlM1KLHvHV6_PE1pCoiaeq_TQczvLl6rzg"
Attribute | Description |
---|---|
id | contract id |
200 OK
Content-Type: "application/vnd.api+json"
{
"data": {
"id": "2c31b4fb-ac16-4fce-8634-16f3dd2a093c",
"type": "contracts",
"attributes": {
"vendor": "DB",
"starts-on": "2018-01-30 00:00:00",
"ends-on": "2018-12-30 00:00:00",
"price": "328.79",
"created-at": "2018-01-15T13:02:43.346Z",
"updated-at":
5D3B
"2018-01-15T13:02:43.346Z"
}
}
}
Deletes a contract for a given user-token:
DELETE /contracts/2c31b4fb-ac16-4fce-8634-16f3dd2a093c
Authorization: "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyIjoiZjNhMTc1MTgtM2M4NS00NDFiLWJiOTktNDQ4ZmQ3ZGEwYTAwIn0.8ilUS6nx8OlM1KLHvHV6_PE1pCoiaeq_TQczvLl6rzg"
Attribute | Description |
---|---|
id | contract id |
204 No content