A very simple URL shortener API.
Coded and tested using these versions:
apartment-2.1.0
bundler-1.16.1
factory-bot-4.8.2
faker-1.8.7
puma-3.11.2
rails-5.1.5
(API only)rspec-3.7.*
ruby-2.3.1
shoulda-matchers-3.1.2
sqlite-1.3.13
Should work with a standard bundle installation:
$ bundle install
Create, migrate and seed the DB. Seeding it is important to ensure that tenants are added by default.
$ rake db:migrate; rake db:seed
Because 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:
$ rake
Alternatively:
$ bundle exec rspec
Run the server and have fun:
$ rails s
Create 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/links
You 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/ab6ywhd0
Which 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!