The service is hosted online
To run the app on your own machine, you must have Python 2.7.x
and pip
installed.
The preferred way of installing the dependancies is using Virtualenv. If you dont have virtualenv, you need to run sudo make install
.
$ git clone https://github.com/prakhar1989/WordCounter.git
$ cd WordCounter
$ make install
$ chmod +x run
$ ./run
* Running on http://127.0.0.1:8000/
Route | Description |
---|---|
GET / | Return pre-generated random text |
GET /?random=true | Generate and return random text |
POST / | Validate request |
GET /admin | Add new data |
GET /
and GET /?random=true
return a JSON object of the following schema
{
"text": "she sells sea shells at the seashore he sells sea seals at the seashore",
"token": 846764,
"words": [ "seashore", "he", "she", "at"]
}
where words
is an array of excluded words.
POST /
accepts data in JSON
in the following format
{
"text": "the quick brown fox jumped over the lazy dog",
"token": "620770",
"words": {
"the": "2",
"quick": "1",
"jumped": "1",
"over": "1",
"dog": "1"
}
}
and returns a status 200 OK
only iff all the following hold true
token
matches thetoken
in the response given to the clienttext
matches thetext
in the response given to the client- Word count in the
words
object is correct and excludes the words returned in the response given to the client
In all other cases, a status of 400 Bad Request
is returned.
Note: Punctuations matter. Hence fox
and fox.
are considered two different words.
Below is a sample cURL
request to /
$ curl -X POST -H "Content-Type:application/json" -d '{
"text": "the quick brown fox jumped over the lazy dog",
"token": "620770",
"words": {
"the": "2",
"quick": "1",
"jumped": "1",
"over": "1",
"dog": "1"
}
}' http://127.0.0.1:8000/
The app contains both unit tests and integrations tests. To run simply type the following
$ ./run -test
An additional Postman collection has been bundled in tests/postman-test.json
which can be run the Newman - a node.js test runner that I built for postman. Here's the screenshot below of the output -
├── Makefile -> The build script for the project
├── README.md -> This file
├── app -> App specific helper python file
├── client.py -> Sample client implementation
├── requirements.txt -> Python Dependancies
├── run -> App runner
├── server.py -> Server implementation
├── templates -> HTML template for admin
├── tests -> Integration and unit tests
The application uses SQLite for persistence and validation. To setup and seed the db.
$ ./run -setup
Also included is a sample client implementation that fetches 10 random texts from the server, sends a request and prints the response recieved.
$ python client.py
Hitting 10 requests ...
Text: the quick brown fox jumped ove... Status Code: 200
Text: Iure omnis consequuntur doloru... Status Code: 200
Text: lorem ipsum dolor set amit dol... Status Code: 200
Text: she sells sea shells at the se... Status Code: 200
Text: she sells sea shells at the se... Status Code: 200
The following open-source libraries are used in the project