Shorty is a micro service to shorten urls, in the style that TinyURL and bit.ly made popular.
- Ruby 2.3.1
- Postgres database
Clone the repository, install gems and setup the database by running:
git clone git@github.com:evandrodutra/shorty.git
cd shorty
bundle install
bundle exec rake db:setup
bundle exec rake db:migrate
bundle exec puma -p 3000
To run tests exec:
bundle exec rspec
You can use guard to automatically run tests and rubocop:
bundle exec guard
You can avoid all the setup above and run the project using Docker. To get Docker set up and running please read the docs: Docker install.
docker-compose build
docker-compose run app rake db:setup db:migrate
Start container and services on http://localhost:3000:
docker-compose up
docker-compose run app bundle exec rspec
curl -X POST -i -H "Content-Type: application/json" -d '{ "url": "http://www.bbc.com/", "shortcode": "bbcnews" }' "http://localhost:3000/shorten"
curl -X GET -i -H "Content-Type: application/json" "http://localhost:3000/bbcnews"
curl -X GET -i -H "Content-Type: application/json" "http://localhost:3000/bbcnews/stats"
All responses must be encoded in JSON and have the appropriate Content-Type header
POST /shorten
Content-Type: "application/json"
{
"url": "http://example.com",
"shortcode": "example"
}
Attribute | Description |
---|---|
url | url to shorten |
shortcode | preferential shortcode |
201 Created
Content-Type: "application/json"
{
"shortcode": :shortcode
}
A random shortcode is generated if none is requested, the generated short code has exactly 6 alpahnumeric characters and passes the following regexp: ^[0-9a-zA-Z_]{6}$
.
Error | Description |
---|---|
400 | url is not present |
409 | The the desired shortcode is already in use. Shortcodes are case-sensitive. |
422 | The shortcode fails to meet the following regexp: ^[0-9a-zA-Z_]{4,}$ . |
GET /:shortcode
Content-Type: "application/json"
Attribute | Description |
---|---|
shortcode | url encoded shortcode |
302 response with the location header pointing to the shortened URL
HTTP/1.1 302 Found
Location: http://www.example.com
Error | Description |
---|---|
404 | The shortcode cannot be found in the system |
GET /:shortcode/stats
Content-Type: "application/json"
Attribute | Description |
---|---|
shortcode | url encoded shortcode |
200 OK
Content-Type: "application/json"
{
"startDate": "2012-04-23T18:25:43.511Z",
"lastSeenDate": "2012-04-23T18:25:43.511Z",
"redirectCount": 1
}
Attribute | Description |
---|---|
startDate | date when the url was encoded, conformant to ISO8601 |
redirectCount | number of times the endpoint GET /shortcode was called |
lastSeenDate | date of the last time the a redirect was issued, not present if redirectCount == 0 |
Error | Description |
---|---|
404 | The shortcode cannot be found in the system |