This is a rails application providing a REST API for superhero related data, including names, superhero names and abilities.
config/routes.rb
contains all routes for the application defined using rails resources.
bundle install
rails s
rails c
Validations in the `HeroPower` model:
- `strength` must be one of the following values: 'Strong', 'Weak', 'Average'
Validations in the `Power` model:
- `description` must be present and at least 20 characters long
The REST API is described below.
GET /heroes
curl -i -H 'Accept: application/json' http://localhost:3000/heroes
HTTP/1.1 200 OK
Date: Thu, 16 Sept 2022 12:36:30 GMT
Status: 200 OK
Connection: close
Content-Type: application/json
Content-Length: 8
[
{ "id": 1, "name": "Kamala Khan", "super_name": "Ms. Marvel" },
{ "id": 2, "name": "Doreen Green", "super_name": "Squirrel Girl" },
{ "id": 3, "name": "Gwen Stacy", "super_name": "Spider-Gwen" }
]
GET /powers
curl -i -H 'Accept: application/json' http://localhost:3000/powers
HTTP/1.1 200 OK
Date: Thu, 16 Sept 2022 12:36:30 GMT
Status: 200 OK
Connection: close
Content-Type: application/json
Content-Length: 8
[
{
"id": 1,
"name": "super strength",
"description": "gives the wielder super-human strengths"
},
{
"id": 1,
"name": "flight",
"description": "gives the wielder the ability to fly through the skies at supersonic speed"
}
]
GET /heroes/:id
curl -i -H 'Accept: application/json' http://localhost:3000/heroes/1
HTTP/1.1 200 OK
Date: Thu, 16 Sept 2022 12:36:30 GMT
Status: 200 OK
Connection: close
Content-Type: application/json
Content-Length: 141
{
"id": 1,
"name": "Kamala Khan",
"super_name": "Ms. Marvel",
"powers": [
{
"id": 1,
"name": "super strength",
"description": "gives the wielder super-human strengths"
},
{
"id": 2,
"name": "flight",
"description": "gives the wielder the ability to fly through the skies at supersonic speed"
}
]
}
GET /heroes/:id
curl -i -H 'Accept: application/json' http://localhost:3000/heroes/106698383
HTTP/1.1 404 Not Found
Date: Thu, 16 Sept 2022 12:36:30 GMT
Status: 404 Not Found
Connection: close
Content-Type: application/json
Content-Length: 35
{"error":"Hero Not found"}
GET /powers/:id
curl -i -H 'Accept: application/json' http://localhost:3000/powers/1
HTTP/1.1 200 OK
Date: Thu, 16 Sept 2022 12:36:30 GMT
Status: 200 OK
Connection: close
Content-Type: application/json
Content-Length: 141
{
"id": 1,
"name": "super strength",
"description": "gives the wielder super-human strengths"
}
GET /powers/:id
curl -i -H 'Accept: application/json' http://localhost:3000/powers/106698383
HTTP/1.1 404 Not Found
Date: Thu, 16 Sept 2022 12:36:30 GMT
Status: 404 Not Found
Connection: close
Content-Type: application/json
Content-Length: 35
{"error":"Power Not found"}
POST /hero_powers
curl -i -H 'Accept: application/json' -d 'comment=great' http://localhost:3000/hero_powers
{
"strength": "Average",
"power_id": 1,
"hero_id": 3
}
HTTP/1.1 201 Created
Date: Thu, 16 Sept 2022 12:36:30 GMT
Status: 201 Created
Connection: close
Content-Type: application/json
Location: /hero_powers/9
Content-Length: 9
{
"id": 1,
"name": "Kamala Khan",
"super_name": "Ms. Marvel",
"powers": [
{
"id": 1,
"name": "super strength",
"description": "gives the wielder super-human strengths"
},
{
"id": 2,
"name": "flight",
"description": "gives the wielder the ability to fly through the skies at supersonic speed"
}
]
}
PATCH /powers/:id
curl -i -H 'Accept: application/json' -X PATCH -d 'description=Updated description' http://localhost:3000/powers/9
{
"description": "Updated description"
}
HTTP/1.1 200 OK
Date: Thu, 16 Sept 2022 12:36:30 GMT
Status: 200 OK
Connection: close
Content-Type: application/json
Content-Length: 10
{
"id": 1,
"name": "super strength",
"description": "Updated description"
}