A very simple URL shortener API.
Coded and tested using these versions:
apartment-2.1.0bundler-1.16.1factory-bot-4.8.2faker-1.8.7puma-3.11.2rails-5.1.5(API only)rspec-3.7.*ruby-2.3.1shoulda-matchers-3.1.2sqlite-1.3.13
Should work with a standard bundle installation:
$ bundle installCreate, migrate and seed the DB. Seeding it is important to ensure that tenants are added by default.
$ rake db:migrate; rake db:seedBecause we use multi-tenancy with the apartment gem, you will want to add them to your
/etc/hosts file:
sudo -- sh -c -e "echo '127.0.0.1 t1.shortener.test t2.shortener.test' >> /etc/hosts"Run the test suite using rake, which should hit rspec:
$ rakeAlternatively:
$ bundle exec rspecRun the server and have fun:
$ rails sCreate a new shortened URL by doing a POST to /links. Add an original parameter.
$ curl -H "Content-Type: application/json"\
-X POST -d '{ "original": "http://github.com" }'\
http://t1.shortener.test:3000/linksYou should get something like this:
{
"id":2,
"original":"http://github.com",
"shortened":"ab6ywhd0",
"subdomain":"t1",
"created_at":"2018-03-04T20:37:21.665Z",
"updated_at":"2018-03-04T20:37:21.665Z"
}Check the shortened value. That'll be your short URL. You can append it to the domain.
Resolve the shortened URL by doing a GET to the shortened value. From the example above,
assuming that the value is ab6ywhd0:
$ curl http://t1.shortener.test:3000/ab6ywhd0Which results in:
<html><body>You are being <a href="http://vplata.com">redirected</a>.</body></html>You can see the actual server response by, for example, using Postman's Interceptor. As expected,
it's an HTTP 302 with the appropriate Location response header.
Yay!