Skip to content
A simple Rails 5 application to shorten URLs similar as bit.ly does.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
app
bin
config
db
lib
log
public
spec
storage
tmp
vendor
.env.sample
.gitignore
.rspec
.ruby-gemset
.ruby-version
Gemfile
Gemfile.lock
Procfile
README.md
Rakefile
config.ru
package.json

README.md

Urls Shortener

To setup the application in your local machine you need to have ruby 2.6.3 and rails 5.2.3 installed. Then follow the next steps:

  1. First clone the repository:
    git clone git@github.com:alexventuraio/urls-shortener.git
  2. Then, enter into the main directory:
    cd urls-shortener/
  3. Now run bundle install
  4. The database is handled by PostgreSQL, we recommend you to install it first. Then copy the sample DB configuration file:
    cp config/database.yml.sample config/database.yml
  5. Now you are ready to create the database and run the migrations by executing:
    rake db:create && rake db:migrate
  6. Now you are ready to run the Rails server with:
    rails s
  7. Finally, open your browser and go to:
    [http://localhost:3000](http://localhost:3000/)

How to run the test suite

We are using Rspec for the entire tests suite, so you can run them by executing:

bundle exec rspec

To run a specific file just add the name file like this:

bundle exec rspec spec/models/url_spec.rb

Also, make sure to set up the ENV variable for the Redis local server. Copy the sample configuration file:

cp .env.sample .env

And set the corresponding value in there:

REDIS_PROVIDER=redis://localhost:6379/0

How to run Job Services

We are handling background processing with Sidekiq and Redis but for development purpose in local environment we are are running jobs inline as you can see in the following config:

# config/application.rb
config.active_job.queue_adapter = Rails.env.production? ? :sidekiq : :async

So, if you want to run jobs in Sidekiq, then you only need to change the queue_adapter to :async and make sure you have Redis running and you should be ready to go.

The live app is at

https://myshorten.herokuapp.com/

The algorithm to shorten URL

We are taking advantage of the already existing Digest Class in Ruby language.

This class allow us to generate a hash from a given string as described in the documentation and accepts a second param to indicate the number of caracters we want to get back. So, we are taking only the first 10 characters to make it kind of short. The most commonly used class method is hexdigest and this is the one we are using, but there are also digest and base64digest.

Basically, this is the way we are generating the shorten URLs.

You can’t perform that action at this time.