/rpub

Primary LanguageRuby


RPUB - Simple mock API

Enpower your mock experience with rpub!

Table of Contents
  1. About The Project
  2. Getting Started
  3. Usage
  4. Examples
  5. Testing

About The Project

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.

Getting Started

Installation

  1. Clone the repo
    $ git clone https://github.com/sarslanoglu/rpub.git
  2. Get inside folder
    $ cd rpub
  3. Install gems
    $ bundle install
  4. 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.

(back to top)

Usage

In order to start using first serve rails to your local

$ rails s

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\" }\""
            }
        }
    }
}

(back to top)

Examples

List endpoints

Request

GET /endpoints HTTP/1.1
Accept: application/vnd.api+json

Response

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

Request

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\" }\""
            }
        }
    }
}

Response

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

Request

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\" }\""
            }
        }
    }
}

Response

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

Request

DELETE /endpoints/12345 HTTP/1.1
Accept: application/vnd.api+json

Response

HTTP/1.1 204 No Content
Sample scenario

1. Create an endpoint

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\" }\""
            }
        }
    }
}

2. Send request to recently created endpoint

GET /hello HTTP/1.1
Accept: application/json

HTTP/1.1 200 OK
Content-Type: application/json

{ "message": "Hello, world" }

(back to top)

Testing

To run all tests just run

$ bundle exec rspec

To run rubocop style checker just run

$ rubocop

(back to top)