Table of Contents
Why project name is rpub? It's easy. These kind of public repos are open to search by other users. So for protecting project name from search engines a simple letter substitution cipher ROT 13 is used here. If you are lazy to change letters like me just use this website
rpub is a simple endpoint mocker with 4 different endpoints to create/update/get/delete endpoints and many more endpoints to serve for mocking when user created more.
- Clone the repo
$ git clone https://github.com/sarslanoglu/rpub.git
- Get inside folder
$ cd rpub
- Install gems
$ bundle install
- Create and migrate database & test database
$ rails db:create $ rails db:migrate $ rails db:migrate RAILS_ENV=test
App is now ready to use. Next step will be about usage.
In order to start using first serve rails to your local
$ rails s
- GET http://localhost:3000/endpoints to get all endpoints
- POST http://localhost:3000/endpoints to create a new endpoint
- PATCH http://localhost:3000/endpoints{/:id} to update id matched endpoint
- DELETE http://localhost:3000/endpoints{/:id} to delete id matched endpoint
After creating endpoint objects all created VERB & PATH pairs will be available on API. You can find many examples about requests on examples section.
Endpoint model is fairly simple and looks like this:
Endpoint {
id Unique string field (system auto assigns, no user interaction)
verb String (required from user input)
path String (required from user input)
response {
code Integer (required from user input)
headers Hash<String, String> (optional from user input)
body String (optional from user input)
}
}
There is a uniqueness relation on verb & path. This means that if there is a matching pair in the system new one can't be added. Previous one should be updated instead.
Example endpoint object on request:
{
"data": {
"type": "endpoints",
"attributes": {
"verb": "GET",
"path": "/greeting",
"response": {
"code": 200,
"headers": {},
"body": "\"{ \"message\": \"Hello, world\" }\""
}
}
}
}
List endpoints
GET /endpoints HTTP/1.1
Accept: application/vnd.api+json
HTTP/1.1 200 OK
Content-Type: application/vnd.api+json
{
"data": [
{
"type": "endpoints",
"id": "12345",
"attributes": [
"verb": "GET",
"path": "/greeting",
"response": {
"code": 200,
"headers": {},
"body": "\"{ \"message\": \"Hello, world\" }\""
}
]
}
]
}
Create endpoint
POST /endpoints HTTP/1.1
Content-Type: application/vnd.api+json
Accept: application/vnd.api+json
{
"data": {
"type": "endpoints",
"attributes": {
"verb": "GET",
"path": "/greeting",
"response": {
"code": 200,
"headers": {},
"body": "\"{ \"message\": \"Hello, world\" }\""
}
}
}
}
HTTP/1.1 201 Created
Content-Type: application/vnd.api+json
{
"data": {
"type": "endpoints",
"id": "12345",
"attributes": {
"verb": "GET",
"path": "/greeting",
"response": {
"code": 200,
"headers": {},
"body": "\"{ \"message\": \"Hello, world\" }\""
}
}
}
}
Update endpoint
PATCH /endpoints/12345 HTTP/1.1
Content-Type: application/vnd.api+json
Accept: application/vnd.api+json
{
"data": {
"type": "endpoints",
"id": "12345"
"attributes": {
"verb": "POST",
"path": "/greeting",
"response": {
"code": 201,
"headers": {},
"body": "\"{ \"message\": \"Hello, everyone\" }\""
}
}
}
}
HTTP/1.1 200 OK
Content-Type: application/vnd.api+json
{
"data": {
"type": "endpoints",
"id": "12345",
"attributes": {
"verb": "POST",
"path": "/greeting",
"response": {
"code": 201,
"headers": {},
"body": "\"{ \"message\": \"Hello, everyone\" }\""
}
}
}
}
Delete endpoint
DELETE /endpoints/12345 HTTP/1.1
Accept: application/vnd.api+json
HTTP/1.1 204 No Content
Sample scenario
POST /endpoints HTTP/1.1
Content-Type: application/vnd.api+json
Accept: application/vnd.api+json
{
"data": {
"type": "endpoints",
"attributes": {
"verb": "GET",
"path": "/hello",
"response": {
"code": 200,
"headers": {
"Content-Type": "application/json"
},
"body": "\"{ \"message\": \"Hello, world\" }\""
}
}
}
}
HTTP/1.1 201 Created
Content-Type: application/vnd.api+json
{
"data": {
"type": "endpoints",
"id": "12345",
"attributes": {
"verb": "GET",
"path": "/hello",
"response": {
"code": 200,
"headers": {
"Content-Type": "application/json"
},
"body": "\"{ \"message\": \"Hello, world\" }\""
}
}
}
}
GET /hello HTTP/1.1
Accept: application/json
HTTP/1.1 200 OK
Content-Type: application/json
{ "message": "Hello, world" }
To run all tests just run
$ bundle exec rspec
To run rubocop style checker just run
$ rubocop