GithubHelp home page GithubHelp logo

shortener's Introduction

Shortener Build Status

A very simple URL shortener API.

Getting Started

Dependencies

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

Install

Should work with a standard bundle installation:

    $ bundle install

Database

Create, migrate and seed the DB. Seeding it is important to ensure that tenants are added by default.

    $ rake db:migrate; rake db:seed

Hosts file

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"

Test

Run the test suite using rake, which should hit rspec:

    $ rake

Alternatively:

    $ bundle exec rspec

Run

Run the server and have fun:

    $ rails s

Using it

Create a short URL

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.

Resolving a shortened URL

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!

shortener's People

Contributors

xnt avatar dependabot[bot] avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.